您的位置:首页 > 其它

BZOJ 1603: [Usaco2008 Oct]打谷机|dfs

2016-01-21 16:51 253 查看
深搜一边即可

#include<set>
#include<map>
#include<ctime>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define T 5006
#define MX 1e9
using namespace std;
int f[T],head[T],lst[T],nxt[T],v[T];
int n,m,tot;
int sc()
{
int i=0,f=1; char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
return i*f;
}
void insert(int x,int y,int z)
{
lst[++tot]=y; nxt[tot]=head[x]; v[tot]=z; head[x]=tot;
lst[++tot]=x; nxt[tot]=head[y]; v[tot]=z; head[y]=tot;
}
void dfs(int x,int fa)
{
for(int i=head[x];i;i=nxt[i])
if(lst[i]!=fa)
{
f[lst[i]]=v[i]^f[x];
dfs(lst[i],x);
}
}
int main()
{
n=sc();
for(int i=1;i<n;i++)
{
int x=sc(),y=sc(),c=sc();
insert(x,y,c);
}
f[1]=0;
dfs(1,0);
cout<<f
;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dfs