题目大意:给定一个 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\n",ans);
for(int i=1;i<n;i++)printf("%d ",cor[i]);
} int main(){
solve();
return 0;
}

【CF1141G】Privatization of Roads in Treeland的更多相关文章

  1. 【CodeForces】671 D. Roads in Yusland

    [题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...

  2. 【HDOJ】2988 Dark roads

    最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...

  3. codeforces 1141G Privatization of Roads in Treeland

    题目链接:http://codeforces.com/contest/1141/problem/G 题目大意: 给你一个无向连通图.每条边都有颜色,如果存在一个点的临边中有超过两条边颜色相同,这个点就 ...

  4. LCA+差分【CF191C】Fools and Roads

    Description 有一颗 \(n\) 个节点的树,\(k\) 次旅行,问每一条边被走过的次数. Input 第一行一个整数 \(n\) (\(2\leq n\leq 10^5\)). 接下来 \ ...

  5. 【kruscal】【最小生成树】poj2421 Constructing Roads

    SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...

  6. 【POJ】1251 Jungle Roads

    题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意 ...

  7. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

  8. 【CF671D】Roads in Yusland(贪心,左偏树)

    [CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...

  9. 【CF617D】Roads in Yusland

    [CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...

随机推荐

  1. java之序列化

    详细内容 连接https://blog.csdn.net/qq_27093465/article/details/78544505 Java 之 Serializable 序列化和反序列化的概念,作用 ...

  2. Entity Framework 6 自定义连接字符串ConnectionString连接MySQL

    在开始介绍之前,首先来看看官方对Entity Framework的解释:Entity Framework (EF) is an object-relational mapper that enable ...

  3. windows 10 & 禁用服务.bat

    windows 10 & 禁用服务.bat 禁用服务.bat @echo off net stop WSearch net stop wuauserv net start TrustedIns ...

  4. vue-cli(vue脚手架)

    vue-cli用于自动生成vue+webpack项目. 安装webpack:npm install webpack -g 检查webpack是否安装成功和版本:webpack -v 如果是webpac ...

  5. 三、zookeeper安装

    一.简介 二.下载解压: #wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar ...

  6. vue自定义组件及传值

    1.使用 Vue.component() 方法注册组件 2.使用 props 属性传递参数 v-for="item in items": 遍历 Vue 实例中定义的名为 items ...

  7. java JSP自定义标签

    来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...

  8. Sql Server 时间格式化

    0   或   100   (*)     默认值   mon   dd   yyyy   hh:miAM(或   PM)       1   101   美国   mm/dd/yyyy       ...

  9. Spring Boot 构建电商基础秒杀项目 (七) 自动校验

    SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...

  10. [离散时间信号处理学习笔记] 10. z变换与LTI系统

    我们前面讨论了z变换,其实也是为了利用z变换分析LTI系统. 利用z变换得到LTI系统的单位脉冲响应 对于用差分方程描述的LTI系统而言,z变换将十分有用.有如下形式的差分方程: $\displays ...