Rainbow Roads

Rainbow Roads

题意:给一棵n个结点的树,每条边都有一种颜色,要求找出所有的good点,good点定义:从这个点出发,到其他任意结点的简单路径(最短路径),相邻的两条边颜色都不同。

 

 题解看这里:here

不过我想了一下\(vis\)设成2就可以了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 #define endl '\n'
 5 const int maxn = 5e5+10;
 6 
 7 struct node{
 8     int to,nxt,col;
 9 }e[maxn<<1];
10 
11 int n,head[maxn],tot,c[maxn],vis[maxn];
12 int ans[maxn],cnt;
13 
14 void init(){
15     memset(head,-1,sizeof(head));
16     tot=0;
17 }
18 
19 void addedge(int u,int v,int w){
20     e[tot].to=v; e[tot].nxt=head[u]; e[tot].col=w; head[u]=tot++;
21     e[tot].to=u; e[tot].nxt=head[v]; e[tot].col=w; head[v]=tot++;
22 }
23 
24 void dfs(int fa,int now){
25     if( vis[now]>2 ) return ;
26     vis[now]++;
27     for(int i=head[now];~i;i=e[i].nxt){
28         if( e[i].to!=fa )
29             dfs(now,e[i].to);
30     }
31 }
32 
33 int main()
34 {
35     init();
36     scanf("%d",&n);
37     for(int i=1;i<n;i++){
38         int a,b,c;
39         scanf("%d%d%d",&a,&b,&c);
40         addedge(a,b,c);
41     }
42 
43     for(int i=1;i<=n;i++){
44         for(int j=head[i];~j;j=e[j].nxt){
45             c[e[j].col]++;
46         }
47         for(int j=head[i];~j;j=e[j].nxt){
48             if( c[e[j].col]>1 ){
49                 dfs(i,e[j].to);
50             }
51         }
52         for(int j=head[i];~j;j=e[j].nxt){
53             c[e[j].col]--;
54         }
55     }
56 
57     for(int i=1;i<=n;i++){
58         if( !vis[i] ) ans[++cnt]=i;
59     }
60     printf("%d\n",cnt);
61     for(int i=1;i<=cnt;i++){
62         printf("%d\n",ans[i]);
63     }
64 
65     return 0;
66 }

 

posted @ 2020-08-04 23:12  swsyya  阅读(219)  评论(0编辑  收藏  举报

回到顶部