CF628 C.C. Ehab and Path-etic MEXs(思维+拓扑排序)
题意:没咋看懂,它的题解是度数最高的分配后其他的随便分配。
我做的是,从子节点往上面,依次加,拓扑排序
#pragma GCC optimize(3 , "Ofast" , "inline")
#include <bits/stdc++.h>
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int> pi;
template<class T>
inline void read (T &x) {
x = 0;
int sign = 1;
char c = getchar ();
while (c < '0' || c > '9') {
if ( c == '-' ) sign = - 1;
c = getchar ();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar ();
}
x = x * sign;
}
const int maxn = 1e6 + 10;
const int inf = int (1e9);
const ll INF = ll (1e18);
const double PI = acos (- 1);
const int mod = 1e9+7;
const double eps = 1e-8;
int n;
vector<int>e[maxn];
pi id[maxn];
map<pi,int>ans;
int deg[maxn];
bool vis[maxn];
void topsort(){
queue<int>q;
rep (i,1,n) {
if(deg[i]==1) {
q.push(i);
}
}
int cnt=0;
while(!q.empty()){
int top=q.front();
q.pop();
vis[top]=true;
int siz = e[top].size ();
rep (i,0,siz-1){
int v=e[top][i];
if(vis[v]) continue;
if(--deg[v]==1){
q.push(v);
}
ans[pi(min(top,v),max(top,v))]=cnt++;
}
}
}
int main () {
scanf ("%d",&n);
rep (i,1,n-1) {
int u,v;
scanf ("%d%d",&u,&v);
deg[u]++;
deg[v]++;
e[u].push_back (v);
e[v].push_back (u);
id[i]=pi(min(u,v),max(u,v));
}
topsort ();
rep (i,1,n-1) {
printf ("%d\n",ans[id[i]]);
}
return 0;
}