比赛 |
4043级NOIP2022欢乐赛8th |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
Dance Mooves |
最终得分 |
100 |
用户昵称 |
yrtiop |
运行时间 |
1.633 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2022-11-21 20:40:27 |
显示代码纯文本
// Problem: P7299 [USACO21JAN] Dance Mooves S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P7299
// Memory Limit: 250 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
const int maxn = 2e5 + 5;
int n,k,p[maxn],pos[maxn],pre[maxn],ans[maxn];
std::set<int> s;
std::vector<int> G[maxn];
bool vis[maxn];
int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
void dfs(int x) {
if(vis[x])return ;
vis[x] = true;
for(auto& v : G[x])
s.emplace(v);
if(!G[x].empty())dfs(G[x].back());
return ;
}
int main() {
freopen("dance.in","r",stdin);
freopen("dance.out","w",stdout);
scanf("%d %d",&n,&k);
for(int i = 1;i <= n;++ i)
p[i] = i;
for(int i = 1;i <= k;++ i) {
int x,y;
scanf("%d %d",&x,&y);
G[p[x]].pb(y);
G[p[y]].pb(x);
std::swap(p[x] , p[y]);
}
for(int i = 1;i <= n;++ i) {
pre[i] = i;
}
for(int i = 1;i <= n;++ i) {
if(G[i].empty())continue ;
if(find(i) == find(G[i].back()))continue ;
pre[find(i)] = find(G[i].back());
}
for(int i = 1;i <= n;++ i) {
if(!ans[find(i)]) {
s.clear();
s.emplace(i);
dfs(i);
ans[find(i)] = s.size();
}
printf("%d\n",ans[find(i)]);
}
return 0;
}