[Codeforces Round #628]1325C - Ehab and Path-etic MEXs[思维][图]

1325C - Ehab and Path-etic MEXs[思维][]

time limit per testmemory limit per testinputoutput
1 seconds256 megabytesstandard inputstandard output

Description:

You are given a tree consisting of n n n nodes. You want to write some labels on the tree’s edges such that the following conditions hold:
Every label is an integer between 0 0 0 and n − 2 n−2 n2 inclusive.
All the written labels are distinct.
The largest value among M E X ( u , v ) MEX(u,v) MEX(u,v) over all pairs of nodes ( u , v ) (u,v) (u,v) is as small as possible.
Here, M E X ( u , v ) 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 u u u to node v v v.

Input

The first line contains the integer n ( 2 ≤ n ≤ 1 0 5 ) n (2≤n≤10^5) n(2n105) — the number of nodes in the tree.
Each of the next n − 1 n−1 n1 lines contains two space-separated integers u u u and v ( 1 ≤ u , v ≤ n ) v (1≤u,v≤n) v(1u,vn) that mean there’s an edge between nodes u u u and v v v. It’s guaranteed that the given graph is a tree.

Output

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


One Example input

3
1 2
1 3

One Example output

0
1

One Example input

6
1 2
1 3
2 4
2 5
5 6

One Example output

0
3
2
4
1


分析:
题意:
n n n个结点, n − 1 n-1 n1条边,一个联通的图
现在给每条边赋值 0 0 0 ( n − 2 ) (n-2) (n2)中任意一个值,且每条边不重复
要使得任意两点 u , v u, v u,v的简单路径的 M E X ( u , v ) MEX(u,v) MEX(u,v)都尽可能小,问边应该如何赋值
其中 M E X ( u , v ) MEX(u,v) MEX(u,v)表示MEX指的是不在集合里的最小非负正数,即除了 u , v u,v u,v以及他们两个点的连边以外的所有值的最小值
做法:
考虑两种情况
第一:如果每个点的入度都是 < 2 <2 <2,那么这些点肯定都在一条链上,则直接输出 0 0 0 ( n − 2 ) (n-2) (n2)
第二:考虑将 0 , 1 , 2 0,1,2 0,1,2分别放在是度大于 2 2 2的点上,其他的随意输出
第二种解释如下:
如果不是在一条链上,那么则有多个分支的情况,现在假设 5 5 5个分支
在这里插入图片描述
如果现在任意选择两个点,例如 2 , 5 2,5 2,5
会发现不经过的点是 3 , 4 , 6 3, 4,6 3,4,6,显然我们需要在这三边设置最小的数
会发现其实任意选两个点会有 3 − 4 3 - 4 34条边不会被选到
可以发现任意选两个点要使得不会被选到的边尽可能多,那么应该选择两个叶子
并且必然会有另外一条不会被选中,比如三条分叉的情况
在这里插入图片描述
这个时候只要将最小的三个数分别放在三个分支即可
因为之后如果任意选择两个点,必然会有一个分支实在 M E X ( u , v ) MEX(u,v) MEX(u,v)中的


Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;

int ind[maxn];
int uu[maxn];
int vv[maxn];

int main() {
    int n, u, v;
    scanf("%d", &n);
    memset(ind, 0, sizeof(ind));
    for(int i = 1; i < n; ++i) {
        scanf("%d%d", &u, &v);
        ind[u]++, ind[v]++, uu[i] = u, vv[i] = v;
    }
    int id = 0;
    for(int i = 1; i <= n; ++i)
        if(ind[i] > 2)  id = i;
    if(id == 0) {
        for(int i = 0; i < n - 1; ++i)
            printf("%d\n", i);
    }else{
        int id2 = 0, id3 = 3;
        for(int i = 1; i < n; ++i) {
            if(id2 < 3 && (id == uu[i] || id == vv[i]))
                printf("%d\n", id2++);
            else
                printf("%d\n", id3++);
        }
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值