• 【CF1141G】Privatization of Roads in Treeland


    题目大意:给定一个 N 个点的无根树,现给这个树进行染色。定义一个节点是坏点,若满足与该节点相连的至少两条边是相同的颜色,求至多有 k 个坏点的情况下最少需要几种颜色才能进行合法染色。

    题解:考虑一个点不是坏点的情况,必须满足与之相连的每条边颜色均不同,设最多的点的度数为 X,若一个坏点也没有,那么最少肯定需要 X 种颜色,若允许有 K 个坏点,则意味着度数第 K+1 大的节点相连的每条边必须颜色均不同,即:答案为第 K+1 大点的度数。至于染色,满足以上条件的话,随便染色即可。

    代码如下

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+10;
    
    struct node{
    	int nxt,to;
    }e[maxn<<1];
    int tot=1,head[maxn],deg[maxn];
    inline void add_edge(int from,int to){
    	e[++tot]=node{head[from],to},head[from]=tot;
    }
    
    int n,k,ans,cor[maxn];
    
    bool cmp(int x,int y){return x>y;}
    
    void dfs(int u,int fa,int c){
    	for(int i=head[u];i;i=e[i].nxt){
    		int v=e[i].to;if(v==fa)continue;
    		++c;
    		if(c>ans)c-=ans;
    		cor[i>>1]=c;
    		dfs(v,u,c);
    	}
    }
    
    void solve(){
    	scanf("%d%d",&n,&k);
    	for(int i=1,x,y;i<n;i++){
    		scanf("%d%d",&x,&y);
    		add_edge(x,y),add_edge(y,x);
    		++deg[x],++deg[y];
    	}
    	sort(deg+1,deg+n+1,cmp);
    	ans=deg[k+1];
    	dfs(1,0,0);
    	printf("%d
    ",ans);
    	for(int i=1;i<n;i++)printf("%d ",cor[i]);
    }
    
    int main(){
    	solve();
    	return 0;
    }
    
  • 相关阅读:
    十一作业
    11.20
    11.13 第十二次、
    11.13 第十一次、
    11.06第十次、
    11.06第九次、
    10.30
    10.23
    10.16
    10.9
  • 原文地址:https://www.cnblogs.com/wzj-xhjbk/p/10567473.html
Copyright © 2020-2023  润新知