[ABC292D] Unicyclic Components - 洛谷
#include <bits/stdc++.h> #define int long long #define PII pair<int,int> #define endl '\n' using namespace std; const int N=1e6+10,M=N*2,INF=0x3f3f3f3f; int p[N],cnt[N],side[N]; int find(int x) { if(p[x]!=x) { p[x]=find(p[x]); } return p[x]; } signed main() { cin.tie(0);cout.tie(0); ios::sync_with_stdio(0); int n,m;cin>>n>>m; for(int i=1;i<=n;i++)p[i]=i,cnt[i]=1; for(int i=1;i<=m;i++) { int x,y;cin>>x>>y; int px=find(x);int py=find(y); if(px!=py) { p[px]=py; cnt[py]+=cnt[px]; side[py]+=side[px]+1; } else side[py]+=1; } int flag=0; for(int i=1;i<=n;i++) { if(p[i]==i) { if(cnt[i]!=side[i]) { //cout<<i<<side[i]<<cnt[i];; cout<<"No"<<endl; flag=1; break; } } } if(!flag) { cout<<"Yes"<<endl; } return 0; }