#include<iostream> #include<map> usingnamespace std; int fa[100001]; int cnt[100001]; intfind(int i) { if (fa[i] == i) return fa[i]; return fa[i] = find(fa[i]); } voiduni(int i, int j) { int ifa = find(i); int jfa = find(j); fa[ifa] = jfa; } intmain() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= n; i++) { int x; cin >> x; // 读入的数字和它的序列混一起 uni(x, i); } for (int i = 1; i <= n; i++) cnt[find(i)]++; int ans = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; ans += cnt[find(x)]; cnt[find(x)] = 0; cout << ans << " "; } cout << "\n"; } return0; }
#include<iostream> #include<queue> usingnamespace std; int fa[40002]; intfind(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } voiduni(int x, int y) { int f1 = find(x); int f2 = find(y); if (f1 != f2) fa[f1] = f2; } structaa { int x, y, w; booloperator<(const aa &a) const { return w < a.w; } }; priority_queue<aa> q; intmain() { int n, m; cin >> n >> m; //记得对立面也要初始化 for (int i = 1; i <= 2 * n; i++) fa[i] = i; int x, y, w; while (m--) { cin >> x >> y >> w; q.push({x, y, w}); } while (!q.empty()) { aa a = q.top(); if (find(a.x) != find(a.y)) { //放在对方对立面~ uni(a.x + n, a.y); uni(a.y + n, a.x); } else { cout << a.w; return0; } q.pop(); } cout << 0; }
#include<iostream> usingnamespace std; int a[33][33]; intmain() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) a[1][i] = i + 1; for (int i = 2; i <= n; i++) { int k = 2; int g = i - 1; while (g % 2 == 0) { k *= 2; g /= 2; } for (int j = 1; j < n; j++) { if (a[i - 1][j] == i) a[i][j] = i - 1; else { int b = a[i - 1][j] - 1; int d = b % k; b /= k; a[i][j] = (b + 1) * k - d; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j < n; j++) cout << a[i][j] << " "; cout << "\n"; } }