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跑一遍染色即可
代码语言:javascript复制// 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;
}