Not Assigning(1400)
互联网 2022/3/1 23:22:41
1 /**\2 https://codeforces.com/contest/1627/problem/C3 想要满足能够赋值的条件,必须所有点的度都不能>34 那么树退化成了链,就op记下输出格式,然后ans记录dfs的结果5 由于链式 2 3 2 3 就满足prime path6 \**/7 #include <bits/stdc++.h>8 using namespace s…
1 /**\ 2 https://codeforces.com/contest/1627/problem/C 3 想要满足能够赋值的条件,必须所有点的度都不能>3 4 那么树退化成了链,就op记下输出格式,然后ans记录dfs的结果 5 由于链式 2 3 2 3 就满足prime path 6 \**/ 7 #include <bits/stdc++.h> 8 using namespace std; 9 #define fi first 10 #define se second 11 #define go continue 12 #define int long long 13 #define PII pair<int, int> 14 #define sf(x) scanf("%lld",&x) 15 #define ytz int _; sf(_); while(_--) 16 #define fory(i,a,b) for(int i = a; i <= b; ++i) 17 #define forl(i,a,b) for(int i = a; i >= b; --i) 18 #define debug(a) cout << #a << " = " << a <<endl; 19 const int N = 1e5 + 10; 20 struct node 21 { 22 int to, next; 23 } e[N << 1]; 24 int cnt, head[N]; 25 vector<PII> op; 26 map<pair<int, int>, int> ans; 27 int vis[N]; 28 int c[N]; 29 int n; 30 inline void init() 31 { 32 cnt = 0; 33 fory(i, 0, n) head[i] = -1, c[i] = vis[i] = 0; 34 ans.clear(); 35 op.clear(); 36 } 37 inline void add_edge(int u, int v) 38 { 39 e[++cnt].to = v; 40 e[cnt].next = head[u]; 41 head[u] = cnt; 42 c[v]++; 43 } 44 void dfs(int root, int num) 45 { 46 47 vis[root] = 1; 48 for(int i = head[root]; ~i; i = e[i].next) 49 { 50 int j = e[i].to; 51 if(!vis[j]) 52 { 53 ans[ {root, j}] = ans[ {j, root}] = num; 54 dfs(j, 5 - num); 55 } 56 } 57 } 58 void solve() 59 { 60 sf(n); 61 init(); 62 for(int i = 0; i < n - 1; ++i) 63 { 64 int x, y; 65 sf(x), sf(y); 66 add_edge(x, y), add_edge(y, x); 67 op.push_back({x, y}); 68 } 69 for(auto t : op) 70 { 71 if(c[t.first] > 2 || c[t.second] > 2) 72 { 73 puts("-1"); 74 return; 75 } 76 } 77 fory(i, 1, n) 78 { 79 if(c[i] == 1) 80 { 81 dfs(i, 3); 82 break; 83 } 84 } 85 for(auto t : op) 86 { 87 printf("%lld ", ans[ {t.first, t.second}]); 88 } 89 puts(""); 90 } 91 signed main() 92 { 93 ytz 94 { 95 solve(); 96 } 97 return 0; 98 }

关于找一找教程网
本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[Not Assigning(1400)]http://www.zyiz.net/tech/detail-304359.html
赞(2)
- 2023-06-01一文详解 Sa-Token 中的 SaSession 对象
- 2023-06-01本科生求职,一种全新的人生挑战
- 2023-06-014.2 字节流与字符流
- 2023-06-01SpringBoot定义优雅全局统一Restful API 响应框架五
- 2023-06-01枪决通知短信:网络欺诈的新变种与社会责任
- 2023-06-01十大移动应用恶意行为
- 2023-05-31OPPO关停自研芯片公司哲库,这对行业将产生什么影响?
- 2023-05-31TiKV 新架构:Partitioned Raft KV 原理解析
- 2023-05-31【中奖公告】成长故事分享:聊聊你的进步经历,与君共勉
- 2023-05-31想卷卷不赢,想躺躺不平