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. springMVC_11拦截器实现登录

    一.   思路 controller实现核对用户名和密码,如果核对正确则保存到session中并且跳转到主页 系统中包含诸多界面,部分界面不需要登录即可进行访问,通过拦截器实现判断是否是不需要登录的界 ...

  2. 【Java每日一题】20170313

    20170310问题解析请点击今日问题下方的“[Java每日一题]20170313”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; import jav ...

  3. Java学习笔记之——封装

    1. 属性和方法放到类中 2. 信息的隐藏 (1) 属性的隐藏 (2) 方法实现的细节隐藏 3. 权限修饰符: 从小到大的顺序:private->默认的(什么都不写)->protected ...

  4. Spring Data REST API集成Springfox、Swagger

    原文: Documenting a Spring Data REST API with Springfox and Swagger 使用Spring Date REST,你可以迅速为Spring Da ...

  5. 剑指offer:1.找出数组中重复的数(java版)

    数组中重复的数:题目:找出数组中重复的数,题目描述:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任 ...

  6. SQL 读取XML到Datatable

    DECLARE @hdoc INT --XML 数据格式 --------------------------------------------------------- ) SET @doc = ...

  7. canvas-6shadow.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS之函数实际参数转换成数组的方法[].slice.call(arguments)

    实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通 ...

  9. POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 13954   Accepted: 4673 Description Bein ...

  10. Android gravity和layout_gravity的区别

    一.gravity和layout_gravity相同处 两者都是设置对齐方式的属性.内部的属性值相同. 根据英文意思也能理解其中的意思.如center_horizontal表示在水平方向上的位置为中间 ...