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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章