比赛 4043级NOIP2022欢乐赛8th 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 Dance Mooves 最终得分 100
用户昵称 HeSn 运行时间 4.433 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-11-21 20:15:58
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 100010;
int n, m, a[MAXN], fa[MAXN];
map<int, int> mp[MAXN];
vector<int> cd[MAXN];
int find(int x) {
	if(x == fa[x]) {
		return x;
	}
	return fa[x] = find(fa[x]);
}
signed main() {
	freopen("dance.in", "r", stdin);
	freopen("dance.out", "w", stdout);
	cin >> n >> m;
	for(int i = 1; i <= n; i ++) {
		fa[i] = i;
		cd[i].push_back(i);
		a[i] = i;
	}
	for(int i = 1; i <= m; i ++) {
		int x, y;
		cin >> x >> y;
		swap(a[x], a[y]);
		cd[a[x]].push_back(x);
		cd[a[y]].push_back(y);
	}
	for(int i = 1; i <= n; i ++) {
		fa[find(i)] = find(a[i]);
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 0; j < cd[a[i]].size(); j ++) {
			mp[find(a[i])][cd[a[i]][j]] = 1;
		}
	}
	for(int i = 1; i <= n; i ++) {
		cout << mp[find(a[i])].size() << endl;
	}
	return 0;
}