Codeforces 780C-Andryusha and Colored Balloons dfs
来源:互联网 发布:中山淘宝摄影 编辑:程序博客网 时间:2024/06/10 06:49
题意:给定一颗树,要在结点上涂色。规定要求每三个直接相连的结点颜色不能相同。求用最少的颜色使满足要求,并输出颜色结点颜色。
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,ox3f,sizeof(a))#define ll long long#define Rep(i,a,n) for(int i=a;i<=n;i++)using namespace std;const int INF = ( 2e9 ) + 2;const int maxn = 2e5+10;vector<int> g[maxn];int c[maxn],ans;void dfs(int u,int fa){int x=0;//避免兄弟结点颜色相同 for(int i=0,L=g[u].size();i<L;i++){int v=g[u][i];if(v==fa||c[v])continue;// 这个点已经染色 x++;while(x==c[u]||x==c[fa])x++;//避免与父节点和祖先结点颜色相同 c[v]=x;dfs(v,u);}if(x>ans)ans=x;}int main(){int n;//freopen("in.txt","r",stdin);scanf("%d",&n);for(int i=0;i<n-1;i++){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);}ans=-1;c[1]=1;dfs(1,0);printf("\n%d\n",ans);for(int i=1;i<=n;i++)printf("%d ",c[i]);}
阅读全文
0 0
- Codeforces 780C Andryusha and Colored Balloons 搜索dfs
- CodeForces 780C Andryusha and Colored Balloons【DFS】
- codeforces 780-C. Andryusha and Colored Balloons(dfs)
- Codeforces 780C-Andryusha and Colored Balloons dfs
- Codeforces 780C-Andryusha and Colored Balloons
- Codeforces 782C Andryusha and Colored Balloons dfs
- Codeforces Round #403 C. Andryusha and Colored Balloons(DFS)
- 【codeforces 782C】Andryusha and Colored Balloons
- codeforces 782c Andryusha and Colored Balloons
- Codeforces Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- C. Andryusha and Colored Balloons
- codeforces 781A Andryusha and Colored Balloons(树上dfs)
- 【Codeforces 781 A Andryusha and Colored Balloons】+ DFS
- Codeforces 782C Andryusha and Colored Balloons【思维+Dfs暴力染色】
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons dfs(树)
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons(DFS 邻接表 数据结构)
- SimpleDateFormat 使用注意事项
- linux下boost库的安装及使用
- 连接池
- Could not resolve hostname t143ghl: Name or service not known lost connection
- 配置yum仓库
- Codeforces 780C-Andryusha and Colored Balloons dfs
- js实现汉字转拼音
- ProgressDialog和AsyncTask结合使用
- Python scikit-learn 模块的SVM方法介绍
- 关闭eclipse控制台的跳出
- js判断undefined类型
- 决策树(Decision Tree)
- Spring Cloud入门一:Spring Cloud Config
- ajax同步查询