【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\n",ans);
for(int i=1;i<n;i++)printf("%d ",cor[i]);
}
int main(){
solve();
return 0;
}
【CF1141G】Privatization of Roads in Treeland的更多相关文章
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
- 【HDOJ】2988 Dark roads
最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...
- codeforces 1141G Privatization of Roads in Treeland
题目链接:http://codeforces.com/contest/1141/problem/G 题目大意: 给你一个无向连通图.每条边都有颜色,如果存在一个点的临边中有超过两条边颜色相同,这个点就 ...
- LCA+差分【CF191C】Fools and Roads
Description 有一颗 \(n\) 个节点的树,\(k\) 次旅行,问每一条边被走过的次数. Input 第一行一个整数 \(n\) (\(2\leq n\leq 10^5\)). 接下来 \ ...
- 【kruscal】【最小生成树】poj2421 Constructing Roads
SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...
- 【POJ】1251 Jungle Roads
题目链接:http://poj.org/problem?id=1251 题意:n个村庄字母标号,每个字母后跟m个字母,表示该字母到mi的距离.求构建所有村庄道路的最短距离. 题解:最小生成树裸题.注意 ...
- 【 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 ...
- 【CF671D】Roads in Yusland(贪心,左偏树)
[CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...
- 【CF617D】Roads in Yusland
[CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...
随机推荐
- js发布订阅模式实现
//可以用于无相关页面或组件的事件.数据传递,减少在onShow中的业务,降低代码耦合 let events = {} /**订阅**/ function on(name, self, callbac ...
- hive排序
1.升序排序 hive > select id,name,sal from emp order by sal; 2.降序 添加关键字desc hive > select id,nam ...
- Laravel 出现 No application encryption key has been specified.
若文件根目录下没有 .env 1..env.example 改名使用命令 copy 修改为 .env 2.使用命令 php artisan key:generate 获取密码,自动保存到 .env3 ...
- python数据结构与算法第三天【时间复杂度计算方法】
最优时间复杂度(不可靠) 最坏时间复杂度(保证) 平均时间复杂度(平均状况) 不同语句的时间复杂度: (1)顺序语句:使用加法 (2)循环语句:使用乘法 (3)分支语句:使用坏时间复杂度 例如:如下代 ...
- loadrunner -vuser
在每个负载生成器上,安装 remote agent dispatcher(process) 和 loadrunner agent 控制器指示remote agent dispatcher 在load ...
- Civil 3D 2017本地化中VBA程序移植到2018版中
中国本地化包简直就是一块鸡肋, 但对于某些朋友来说还真离不了: 可惜中国本地化包的推出一直滞后, 在最新版软件出来后1年多, 本地化还不一定能够出来, 即使出来了, 也只能是购买了速博服务的用户才能得 ...
- C# 调用短信接口
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- springmvc使用swagger生成rest api文档
pom.xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
- BZOJ5419[Noi2018]情报中心——线段树合并+虚树+树形DP
题目链接: [NOI2018]情报中心 题目大意:给出一棵n个节点的树,边有非负边权,并给出m条链,对于每条链有一个代价,要求选出两条有公共边的链使两条链的并的边权和-两条链的代价和最大. 花了一天的 ...
- Mail.Ru Cup 2018 Round 2
A:阅读理解. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...