BZOJ3498: PA2009 Cakes(三元环)
程序员文章站
2022-06-30 13:26:12
题意 "题目链接" Sol 按照套路把边转成无向图,我们采取的策略是从权值大的向权值小的连边 然后从按权值从小到大枚举每个点,再枚举他们连出去的点$v$ 如果$v$的度数$\leqslant M$,那么就再暴力枚举$v$连出去的点$t$,看$u$与$t$是否联通(打标记) 否则暴力枚举$u$连出去的 ......
题意
sol
按照套路把边转成无向图,我们采取的策略是从权值大的向权值小的连边
然后从按权值从小到大枚举每个点,再枚举他们连出去的点\(v\)
如果\(v\)的度数\(\leqslant m\),那么就再暴力枚举\(v\)连出去的点\(t\),看\(u\)与\(t\)是否联通(打标记)
否则暴力枚举\(u\)连出去的点\(t\),看\(v\)与\(t\)是否联通(直接hash表)
复杂度为\(o(m \sqrt{m})\)
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn = 100001; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n, m, a[maxn], block, siz[maxn], flag[maxn], rak[maxn], tp[maxn]; vector<int> v[maxn]; set<int> s[maxn]; int comp(const int &x, const int &y) { return a[x] == a[y] ? x < y : a[x] < a[y]; } int main() { n = read(); m = read(); block = sqrt(m); for(int i = 1; i <= n; i++) a[i] = read(), tp[i] = i; sort(tp + 1, tp + n + 1, comp); for(int i = 1; i <= n; i++) rak[tp[i]] = i; for(int i = 1; i <= m; i++) { int x = read(), y = read(); if(rak[x] > rak[y]) v[x].push_back(y), siz[x]++; else v[y].push_back(x), siz[y]++; } ll ans = 0; for(int i = 3; i <= n; i++) { int x = tp[i]; for(int j = 0, to; j < v[x].size(); j++) flag[to = v[x][j]] = i; for(int j = 0, to; j < v[x].size(); j++) { if(siz[to = v[x][j]] <= block) { for(int k = 0; k < v[to].size(); k++) if(flag[v[to][k]] == i) ans += a[x]; } else { for(int k = 0; k < v[x].size(); k++) if(s[to].count(v[x][k])) ans += a[x]; } s[x].insert(to); } } cout << ans; return 0; } /* 2 1 13 17 2 1 */
下一篇: 我让你玩一会儿好了