比赛 4043级NOIP2022欢乐赛8th 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 Dance Mooves 最终得分 100
用户昵称 lihaoze 运行时间 2.418 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-11-21 20:39:38
显示代码纯文本
#include "bits/stdc++.h"

const int N = 1e5 + 10;
int n, k;
int fa[N], a[N];

std::vector<int> to[N];
std::set<int> circle[N];

int find(int x) {
    return x == fa[x] ? x : fa[x] = find(fa[x]);
}

int main() {
    freopen("dance.in", "r", stdin); 
    freopen("dance.out", "w", stdout);

    for (int i = 1; i < N; ++ i) {
        fa[i] = a[i] = i;
        to[i].push_back(i);
    }

    std::cin >> n >> k;

    for (int i = 1; i <= k; ++ i) {
        int x, y;
        std::cin >> x >> y;

        std::swap(a[x], a[y]);
        to[a[x]].push_back(y),
        to[a[y]].push_back(x);
    }

    for (int i = 1; i <= n; ++ i) {
        fa[find(i)] = find(a[i]);
    }

    for (int i = 1; i <= n; ++ i) {
        for (auto v : to[i]) 
            circle[find(i)].insert(v);
    }

    for (int i = 1; i <= n; ++ i) {
        std::cout << circle[find(i)].size() << '\n';
    }
    return 0;
}