zoukankan      html  css  js  c++  java
  • codefoce 782c

    好久没有更新了,特地来水一水

    简单的涂色问题,大佬说基本都是贪心

    #include<cstdio>
    #include<iostream>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define maxn 200200
    using namespace std;
    int list[maxn];
    vector<int>G[maxn];
    void insert(int be, int en) {
    	G[be].push_back(en);
    }
    int n;
    int cnt = 1;
    int ans = 0;
    int fa[maxn];
    int vis[maxn];
    int bfs() {
    	queue<int>que;
    	que.push(1);
    	vis[1] = 1;
    	list[1] = 1;
    	while (!que.empty()) {
    		int x = que.front();
    		que.pop();
    		cnt = 1;
    		for (int i = 0; i < G[x].size();i++) {
    			int p = G[x][i];
    			if (!vis[p]) {
    				vis[p] = 1;
    				fa[p] = x;
    				if (x == 1) {
    					list[p] = ++cnt;
    					ans = max(ans, cnt);
    				}
    				else {
    					while (1) {
    						if (list[x] == cnt || list[fa[x]] == cnt) cnt++;
    						else break;
    					}
    					ans = max(ans, cnt);
    					list[p] = cnt++;
    				}
    				que.push(p);
    			}
    		}
    	}
    	return 0;
    }
    int main() {
    	int be, en;
    	scanf("%d", &n);
    	for (int i = 1; i < n; i++) {
    		scanf("%d %d", &be, &en);
    		insert(be, en);
    		insert(en, be);
    	}
    	bfs();
    	printf("%d
    ", ans);
    	for (int i = 1; i <= n; i++) {
    		printf("%d ", list[i]);
    	}
    	return 0;
    }
    

      

    寻找真正的热爱
  • 相关阅读:
    TyvjP2018 「Nescafé26」小猫爬山
    简化版桶排序
    getchar吸收回车
    fprintf与fscanf
    c语言命令行参数
    bit、byte、位、字节、汉字的关系
    C语言联合体
    结构体
    关于Integer的parseInt(String s, int radix)方法的使用
    java中nextLine()和next()的区别
  • 原文地址:https://www.cnblogs.com/lesning/p/11838536.html
Copyright © 2011-2022 走看看