【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons
从任意点出发,贪心染色即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include<cstdio> #include<algorithm> using namespace std; int v[200010<<1],next[200010<<1],first[200010],e; void AddEdge( int U, int V) { v[++e]=V; next[e]=first[U]; first[U]=e; } bool vis[200010]; int n,col[200010]; void dfs( int U, int n1, int n2) { vis[U]=1; int co=0; for ( int i=first[U];i;i=next[i]) if (!vis[v[i]]) { ++co; while (co==n1 || co==n2) ++co; col[v[i]]=co; dfs(v[i],col[U],col[v[i]]); } } int main() { // freopen("c.in","r",stdin); int x,y; scanf ( "%d" ,&n); for ( int i=1;i<n;++i) { scanf ( "%d%d" ,&x,&y); AddEdge(x,y); AddEdge(y,x); } col[1]=1; dfs(1,1,-1); printf ( "%d\n" ,*max_element(col+1,col+n+1)); for ( int i=1;i<n;++i) printf ( "%d " ,col[i]); printf ( "%d\n" ,col[n]); return 0; } |
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步