转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1705    Accepted Submission(s): 369
Special Judge

Problem Description
As we all kown, MZL hates the endless loop deeply, and he commands you to solve this problem to end the loop.
You are given an undirected graph with n vertexs and m edges. Please direct all the edges so that for every vertex in the graph the inequation |out degree − in degree|≤1 is satisified.
The graph you are given maybe contains self loops or multiple edges.
 



Input
The first line of the input is a single integer T, indicating the number of testcases.
For each test case, the first line contains two integers n and m.
And the next m lines, each line contains two integers ui and vi, which describe an edge of the graph.
T≤100, 1≤n≤105, 1≤m≤3∗105, ∑n≤2∗105, ∑m≤7∗105.
 



Output
For each test case, if there is no solution, print a single line with −1, otherwise output m lines,.
In ith line contains a integer 1 or 0, 1 for direct the ith edge to ui→vi, 0 for ui←vi.
 



Sample Input
2
3 3
1 2
2 3
3 1
7 6
1 2
1 3
1 4
1 5
1 6
1 7
 



Sample Output
1
1
1
0
1
0
1
0
1
 

题意就是给你一张无向图,让你把它变成有向图,使得对于每一个顶点都满足出度与入度的差的绝对值小于等于一

利用欧拉回路,在欧拉图中,每个点的出度都等于入度,那么对于这个图,其实就相当于若干个欧拉图,然后去掉一些边。

然后我们需要做的就是补边。也就是对于每个奇度点,加一条连到其它奇度点的无向边,然后跑欧拉回路,跑的方向就是这条边的方向。

另外注意有多个连通分支。这题比较容易T,虽然我的队友在比赛时瞬间就AC了。。。然而我还是在赛后T了好久,毕竟队友是final选手

 /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author xyiyy @https://github.com/xyiyy
*/ #include <iostream>
#include <fstream> //#####################
//Author:fraud
//Blog: http://www.cnblogs.com/fraud/
//#####################
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype> using namespace std;
#define rep(X, N) for(int X=0;X<N;X++) const int MAXN = ;
int head[MAXN];
int Next[MAXN], To[MAXN];
int vis[MAXN];
int used[];
int deg[];
int gao;
int tot; void init(int n) {
tot = ;
rep(i, n)head[i] = -;
} void addedge(int u, int v) {
Next[tot] = head[u];
To[tot] = v;
vis[tot] = ;
head[u] = tot++;
} void eular(int u){
used[u] = ;
int i;
while(head[u]!=-){
//for(int &i = head[u];i != -1;i = Next[i]){
i = head[u];
head[u] = Next[head[u]];
if(vis[i])continue;
vis[i^] = ;
eular(To[i]);
}
}
int Scan() {
int res=, ch;
while(ch=getchar(), ch<''||ch>'');
res=ch-'';
while((ch=getchar())>=''&&ch<='')
res=res*+ch-'';
return res;
}
void Out(int a) {
if (a > )
Out(a / );
putchar(a % + '');
} class hdu5348 {
public:
void solve() {
int t;
t =Scan();//in >> t;
while (t--) {
int n, m;
n = Scan();m=Scan();//in >> n >> m;
init(n);
rep(i, n)deg[i] = ;
int u, v;
rep(i, m) {
u = Scan();v= Scan();//in >> u >> v;
u--, v--;
deg[u]++;
deg[v]++;
addedge(u, v);
addedge(v, u);
}
gao = -;
rep(i, n) {
if (deg[i] & ) {
if (gao != -) {
addedge(i, gao);
addedge(gao, i);
gao = -;
} else gao = i;
}
}
rep(i, n) used[i] = ;
/*rep(i,n){
if(!used[i]){
dfs(i);
num++;
}
}*/
gao = -;
rep(i, n) {
if (!used[i]) {
eular(i);
}
}
m<<=;
for(int i=;i<m;i+=){
if (vis[i])putchar('');
else putchar('');
putchar('\n');
} }
}
}; int main() {
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
hdu5348 solver;
//std::istream &in(std::cin);
//std::ostream &out(std::cout);
solver.solve();
return ;
}

hdu5348 MZL's endless loop(欧拉回路)的更多相关文章

  1. 2015多校.MZL's endless loop(欧拉回路的机智应用 || 构造)

    MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  2. [2015hdu多校联赛补题]hdu5348 MZL's endless loop

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1, ...

  3. 2015 Multi-University Training Contest 5 hdu 5348 MZL's endless loop

    MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  4. Hdu 5348 MZL's endless loop (dfs)

    题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...

  5. 图论 HDOJ 5348 MZL's endless loop

    题目传送门 /* 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜, ...

  6. HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)

    题意: 给一个所有你可能想得到的奇葩无向图,要求给每条边定向,使得每个点的入度与出度之差不超过1.输出1表示定向往右,输出0表示定向往左. 思路: 网络流也是可以解决的!!应该挺简单理解的.但是由于复 ...

  7. HDU 5348 MZL's endless loop(DFS去奇数度点+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题目大意:给你一张图,有n个点,和m条无向边,让你把m条无向边变成有向边,使得每个节点的|出度- ...

  8. HDU 5348 MZL's endless loop

    乱搞题...第一直觉是混合图的欧拉通路,但是感觉并没有多大关系.最终AC的做法是不断的寻找欧拉通路,然后给边标号.所有边访问了一遍,所有点访问了一遍,效率是o(n+m).不存在-1的情况. #incl ...

  9. 死循环(endless loop)

    死循环 死循环就是一个无法结束的循环.(endless loop / infinite loop) 出现死循环是因为没有设置好结束条件,循环的结束条件很重要,要充分考虑各种边界情况. 以上一篇随笔中的 ...

随机推荐

  1. Ghost克隆软件

    克隆软件Ghost初级使用教程 一.什么是Ghost ? Ghost(幽灵)软件是美国赛门铁克公司推出的一款出色的硬盘备份还原工具,可以实现FAT16.FAT32.NTFS.OS2等多种硬盘分区格式的 ...

  2. javascript:Array.slice.call 到Array.prototype.slice.call

    举个从对象到数组的例子: var obj={}; obj[1]=1; obj[2]=2; obj.length=2; var arr =Array.prototype.slice.call(obj); ...

  3. maven POM.xml 标签详解

    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以 ...

  4. Lintcode--002(两个字符串是变位词)

    写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 您在真实的面试中是否遇到过这个题?     样例 给出 s = "abcd", ...

  5. Unity GUI TextField不能输入文字

    最近在弄Unity的GUI. 也算是好久不用了,有点不熟悉了. 用TextField的时候发现GUI是出来了不过不能输入文字 到网上查了一下说要用一个public的string来接收 我看了我的代码 ...

  6. Cross-compiling Qt Embedded 5.5 for Raspberry Pi 2

    This tutorial shows how to cross-compile the Embedded build of Qt 5.5 for Raspberry Pi 2. The Embedd ...

  7. 【转】使用 vim + ctags + cscope + taglist 阅读源码

    原文网址:http://my.oschina.net/u/554995/blog/59927 最近,准备跟学长一起往 linux kernel 的门里瞧瞧里面的世界,虽然我们知道门就在那,但我们还得找 ...

  8. Android Content Provider简介

    Content Provider是Android的四大组件之一,与Activity和Service相同,使用之前需要注册: Android系统中存在大量的应用,当不同的应用程序之间需要共享数据时,可以 ...

  9. cf492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  10. HDU 3586 : Information Disturbing

    Problem Description In the battlefield , an effective way to defeat enemies is to break their commun ...