前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland

Codeforces Round #547 (Div. 3)G. Privatization of Roads in Treeland

作者头像
glm233
发布2020-09-28 10:40:33
3350
发布2020-09-28 10:40:33
举报
文章被收录于专栏:glm的全栈学习之路

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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档