#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 1E5 + 10;
int fa[maxn],d[maxn],cnt[maxn];
inline void ini(int n){
for(int i = 0; i <= n ;++i){
fa[i] = i;
}
memset(d,0,sizeof(int) * (n + 1));
memset(cnt,0,sizeof(int) * (n + 1));
}
int fnd(int x){
return fa[x] == x ? x:fa[x] = fnd(fa[x]);
}
inline void uni(int x,int y){
fa[fnd(x)] = fnd(y);
}
int main(){
int n,m,a,b;
scanf("%d%d",&n,&m);
ini(n);
for(int i = 0;i < m ; ++i){
scanf("%d%d",&a,&b);
++d[a];++d[b];
uni(a,b);
}
int f;
for(int i = 1;i <= n ; ++i){
f = fnd(i);
++cnt[f];
if(f != i) d[f] += d[i];
}
int ans = 0;
for(int i = 1; i <= n ; ++i){
if(fa[i] == i && cnt[i] == d[i] / 2 + 1){
++ans;
}
}
printf("%d\n",ans);
return 0;
}
[2016-04-01][codeforces][659E][New Reform]
原文:http://www.cnblogs.com/qhy285571052/p/d93ea77a101a98b7951d3b5a052a777b.html