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

题目链接:

http://codeforces.com/contest/1141/problem/G

题意:

给有n个节点的树的n-1条边染色,这样的节点数不能超过k个,即一个节点的某两条或更多边被染同一种颜色,输出最少需要染色的颜色数,并输出对应某条边,所染的颜色编号;

分析:

简单题
看了别人代码,然后发现,所需染最少颜色的颜色数,就是点度数为,第k+1大的度数(即点的出度与入度和),然后进行dfs就行了;自己就没发现;
相比之下自己写的就很烂了;

代码:

红名大佬代码:

#include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;
#define x first
#define y second

const int N = 200005;

int n, k, d[N], r[N];
vector<pii> e[N];

void f(int x, int y, int t, int z){
	int c = 0;
	for(pii i : e[x]){
		if(i.x == y) continue;
		c++;
		if(c == z) c++;
		c = min(c, t);
		r[i.y] = c;
		f(i.x, x, t, c);
	}
}

int main(){
	scanf("%d%d", &n, &k);
	for(int i = 0, x, y; i < n - 1; i++){
		scanf("%d%d", &x, &y);
		d[x]++; d[y]++;
		e[x].emplace_back(y, i);
		e[y].emplace_back(x, i);
	}
	sort(d + 1, d + n + 1);
	reverse(d + 1, d + n + 1);
	int t = d[k + 1];
	printf("%d\n", t);
	f(1, 0, t, 0);
	for(int i = 0; i < n - 1; i++) printf("%d ", r[i]);
	puts("");
}

本菜鸡写的:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
using namespace std;

const int inf=0x7f7f7f7f;
const int maxn=1e1+50;
const int N=4e5+50;
typedef long long ll;
typedef struct{
    ll u,v,next,w;
}Edge;
Edge e[N];
typedef struct{
    int out,pos;
}Point;
Point p[N];
int cnt,head[N];

inline void add(int u,int v){
    e[cnt].u=u;
    e[cnt].v=v;
    //e[cnt].w=w;
    // e[cnt].f=f;
    e[cnt].next=head[u];
    head[u]=cnt++;
    e[cnt].u=v;
    e[cnt].v=u;
    // e[cnt].w=0;
    // e[cnt].f=-f;
    e[cnt].next=head[v];
    head[v]=cnt++;
}
inline void write(int x)
{
     if(x<0) 
        putchar('-'),x=-x;
     if(x>9)
        write(x/10);
     putchar(x%10+'0');
}
inline int read()
{
    int x = 0;
    int f = 1;
    char c = getchar();
    while (c<'0' || c>'9'){    
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0'&&c <= '9'){
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x*f;
}

int n,k,mark[N],t=1,color[N],q=1,maxclo;

bool com(Point x,Point y){
    return x.out>y.out;
}

void dfs(int x,int f,int c){
    int clo=0;
    for(int i=head[x];i!=-1;i=e[i].next){
        int v=e[i].v;
        if(v!=f){
            if(x==1){
                if(mark[x]){
                    color[i]=color[i^1]=clo=1;
                }
                else
                    color[i]=color[i^1]=++clo;
            }
            else {
                if(mark[x])
                    color[i]=color[i^1]=c,clo=c;
                else
                    color[i]=color[i^1]=(clo+1==c?clo+=2:++clo);
            }
            dfs(v,x,clo);
        }
    }
    maxclo=max(maxclo,clo);
}
int main() {
    cin>>n>>k;
    int u,v;
    memset(head,-1,sizeof(head));
    for(int i=0;i<n-1;i++){
        scanf("%d%d",&u,&v);
        add(u,v);
        p[u].pos=u,p[u].out++;
        p[v].pos=v,p[v].out++;
    }
    sort(p+1,p+n+1,com);
    for(int i=1;i<=k;i++){
        mark[p[i].pos]=1;
    }
    dfs(1,0,1);
    cout<<maxclo<<endl;
    for(int i=0;i<cnt;i+=2){
        //cout<<e[i].u<<" "<<e[i].v<<" ";
        cout<<color[i]<<" ";
    }
    return 0;
}

(仅供个人理解)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值