Loading

Codeforces Round #628 (Div. 2) C. Ehab and Path-etic MEXs(思维题+一点点图论)

You are given a tree consisting of nn nodes. You want to write some labels on the tree's edges such that the following conditions hold:

  • Every label is an integer between 00 and n2n−2 inclusive.
  • All the written labels are distinct.
  • The largest value among MEX(u,v)MEX(u,v) over all pairs of nodes (u,v)(u,v) is as small as possible.

Here, MEX(u,v)MEX(u,v) denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node uu to node vv .

Input

The first line contains the integer nn (2n1052≤n≤105 ) — the number of nodes in the tree.

Each of the next n1n−1 lines contains two space-separated integers uu and vv (1u,vn1≤u,v≤n ) that mean there's an edge between nodes uu and vv . It's guaranteed that the given graph is a tree.

Output

Output n1n−1 integers. The ithith of them will be the number written on the ithith edge (in the input order).

Examples
Input
Copy
3
1 2
1 3
Output
Copy
0
1
Input
Copy
6
1 2
1 3
2 4
2 5
5 6
Output
Copy
0
3
2
4
1
这题一开始没读懂题意,各种最大最小最大也很绕...看了别人的博客才差不多理解了...
大意是给定一棵树,要求给它的各个边0~n-2编号,要满足任意两点间最大的MEX值最小。MEX(u,v)代表点u到点v的路径(是唯一的)所有边的编号构成的集合里最小的没有出现的数。然后找度数大于等于3的点,如果没有的话说明这是一条链,随便给边赋编号就行;如果有的话就把这个点连的三条边赋值成0,1,2,其他边任意,这样能保证所有的MEX始终小于等于2,可以自己举几个例子理解一下。
#include <bits/stdc++.h>
#define N 100005
using namespace std;
int n;
int node[100005]={0};
struct edge
{
    int x;
    int y;
    int num;
}e[100005];
int main()
{
    cin>>n;
    int i;
    int nod=0;//存储度大于等于3的点 
    for(i=1;i<=n-1;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        e[i].x=x;
        e[i].y=y;
        e[i].num=-1;
        node[x]++;
        node[y]++;
        if(node[x]>=3)nod=x;
        if(node[y]>=3)nod=y; 
    }
    int cnt;
    if(nod)
    {
        cnt=0;
        for(i=1;i<=n-1;i++)
        {
            if(cnt==3)break;
            if(e[i].x==nod||e[i].y==nod)
            {
                e[i].num=cnt;
                cnt++;
            }
        }
        for(i=1;i<=n-1;i++)
        {
            if(e[i].num==-1)
            {
                cout<<cnt<<endl;
                cnt++;
            }
            else
            {
                cout<<e[i].num<<endl;
            }
        }
    }
    else
    {
        for(i=0;i<n-1;i++)cout<<i<<endl;
    }
    return 0;
}

 



posted @ 2020-03-15 23:08  脂环  阅读(192)  评论(0编辑  收藏  举报