ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Not Assigning(1400)

2022-03-01 22:34:17  阅读:149  来源: 互联网

标签:int root define ans 1400 Assigning sf op


 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 }

 

标签:int,root,define,ans,1400,Assigning,sf,op
来源: https://www.cnblogs.com/-ytz/p/15952935.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有