CodeForces 780CAndryusha and Colored Balloons (DFS)

来源:互联网 发布:知聊赚钱是真的吗 编辑:程序博客网 时间:2024/06/06 07:36

you can find the problem here :http://codeforces.com/problemset/problem/780/C

You have a tree with N nodes and you are asked to find a dye solution for each node that child nodes of this node have different color and this node ‘s color is also different from it’s child node.
so we can simplify the question : if a node is not dyed , it’s color can’t be same it’s parent and it’s grandparent .This conclusion can be easily prove by draw some tree……

#include <bits/stdc++.h>using namespace std;int a[200005],vis[200005];int n;vector<int> edge[200005];void dfs(int u,int prevc,int cur,int p){    a[u] = cur;    int k  = 1;    for(auto v : edge[u])    {       if(v != p)       {           while(k == cur || k == prevc) k++;           dfs(v,cur,k,u);           k++;       }    }    return;}int main(){    cin>>n;    int mx = 1;    for(int i = 0;i<n-1;i++)    {        int u,v;        cin>>u>>v;        edge[u].push_back(v);        edge[v].push_back(u);        mx = max(mx,max((int)edge[u].size(),(int)edge[v].size()));    }    cout<<mx+1<<endl;    dfs(1,-1,1,-1);    for(int i = 1;i<=n;i++)    {        cout<<a[i]<<" ";    }    return 0;}
阅读全文
0 0
原创粉丝点击