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. oracle中rownum的使用

    rownum是系统的一个关键字,表示行号,是系统自动分配的,第一条符合要求的数据行号就是1,第二条符合要求的数据行号就是2. Rownum 不能直接使用 例:取前多少条数据: 取中间的一些数据: se ...

  2. C# 动态添加类、动态添加类型、代码添加类型

    引用控件: DLL下载地址:http://pan.baidu.com/s/1nv2GUWL public class TypeCreator { public static Type Creator( ...

  3. 洛谷P4462 [CQOI2018]异或序列(莫队)

    题意 题目链接 Sol 一开始以为K每次都是给出的想了半天不会做. 然而发现读错题了维护个前缀异或和然后直接莫队搞就行,. #include<bits/stdc++.h> #define ...

  4. LNMP的配置与优化

    一.LNMP的下载 LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat/Fedora.Debian/Ubuntu/Raspbian/Deepin VPS或独立主机安 ...

  5. python 让挑选家具更方便

    原文链接:https://mp.weixin.qq.com/s/tQ6uGBrxSLfJR4kk_GKB1Q 家中想置办些家具,听朋友介绍说苏州蠡(li第二声)口的家具比较出名,因为工作在苏州,也去那 ...

  6. Wu反走样算法绘制直线段

    Wu反走样算法 原理:在我看来,Wu反走样算法是在Bresenham算法基础上改进了一番,它给最靠近理想直线/曲线的两个点以不同的亮度值,以达到模糊锯齿的效果.因为人眼看到的是线附近亮度的平均值. M ...

  7. coTurn测试程序之 turnutils_uclient

    接着对使用coTurn搭建的STUN/TURN服务使用turnutils_uclient程序测试其TURN服务是否正常. 直接连接服务测试服务是否正常.为保证测试使用的服务是TURN服务,在TURN服 ...

  8. Scala抽象类型

    package big.data.analyse.scala import scala.io.{BufferedSource, Source} /** * 抽象类型 * Created by zhen ...

  9. Spring MVC 拦截器 (十)

    完整的项目案例: springmvc.zip 目录 实例 除了依赖spring-webmvc还需要依赖jackson-databind(用于转换json数据格式) <!--json-->& ...

  10. Windows Server 2016-Hyper-V 2016新增功能

    本文解释了Windows Server 2016和Microsoft Hyper-V Server 2016上Hyper-V的新增功能和变更功能. 与Connected Standby兼容(新) 在使 ...