标签:mem insert splay opened force scanf cst gif blog
人傻多做题
----------- 昏割线 -----------
【】想出来的
【补】想不出来看题姐的
10.27
cf 782c Andryusha and Colored Balloons
【】 dfs 染色和父亲,父亲的父亲不同 儿子挨着放
1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <set> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 const int maxn = 3e5 + 5; 9 10 int n,c[maxn],vis[maxn]; 11 vector<int> g[maxn]; 12 13 void dfs(int u,int fa) 14 { 15 vis[u] = 1; 16 int p = 0; 17 for (int i = 0;i < g[u].size();i++) 18 { 19 int v = g[u][i]; 20 if (vis[v] || v == fa) continue; 21 for(;;) 22 { 23 p++; 24 if (p != c[u] && p != c[fa]) break; 25 } 26 c[v] = p; 27 dfs(v,u); 28 } 29 } 30 31 int main() 32 { 33 scanf("%d",&n); 34 int u,v; 35 for (int i = 1;i < n;i++) 36 { 37 scanf("%d %d",&u,&v); 38 g[u].push_back(v); 39 g[v].push_back(u); 40 } 41 42 memset(c,0,sizeof(c)); 43 memset(vis,0,sizeof(vis)); 44 45 c[1] = 1; 46 dfs(1,-1); 47 48 49 // for (int i = 1;i <= n;i++) printf("c[%d] = %d\n",i,c[i]); 50 set<int> s; 51 for (int i = 1;i <= n;i++) s.insert(c[i]); 52 printf("%d\n",s.size()); 53 for (int i = 1;i <= n;i++) printf("%d ",c[i]); 54 puts(""); 55 56 57 return 0; 58 } 59 60 61 /* 62 63 5 64 2 3 65 5 3 66 4 3 67 1 3 68 69 */
标签:mem insert splay opened force scanf cst gif blog
原文地址:http://www.cnblogs.com/wuyuewoniu/p/7742693.html