Problem UVA1613-K-Graph Oddity

Accept: 108  Submit: 884
Time Limit: 3000 mSec

Problem Description

Input

The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.
The first line of the input file contains two integer numbers n and m, where n is the number of vertices in the graph (3 ≤ n ≤ 9999, n is odd), m is the number of edges in the graph (2 ≤ m ≤ 100000). The following m lines describe edges of the graph, each edge is described by two integers ai, bi (1 ≤ ai,bi ≤ n,ai = bi) — the vertex numbers connected by this edge. Each edge is listed at most once. The graph in the input file is connected, so there is a path between any pair of vertices.

 Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
On the first line of the output file write a single integer number k — the minimal odd integer number, such that the degree of any vertex does not exceed k. Then write n lines with one integer number ci (1 ≤ ci ≤ k) on a line that denotes the color of i-th vertex. The colors of any two adjacent vertices must be different. If the graph has multiple different colorings, print any of them. At least one such coloring always exists.
 

 Sample Input

3 2
1 3
3 2
7 8
1 4
4 2
2 6
6 3
3 7
4 5
5 6
5 2
 

Sample Output

3
1
1
2
 
3
1
1
1
2
3
2
2
 
题解:这个题过得比较意外,一开始想从度数大的开始涂,弄一个优先队列,画了几个图,发现好像不管怎么涂都对,就写了一个然后过了,为什么是对的真的说不好,欢迎大佬指教。
 
 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + , maxm =  + ;

 int n, m, k;

 struct Edge {
int to, next;
}edge[maxm << ]; int tot, head[maxn];
int col[maxn], deg[maxn];
bool vis[maxn]; void init() {
tot = ;
memset(head, -, sizeof(head));
memset(deg, , sizeof(deg));
memset(col, -, sizeof(col));
} void AddEdge(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void dfs(int u) {
memset(vis, false, sizeof(vis));
//printf("%d %d\n", fa, u);
for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (col[v] != -) vis[col[v]] = true;
} for (int i = ; i <= k; i++) {
if (!vis[i]) {
col[u] = i;
break;
}
} for (int i = head[u]; i != -; i = edge[i].next) {
int v = edge[i].to;
if (col[v] == -) {
dfs(v);
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
bool flag = false;
while (~scanf("%d%d", &n, &m)) {
if(flag) printf("\n");
flag = true;
init();
int x, y;
for (int i = ; i < m; i++) {
scanf("%d%d", &x, &y);
deg[x]++, deg[y]++;
AddEdge(x, y);
AddEdge(y, x);
} int Max = ;
for (int i = ; i <= n; i++) {
Max = Max > deg[i] ? Max : deg[i];
} k = (Max & ) ? Max : Max + ;
dfs();
printf("%d\n", k);
for (int i = ; i <= n; i++) {
printf("%d\n", col[i]);
}
}
return ;
}

UVA1613-K-Graph Oddity(贪心)的更多相关文章

  1. 沈阳网络赛F-Fantastic Graph【贪心】or【网络流】

    "Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...

  2. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)

    https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...

  4. HDU4647:Another Graph Game(贪心)

    Problem Description Alice and Bob are playing a game on an undirected graph with n (n is even) nodes ...

  5. HDU-4647 Another Graph Game 贪心,博弈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4647 注意这题两人的决策是想要使得自己的分数与对方的差值最大.. 注意到数据范围,显然是贪心之类的,如 ...

  6. hdoj 5122 K.Bro Sorting 贪心

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...

  7. Gym - 100283K K. Cubes Shuffling —— 贪心

    题目链接:http://codeforces.com/gym/100283/problem/K 题解: 要使其相邻两项的差值之和最小,那么越靠中间,其数值越小. 那么剩下的问题就是如何放数字了.一开始 ...

  8. HDU 4647 Another Graph Game(贪心)

    题目链接 思路题.看的题解. #include <cstdio> #include <string> #include <cstring> #include < ...

  9. 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS

    原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...

  10. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

随机推荐

  1. spring-framework-中文文档二:Bean概述

    Spring IoC容器管理一个或多个bean.这些bean是使用您提供给容器的配置元数据创建的,例如,以XML <bean/>定义的形式 . 在容器本身中,这些bean定义被表示为 Be ...

  2. 一位ML工程师构建深度神经网络的实用技巧

    一位ML工程师构建深度神经网络的实用技巧 https://mp.weixin.qq.com/s/2gKYtona0Z6szsjaj8c9Vg 作者| Matt H/Daniel R 译者| 婉清 编辑 ...

  3. 申请Office 365一年免费的开发者账号攻略(2018年10月份版本)

    要进行Office 365开发,当然需要有完整的Office 365环境才可以.为了便于广大开发人员快速地启动这项工作,微软官方给所有开发人员提供了免费的一年开发者账号   那么如何申请Office ...

  4. 【vue】使用vue构建多页面应用

    先了解一些单页面和多页面的区别 mm 多页应用模式MPA 单页应用模式SPA 应用构成 由多个完整页面构成 一个外壳页面和多个页面片段构成 跳转方式 页面之间的跳转是从一个页面跳转到另一个页面 页面片 ...

  5. Ubuntu-Tweak 安装

    Ubuntu Tweak 是中国人开发的一款专门为Ubuntu准备的配置.调整工具,类似与compiz,界面更友好.   下面是安装命令:   第一步:添加tweak源 sudo add-apt-re ...

  6. Android Dialog对话框

    Dialog的基本方法 //创建Dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设 ...

  7. (后端)JackSon将java对象转换为JSON字符串(转)

    转载小金金金丶园友: JackSon可以将java对象转换为JSON字符串,步骤如下: 1.导入JackSon 的jar包 2.创建ObjectMapper对象 3.使用ObjectMapper对象的 ...

  8. mysql学习之完整的select语句

    本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order by limit 首发日期:2018-04-11 完整语法: 先给一下完整的语法,后面将逐一来 ...

  9. Apache Windows下Apache安装步骤

    1.apache官网下载Apache HTTP Server服务器 我相信有些朋友刚用apache服务器时,都希望从官网上下载,而面对着官网上众多的项目和镜像以及目录,也许有点茫然.下面是具体步骤: ...

  10. (python)排序算法

    一.冒泡排序 1.冒泡排序实现思路 需要两层循环,外层循环控制总共循环几次,内层循环控制交换的次数(注意索引超界的问题). 外层第一次循环,内层第一次循环,将第一个元素(y)与第二个元素(y+1)进行 ...