Today I learned about DSU or Disjoint set union. Disjoint is basically when two sets have intersection == NULL. This is a powerful tool for : 1) Finding Cycles 2) Checking if two elements are in same group or not i.e linked by some logic together Two really good questions for these ...
voidsolve(){intn;cin>>n;vector<vector<int>>a(n,vector<int>(n));cin>>a;DisjointSetUniondsu(2*(n+1));for(inti=0;i<n;i++){for(intj=i+1;j<n;j++){if(a[i][j
disjoint-set,kruskal,graphs -1 RHandarov 7 years ago 4 Comments (1) Show archived|Write comment? Jakube 7 years ago,#| 0 Link:Disjoint Set Union at cp-algorithms.com →Reply
const int maxm=111111; struct DisjointSet{ int pa[maxn]; void makeSet(int n){ for (int i=0;i<=n;i++) pa[i]=i; } int findSet(int x){ if (x!=pa[x]) pa[x]=findSet(pa[x]); return pa[x]; } void unionSet(int x,int y){ x=findSet(x); y=findSet(y); if (x...
void solve() { int n; cin >> n >> mod; initfac(); int64_t ans = 0; // 枚举最后剩下的两个点的相对顺时针的距离,枚举到n/2避免重复 for (int a = 1; a < (n + 1) / 2; a++) { int v = a + 1 - (n & 1); // 计算合法的终止点,可以直接得到其值为a + 1 - (n&...
算法竞赛模板库 by 灵茶山艾府 💭💡🎈. Contribute to EndlessCheng/codeforces-go development by creating an account on GitHub.
2 0 The Disjoint Set Union used in the alternative solution of E is special (at least I do think so). I didn't understand how to use it at first sight. I use set instead. → Reply » » platelet 19 months ago, # ^ | 0 Disjoint Set Union is used in this way, ...
-4:+1+2-3-4 -6:-1+2-3-4 -8:+1-2-3-4 -10:-1-2-3-4 事实上每次把还没碰到右边的负数区的第一个负数右移一个位置,就会使得整体-2,假如没有这个数就把+1改成-1也是使得整体-2。所以可以构造出[-sum,+sum]里面和sum奇偶相同的数。
p=2 // https://cp-algorithms.com/data_structures/disjoint_set_union.html#toc-tgt-11 // https://cp-algorithms.com/data_structures/disjoint_set_union.html#toc-tgt-12 // https://oi-wiki.org/ds/dsu/#_9 // // 模板题 https://codeforces.com/problemset/problem/1850/H 1700 /...
Total complexity: O(n⋅log(n))O(n⋅log(n)) or O(n⋅α(n))O(n⋅α(n)), based on how you construct the disjoint set union. → Reply step_by_step 6 years ago, # ^ | +3 Why not to use DFS to find the components connected by 1 edges? → Reply AkiLotus ...