codeforces 1141 G. Privatization of Roads in Treeland

G

题意 给你一颗树 定义不满足的点就是他连接的所有边里面出现了相同的颜色 问你用颜色去染色 使得不满足的点数量不超过k

做法 我们知道既然你最多只能k个不满足点 那么我们贪心按照度数从大到小排序 另颜色数等于 k+1点的度数 就满足后面所有点都合法 前面所有点都是不满足点 贪心的去染色即可解决这道题

/*
        If you can't see the repay
        Why not just working step by step?
        to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
//inv[1]=1;
//for(int i=2; i<M; ++i) inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
//for(int i=0; i<LIM; ++i) coef[i]=1ll*(P[i]-1)*inv[P[i]]%mod;
const int MAX_N = 200025;
int col[MAX_N],ans[MAX_N],arr[MAX_N],m;
vector<int > G[MAX_N],id[MAX_N];
struct node
{
    int u,fa;
    node(){}
    node(int uu,int faa){
        u = uu;
        fa = faa;
    }
};
bool cmp(int a,int b)
{
    return a > b;
}
void bfs()
{
    queue<node> q;
    q.push(node(1,0));
    while(!q.empty())
    {
        node now = q.front();
        q.pop();
        int cnt = col[now.u];
        int sz = G[now.u].size();
        for(int i = 0;i<sz;++i)
        {
            int to = G[now.u][i];
            if(to==now.fa) continue;
            cnt++;
            if(cnt>m) cnt-=m;
            col[to] = cnt;
            ans[id[now.u][i]] = cnt;
            q.push(node(to,now.u));
        }
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,k,a,b;
    scanf("%d%d",&n,&k);
    for(int i = 1;i<n;++i)
    {
        scanf("%d%d",&a,&b);
        G[a].push_back(b);
        id[a].push_back(i);
        arr[a]++;
        G[b].push_back(a);
        id[b].push_back(i);
        arr[b]++;
    }
    sort(arr+1,arr+1+n,cmp);
    m = arr[k+1];

    bfs();
    printf("%d\n",m);
    for(int i = 1;i<n;++i)
        i==n?printf("%d\n",ans[i]):printf("%d ",ans[i]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/heucodesong/article/details/88690852
今日推荐