转载请注明出处: 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. shell中的declare命令

    declare命令有如下选项: -a 声明一个数组 -i 声明一个整型 -f 打印所有函数定义 -F 仅打印函数名字 -r 声明一个readonly变量,该变量的值无法改变,并且不能为unset -x ...

  2. Kendo Web UI Grid添加一个html控件如(checkbox,button)

    在Kendo Web UI Grid增加一个控件如效果图: <div id="grid1"></div><script> $("#gr ...

  3. CREATE DATABASE

    CREATE DATABASE IF NOT EXISTS `focusdata` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `fo ...

  4. controller 和 指令 通讯方法

    在 angular 中我们经常会使用多个 controller 和 指令 他们拥有各自的 $scope , 这就产生了跨$scope调用的问题. 有几种常见的方法来可以使用. 方法一 : 指令 req ...

  5. Oracle AWR

    http://www.linuxidc.com/Linux/2011-10/44563.htm http://t.askmaclean.com/thread-3227-1-1.html http:// ...

  6. Altium Designer (protel) 各版本“故障”随谈

    Altium 的版本很多,每个版本都或多或少有些可容忍或可不容忍的问题,此贴只是希望各位能将遇到的问题写出来,只是希望 给还在使用 altium 的网友一些参考,也希望有些能被 altium 所接受@ ...

  7. MySQL所有函数及操作符

    参考:Function and Operator Reference Name Description ABS() Return the absolute value ACOS() Return th ...

  8. 转:Android模拟器连接电脑网络

    原文地址:http://www.it165.net/pro/html/201212/4444.html 第一步: 在命令行(就是开始——运行——输入cmd)模式下输入adb shell命令一般会报两种 ...

  9. SQL 连接 JOIN 例解。(左连接,右连接,全连接,内连接,交叉连接,自连接)

    SQL 连接 JOIN 例解.(左连接,右连接,全连接,内连接,交叉连接,自连接) 最近公司在招人,同事问了几个自认为数据库可以的应聘者关于库连接的问题,回答不尽理想-现在在这写写关于它们的作用假设有 ...

  10. Bring it on

    I am going to open a whole new English Blog here. Most blogs here would be computer technologies, in ...