# Papa的坦克（原BZOJ 1603 打谷机）

4
2 3 0
3 4 1
1 2 0

1

## dfs解法：

#include<stdio.h>

struct Edge{
int to,next,val;
}edge[1001];
int n,cnt,first[1001],f[1001];

{
edge[++cnt].to=to;
edge[cnt].val=val;
edge[cnt].next=first[from];
first[from]=cnt;
}

void dfs(int x,int from)
{
for(int i=first[x];i;i=edge[i].next)
{
f[edge[i].to]=f[x]^edge[i].val;
dfs(edge[i].to,x);
}
}

int main()
{
scanf("%d",&n);
int from,to,val;
while(scanf("%d%d%d",&from,&to,&val)!=EOF){
}
dfs(1,0);
printf("%d",f[n]);
return 0;
}

## 拆点并查集解法：

#include<cstdio>
#include<cstring>
int ufs[5000];

int Find(int x)
{
return ufs[x] ==x?x:ufs[x]= Find(ufs[x]);
}

int main()
{
int n, i, t1, t2, x, y, t;
scanf("%d", &n);
{
memset(ufs, 0, sizeof(ufs));
for(i=1; i<=n*2+1 ;i++)
ufs[i]=i;
for(i=1;i<=n-1;i++)
{
scanf("%d%d%d", &x, &y, &t);
if(t==0)
{
t1 = Find(x);
t2 = Find(y);
ufs[t1] = t2;

t1 = Find(x+n);
t2 = Find(y+n);
ufs[t1] = t2;
}
else
{
t1 = Find(x);
t2 = Find(y+n);
ufs[t1] = t2;

t1 = Find(x+n);
t2 = Find(y);
ufs[t1] = t2;
}
}
if(Find(1)==Find(n))
printf("0\n");
else
printf("1\n");
}
return 0;
}

posted @ 2018-07-29 18:51  qseer  阅读(217)  评论(0编辑  收藏  举报