https://codeforces.com/contest/13
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 #define int long long 5 const int maxn = 1e5 + 5; 6 int n; 7 int u, v; 8 vector<int> g[maxn]; 9 int ans[maxn]; 10 11 signed main() { 12 //freopen("in", "r", stdin); 13 ios::sync_with_stdio(0); 14 cin >> n; 15 16 for (int i = 1; i < n; i++) { 17 cin >> u >> v; 18 g[u].push_back(i); 19 g[v].push_back(i); 20 ans[i] = -1; 21 } 22 int cnt = 0; 23 for (int i = 1; i <= n; i++) { 24 if (g[i].size() >= 3) { 25 for (int j = 0; j < 3; j++) { 26 ans[g[i][j]] = j; 27 } 28 cnt = 3; 29 break; 30 } 31 } 32 33 for (int i = 1; i < n; i++) { 34 if (ans[i] == -1) 35 ans[i] = cnt++; 36 } 37 for (int i = 1; i < n; i++) 38 cout << ans[i] << endl; 39 return 0; 40 }
这是一道图论的构造题,有时候不必纠结样例一样不,比如说第二个样例的输出结果
给了n 个点,n - 1条边,每条边的两个端点u,v 找u,v对应的最大的最小
n 个顶点的树是n--1条边
如果是链的话,这几个数字怎么排都可以
如果不是的话,节点的度= 3的时候,排0,1,2
>3的话,就变成任意链了,自己画一下就知道了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 int u,v,x; 5 signed main(){ 6 //freopen("in","r",stdin); 7 ios::sync_with_stdio(0);//用这个不能用" " 8 cin >> u >> v; 9 // cout << u << v << endl; 10 if(v < u) cout << "-1"; 11 else if(v == u) { 12 if(u) cout << 1 << endl; 13 cout << u;//当=0时,最后一组杨样例给的是0 14 } 15 else{ 16 x = (v - u) / 2; 17 if((v - u) & 1) cout << "-1";//这里是(v-u),不是x 18 else{ 19 if((x & u) == 0) { 20 cout << 2 << endl; 21 cout << u + x << " " << x; 22 } 23 else { 24 cout << 3 << endl; 25 cout << u << " " << x << " " << x; 26 } 27 } 28 } 29 return 0; 30 }