bzoj1603 / P2913 [USACO08OCT]车轮旋转Wheel Rotation

P2913 [USACO08OCT]车轮旋转Wheel Rotation

稳妥起见(防止数据出锅),用了bfs

每次的转移可以直接用异或和解决。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 #define re register
 6 using namespace std;
 7 void read(int &x){
 8     char c=getchar();x=0;
 9     while(!isdigit(c)) c=getchar();
10     while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
11 }
12 #define N 1002
13 queue <int> h;
14 int n,d[N]; bool vis[N];
15 int cnt,hd[N],nxt[N<<1],ed[N],poi[N<<1],val[N<<1];
16 void adde(int x,int y,int v){
17     nxt[ed[x]]=++cnt; hd[x]=hd[x]?hd[x]:cnt;
18     ed[x]=cnt; poi[cnt]=y; val[cnt]=v;
19 }
20 int main(){
21     read(n); int q1,q2,q3;
22     for(re int i=1;i<n;++i){
23         read(q1);read(q2);read(q3);q3^=1;//先取反,便于后面的异或
24         adde(q1,q2,q3); adde(q2,q1,q3);
25     } 
26     h.push(1); d[1]=1; vis[1]=1;
27     while(!h.empty()){
28         int x=h.front(); h.pop();
29         for(int i=hd[x];i;i=nxt[i]){
30             int to=poi[i];
31             if(!vis[to]){
32                 d[to]=d[x]^val[i];
33                 if(to==n){
34                     printf("%d",d[n]);
35                     return 0;
36                 }
37                 vis[to]=1;
38                 h.push(to);
39             }
40         }
41     }
42 }
View Code

 

posted @ 2018-10-25 16:42  kafuuchino  阅读(216)  评论(0编辑  收藏  举报