标签:space ace test https set cpp pair contest ret
#include <iostream>
#include <unordered_set>
using namespace std;
const int N = 1e6 + 10;
int f[N];
pair<int,int>p[N];
int idx = 0;
int ans[N];
int main()
{
int n;
cin >> n;
int idx1 = n - 2;
for(int i = 0;i < n - 1;i ++)
{
int a,b;
cin >> a >> b;
p[i] = {a,b};
f[a]++;
f[b]++;
}
unordered_set<int>st;
for(int i = 1;i <= n;i ++)
{
if(f[i] == 1){
st.insert(i);
}
if(st.size() >= n - 1){
break;
}
}
for(int i = 0;i < n - 1;i ++)
{
int a = p[i].first,b = p[i].second;
if(st.count(a) || st.count(b)){
ans[i] = idx++;
}
else{
ans[i] = idx1--;
}
}
for(int i = 0;i < n - 1;i ++)
{
cout << ans[i] << endl;
}
return 0;
}
标签:space ace test https set cpp pair contest ret
原文地址:https://www.cnblogs.com/QFNU-ACM/p/13113451.html