思路: 图论模型化,种类数就是点,对数就是边, 要求始终不存在子图使点数等于边数,就是不允许有环,使用并查集即可。
代码:
#include<iostream>
#include<cstdio>
#define N 100001
using namespace std;
int p[N+5];
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
void init()
{
for(int i=0;i<N;i++)
{
p[i]=i;
}
}
int main()
{
int a,b;
init();
int count=0;
while(scanf("%d",&a)==1)
{
if(a==-1)
{
cout<<count<<endl;
count=0;
init();
}
else scanf("%d",&b);
int x=find(a);
int y=find(b);
if(x!=y) p[x]=y;
else count++;
}
}