cdoj1100 并查集
http://acm.ccucomp.cn/acmhome/problemdetail.do?&method=showdetail&id=1100
下面代码错了!!!!只是在学校oj上面ac了
#include <iostream>
#include <math.h>
using namespace std;
typedef struct node
{
int pre;
int next;
int v;
}Lan;
Lan lan[50001];
int Find(int n)
{
int f;
f=n;
while(f!=lan[f].v)
f=lan[f].v;
return f;
}
void Merge(int x,int y)
{
lan[x].v=y;
if(lan[y].pre==0)
lan[y].pre=lan[x].pre;
if(lan[y].next==0)
lan[y].next=lan[x].next;
}
int main()
{
int n,k;
int count;
while(cin>>n>>k)
{
count=0;
int i,j,x,y,d;
//memset(flag,0,sizeof(flag));
for(i=0;i<=n;i++)
{
lan[i].v=i;
lan[i].pre=0;
lan[i].next=0;
}
int findx,findy;
for(i=0;i<k;i++)
{
cin>>d>>x>>y;
if(x>n||y>n)
{ count++;continue; }
if(d==2&&x==y)
{ count++;continue; }
if(d==1)
{// if(!Merge(x,y))
findx=Find(x);
findy=Find(y);
if(lan[findx].pre==findy||lan[findx].next==findy)
count++;
else
Merge(findx,findy);
}
if(d==2)
{
findx=Find(x);
findy=Find(y);
if(findx==findy||lan[findx].pre==findy)
count++;
else
{
if(lan[findy].pre!=0)
Merge(findx,lan[findy].pre);
if(lan[findx].next!=0)
Merge(findy,lan[findx].next);
lan[findx].next=findy;
lan[findy].pre=findx;
}/////////////
}
}
cout<<count<<endl;
}
return 0;
}