Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G
题意
在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来染一棵树
题解
- 选择弃掉度数最高的k个点,然后第k+1个点的度数就是答案
代码
#include<bits/stdc++.h>
#define N 200005
#define pb push_back
using namespace std;
int n,k,u,v,in[N],c[N],m,i;
struct node{
int e,v;
};
vector<node>g[N];
bool cmp(int x,int y){
return x>y;
}
void dfs(int u,int fa,int cl){
for(int i=0;i<g[u].size();i++){
int v=g[u][i].v,id=g[u][i].e;if(v==fa)continue;
if(cl>m)cl-=m;
c[id]=cl++;
dfs(v,u,cl);
}
}
int main(){
cin>>n>>k;
for(i=1;i<=n-1;i++){
scanf("%d%d",&u,&v);
in[u]++;in[v]++;
g[u].pb({i,v});
g[v].pb({i,u});
}
sort(in+1,in+n+1,cmp);
m=in[k+1];
dfs(1,0,1);
printf("%d\n",m);
for(i=1;i<=n-1;i++){
printf("%d ",c[i]);
}
}
Codeforces Round #547 (Div. 3) G 贪心的更多相关文章
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #481 (Div. 3) G. Petya's Exams
http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...
- Codeforces Round #346 (Div. 2) G. Fence Divercity dp
G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...
- Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)
题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...
- Codeforces Round #570 (Div. 3) G. Candy Box (hard version) (贪心,优先队列)
题意:你有\(n\)个礼物,礼物有自己的种类,你想将它们按种类打包送人,但是打包的礼物数量必须不同(数量,与种类无关),同时,有些礼物你想自己留着,\(0\)表示你不想送人,问你在送出的礼物数量最大的 ...
随机推荐
- java中拼接两个对象集合
目标: 根据两个list中每条记录的某个属性是否相同来拼接. 1.首先定义一个字符串 String str = "[{\"ITEMID\":2,\"ITEMN ...
- .net 调用 Matlab生成dll出现的问题(The type initializer for 'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception.)
https://cn.mathworks.com/matlabcentral/answers/278399-i-get-an-error-saying-the-type-initializer-for ...
- mybatis的typeHandler
typeHandler作用: 1.传参时将javaType类型转换成jdbcType 2.结果集中ResultSet中取值时,jdbcType转换为javaType; 系统自定义的typeHandle ...
- ffmpeg 编译
下载FFmpeg git clone https://git.ffmpeg.org/ffmpeg.git 配置编译FFmpeg ./configure --prefix=host --enable-s ...
- 【Linux命令】Linux下的tar压缩解压缩命令详解(转)
tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个 ...
- 在BootStrap的modal中使用Select2搜索框无法输入
用modal来show一个对话框 dialog.modal({ backdrop:true, keyboard:true, show:true }); 1 2 3 4 5 然后再modal中初始化se ...
- 第三方jar上传到Maven私服(Nexus)
mvn deploy:deploy-file -DgroupId=taobao-sdk -DartifactId=taobao-sdk-java -Dversion=1.0 -Dpackaging=j ...
- ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类
ArcGIS自定义脚本-通过txt/excel/dbf/table生成多边形要素类 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:读取文本文件,常见多边形要素 ...
- Mybatis控制台打印sql
mybatis-config.xml配置如下: <configuration> <settings> <setting name="lazyLoadingEna ...
- python,关于用户登录与注册问题
tag=Truecount=1while tag: name = input('请输入用户名>>:').strip() password = input('请输入密码>>:') ...