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
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
1 3
3 2
1 4
4 2
2 6
6 3
3 7
4 5
5 6
5 2
Sample Output
1
1
2
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(贪心)的更多相关文章
- 沈阳网络赛F-Fantastic Graph【贪心】or【网络流】
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- UVA 10720 Graph Construction 贪心+优先队列
题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F Fantastic Graph(贪心或有源汇上下界网络流)
https://nanti.jisuanke.com/t/31447 题意 一个二分图,左边N个点,右边M个点,中间K条边,问你是否可以删掉边使得所有点的度数在[L,R]之间 分析 最大流不太会.. ...
- HDU4647:Another Graph Game(贪心)
Problem Description Alice and Bob are playing a game on an undirected graph with n (n is even) nodes ...
- HDU-4647 Another Graph Game 贪心,博弈
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4647 注意这题两人的决策是想要使得自己的分数与对方的差值最大.. 注意到数据范围,显然是贪心之类的,如 ...
- hdoj 5122 K.Bro Sorting 贪心
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- Gym - 100283K K. Cubes Shuffling —— 贪心
题目链接:http://codeforces.com/gym/100283/problem/K 题解: 要使其相邻两项的差值之和最小,那么越靠中间,其数值越小. 那么剩下的问题就是如何放数字了.一开始 ...
- HDU 4647 Another Graph Game(贪心)
题目链接 思路题.看的题解. #include <cstdio> #include <string> #include <cstring> #include < ...
- 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS
原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
随机推荐
- openjudge------ 日期的种类题目
描述TXT is a vegetable chicken,so 出题什么的完全不会啊! 干脆直接从网络上copy一题下来吧. 小明正在整理一批历史文献.这些历史文献中出现了很多日期.小明知道这些日期都 ...
- 【Java每日一题】20170228
20170227问题解析请点击今日问题下方的“[Java每日一题]20170228”查看(问题解析在公众号首发,公众号ID:weknow619) package Feb2017; import jav ...
- linux中make的用法
一.linux中make的用法 目的: 基本掌握了make 的用法,能在Linux系统上编程.环境: Linux系统准备: 准备三个文件:file1.c, file ...
- class example of C++
#include <iostream> using namespace std; class Rectangle { int width, height; public: ...
- 腾讯云下的CentOS7 配置 Apache服务器
第一步 :安装Apache服务程序(软件包名为httpd) * yum install httpd 第二步:配置httpd.conf文件 * vi /etc/httpd/conf/httpd.conf ...
- Java岗 面试考点精讲(网络篇03期)
1. OSI七层模型 总结一下: 应用用层按协议打包数据 由传输层加上双方的端口号 由网络层加上双方的IP地址 由链路层加上双方的MAC地址,并将数据拆分成数据帧 数模信号转换并由物理层传输到另一端 ...
- JavaScript定时器实现的原理分析
原文链接:http://www.cnblogs.com/st-leslie/p/6082450.html 一.储备知识 在我们在项目中一般会遇见过这样的两种定时器,第一种是setTimeOut,第二种 ...
- ES6中字符串扩展
ES6中字符串扩展 ① for...of 遍历字符串: 例如: for(let codePoint of 'string'){ console.log(codePoint) } 运行结果: ② in ...
- dpr,ppi,dip,viewport的一些概念
一 ppi,dpr,dip,分辨率,屏幕尺寸,设备物理像素,设备独立像素 分辨率:1920px*1080px 在这个设备上纵向上有 1920个像素点(色块)... 屏幕尺寸:5英寸(in) = 5*2 ...
- win10怎么录制电脑屏幕 电脑播放视频录制
随着社会的发展,网络信息化时代已经来临,作为一个上班族,每天都离不开电脑,电脑仿佛就是我们的合作伙伴,也是陪伴我们的朋友,如今win10系统已经出来了,关于win10系统的问题相信大家有很多的问题,今 ...