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) ...
随机推荐
- Python3 系列之 面向对象篇
面向对象的三大特性:继承.封装和多态 Python 做到胶水语言,当然也支持面向对象的编程模式. 封装 class UserInfo(object): lv = 5 def __init__(self ...
- 借鉴Glide思想二次封装Fresco
本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 最近封装了个 Fresco 的组件库:DFresco,就顺便来讲讲. 背景 Fresco 图片库很强大,我们项目中就是使用的 Fre ...
- OpenVDB for Mitsuba
https://github.com/zhoub/mitsuba-vdb
- ionic 兼容title居中显示和tab栏在显示底部
在app.js里的 .config 里添加配置,同时在函数中引入 $ionicConfigProvider,具体格式如下所示: .config(function($stateProvider, $ur ...
- 关于bug的一些思考
上午看了两道算法,自己编译器上面敲了一遍,然后又去网站上敲了一遍: 编译器上面无论哦如何都调不出来,网站上面也是: 吃个午饭,睡个觉,醒来重新手撸了一遍,然后就过了 : 面对这种事情,真的是自己应该多 ...
- Vue组件的使用
前面的话 组件(component)是Vue最强大的功能之一.组件可以扩展HTML元素,封装可重用的代码,根据项目需求,抽象出一些组件,每个组件里包含了展现.功能和样式.每个页面,根据自己的需要,使用 ...
- php 接口与前端数据交互实现
最近在做前后端数据交互的尝试,也跳了很多坑,使用的是php+bootstrap-table+js,把一些收获记录在这里,方便查询. 这个小项目,仅有3个文件,分别为: crud.html data.p ...
- MySQL 基本语句(2)
1.创建数据库 :create database 名称 [charset 字符集 collate 校对规则] ; 如: drop database if exists `mydb` ; # 若存在就 ...
- spark大批量读取Hbase时出现java.lang.OutOfMemoryError: unable to create new native thread
这个问题我去网上搜索了一下,发现了很多的解决方案都是增加的nproc数量,即用户最大线程数的数量,但我修改了并没有解决问题,最终是通过修改hadoop集群的最大线程数解决问题的. 并且网络上的回答多数 ...
- 【redis专题(7)】命令语法介绍之Pub/Sub
Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息.主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似.p ...