cf: Ehab and Path-etic MEXs

题目大意

给定一个连通图,要求你给他们的边赋值,所有边的MEX(u, v)的最大值最小
me(u, v)是u->v的边上的没有出现过的最小整数

样例

inputCopy
6
1 2
1 3
2 4
2 5
5 6
outputCopy
0
3
2
4
1
 MEX(1, 6)=2; MEX(1, 3)=0; MEX(1, 4) = 1.

思路

先将叶子结点的边从依次赋值,然后,再赋值不是叶子结点的边
这样就可以满足任意两条边都存在不大于总边数的权值,
在极限情况下,当图是一条线的时候,最大值mex是边数加一

代码

#include <iostream>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 200010;

int deg[N], a[N], b[N];
int n, m;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i < n; i++)
    {
        scanf("%d%d", &a[i], &b[i]);
        deg[a[i]]++;
        deg[b[i]]++;
    }

    int cnt = 0;
    for (int i = 1; i <= n; i++)
        if (deg[a[i]] == 1 || deg[b[i]] == 1)
            cnt++;

    int c = 0;
    for(int i = 1; i < n; i++)
    {
        if(deg[a[i]] == 1 || deg[b[i]] == 1)
            printf("%d\n", c++);
        else printf("%d\n", cnt++);
    }
    return 0;
}
发布了118 篇原创文章 · 获赞 221 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_45432665/article/details/104875703
今日推荐