Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland
G. Privatization of Roads in Treeland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Treeland consists of n cities and n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed kk and the number of companies taking part in the privatization is minimal.
Choose the number of companies r such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most k. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal r that there is such assignment to companies from 1 to r that the number of cities which are not good doesn't exceed k.
题意:用最少的颜色绘制无向图的边,这样,不合适的顶点的数量不会超过k。如果一个顶点至少有两条相同颜色的边,那么这个顶点就是不合适的。
思路:找到这样一个最小度使得大于这个度的点数不超过k,这个度就是答案,最后dfs跑一遍染色即可
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
vector<vector<pair<ll,ll>>>p;
ll D;
vector<ll>ans;
inline void dfs(ll now,ll last,ll lastcol)
{
ll color=1;
//cout<<1<<endl;
for(auto it:p[now])
{
//cout<<it.first<<" "<<it.second<<endl;
if(it.first!=last)
{
if(color==lastcol)
{
color+=1;
color=(color-1)%D+1;
//lastcol=-1;
}
ans[it.second]=color;
//cout<<"a"<<endl;
dfs(it.first,now,color);
color+=1;
color=(color-1)%D+1;
}
}
}
int main()
{
ll n=read(),k=read();
p.resize(n+5);
vector<ll>d(n+5);
for(rg i=1;i<n;i++)
{
ll x=read(),y=read();
p[x].push_back(make_pair(y,i));
p[y].push_back(make_pair(x,i));
d[x]++,d[y]++;
}
//cout<<1<<endl;
map<ll,ll>cnt;
for(ll it:d)
{
if(it)
cnt[it]++;
//cout<<it<<endl;
}
D=0;
ll temp=n;
for(auto it:cnt)
{
if(temp>k)
{
temp-=it.second;
D=it.first;
}else break;
}
cout<<D<<endl;
ans.resize(n+5);
dfs(1,-1,-1);
for(rg i=1;i<n;i++)
{
i==n-1?cout<<ans[i]:cout<<ans[i]<<" ";
}
return 0;
}
- pymongo.errors:Sort operation used more than the maximum 33554432 bytes of RAM. Add an index,
- PG学习初体验--源码安装和简单命令(r8笔记第97天)
- Pymongo: TypeError: if no direction is specified, key_or_list must be an instance of list
- Java面试系列19-Struts2
- JS游戏开发,让你的静态人物动起来(来自网路)
- Golang语言websocket源码
- 社团划分——Fast Unfolding算法
- 【PSU】AIX 11g RAC自动打GI PSU5
- 详解自动识别验证码,LSTM大显身手
- 智能算法——PageRank
- 【Go 语言社区】Golang 可变参数的使用
- 【Go 语言社区】Golang源码解读之map
- 一则报警信息所折射出来的诸多问题(r9笔记第14天)
- Java面试系列17-编程题-读取服务器字符、实现序列化、计数器、1000阶乘、n出列问题等
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 浅谈Android Studio 3.0 的一些小变化
- 详解如何使用Android Studio开发Gradle插件
- Android ItemDecoration 实现分组索引列表的示例代码
- Android TextView实现词组高亮的示例代码
- Android开发使用json实现服务器与客户端数据的交互功能示例
- Android中实现词组高亮TextView方法示例
- AsyncTask类实例详解
- Android中ListView的item点击没有反应的解决方法
- android 应用内部悬浮可拖动按钮简单实现代码
- 详解Android PopupWindow怎么合理控制弹出位置(showAtLocation)
- Android Studio 3.0上分析内存泄漏的原因
- Android 实现图片生成卷角和圆角缩略图的方法
- Android使用TextInputLayout创建登陆页面
- Android使用WebSocket实现多人游戏
- Android Studio多渠道打包套路