POJ-1182 食物链 并查集
并查集 食物链 http://poj.org/problem?id=1182
题解 请移步http://blog.csdn.net/niushuai666/article/details/6981689
创新的亮点
1 在找爸爸的时候更新关系。
2 判断两者的关系运用的向量思维
3 关系的表达 同类0 吃1 被吃2 同时%3
为什么TE了
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#define MAX 50005
#define INF 999999
using namespace std;
int fx,fy;
int fa[MAX];
short re[MAX];
int getfather(int x)
{
if(fa[x]!=x)
{
int temp=fa[x];
fa[x]=getfather(temp);
re[x]=(re[x]+re[temp])%3;
}
return fa[x];
}
int main()
{
int n,k,d,x,y,ans=0;
cin>>n>>k;
memset(re,0,sizeof(re));
for(int i=1;i<=n;i++) fa[i]=i;
while(k--)
{
cin>>d>>x>>y;
if(x>n||y>n)
{
ans++;
continue;
}
if(d==2&&x==y)
{
ans++;
continue;
}
int fx=getfather(x);
int fy=getfather(y);
if(fx!=fy)
{
fa[fy]=fx;
re[fy]=(re[x]+d-1-re[y]+3)%3;
}
else
{
if(d==1&&re[x]!=re[y])
{ans++;
continue;
}
if(d==2&&((3 - re[x] + re[y]) % 3 != d - 1))
{
ans++;
continue;
}
}
}
cout<<ans<<endl;
return 0;
}
二叉堆
merge函数的作用是:将两个有序的序列合并为一个有序的序列。函数参数:merge(first1,last1,first2,last2,result,compare);