转载请注明出处: 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选手

  1 /**
  2  * code generated by JHelper
  3  * More info: https://github.com/AlexeyDmitriev/JHelper
  4  * @author xyiyy @https://github.com/xyiyy
  5  */
  6
  7 #include <iostream>
  8 #include <fstream>
  9
 10 //#####################
 11 //Author:fraud
 12 //Blog: http://www.cnblogs.com/fraud/
 13 //#####################
 14 #pragma comment(linker, "/STACK:102400000,102400000")
 15 #include <iostream>
 16 #include <sstream>
 17 #include <ios>
 18 #include <iomanip>
 19 #include <functional>
 20 #include <algorithm>
 21 #include <vector>
 22 #include <string>
 23 #include <list>
 24 #include <queue>
 25 #include <deque>
 26 #include <stack>
 27 #include <set>
 28 #include <map>
 29 #include <cstdio>
 30 #include <cstdlib>
 31 #include <cmath>
 32 #include <cstring>
 33 #include <climits>
 34 #include <cctype>
 35
 36 using namespace std;
 37 #define rep(X, N) for(int X=0;X<N;X++)
 38
 39 const int MAXN = 800010;
 40 int head[MAXN];
 41 int Next[MAXN], To[MAXN];
 42 int vis[MAXN];
 43 int used[100010];
 44 int deg[100010];
 45 int gao;
 46 int tot;
 47
 48 void init(int n) {
 49     tot = 0;
 50     rep(i, n)head[i] = -1;
 51 }
 52
 53 void addedge(int u, int v) {
 54     Next[tot] = head[u];
 55     To[tot] = v;
 56     vis[tot] = 0;
 57     head[u] = tot++;
 58 }
 59
 60 void eular(int u){
 61     used[u] = 1;
 62     int i;
 63     while(head[u]!=-1){
 64     //for(int &i = head[u];i != -1;i = Next[i]){ 65         i = head[u];
 66         head[u] = Next[head[u]];
 67         if(vis[i])continue;
 68         vis[i^1] = 1;
 69         eular(To[i]);
 70     }
 71 }
 72 int Scan() {
 73     int res=0, ch;
 74     while(ch=getchar(), ch<'0'||ch>'9');
 75     res=ch-'0';
 76     while((ch=getchar())>='0'&&ch<='9')
 77         res=res*10+ch-'0';
 78     return res;
 79 }
 80 void Out(int a) {
 81     if (a > 9)
 82         Out(a / 10);
 83     putchar(a % 10 + '0');
 84 }
 85
 86 class hdu5348 {
 87 public:
 88     void solve() {
 89         int t;
 90         t =Scan();//in >> t;
 91         while (t--) {
 92             int n, m;
 93             n = Scan();m=Scan();//in >> n >> m;
 94             init(n);
 95             rep(i, n)deg[i] = 0;
 96             int u, v;
 97             rep(i, m) {
 98                 u = Scan();v= Scan();//in >> u >> v;
 99                 u--, v--;
100                 deg[u]++;
101                 deg[v]++;
102                 addedge(u, v);
103                 addedge(v, u);
104             }
105             gao = -1;
106             rep(i, n) {
107                 if (deg[i] & 1) {
108                     if (gao != -1) {
109                         addedge(i, gao);
110                         addedge(gao, i);
111                         gao = -1;
112                     } else gao = i;
113                 }
114             }
115             rep(i, n) used[i] = 0;
116             /*rep(i,n){
117                 if(!used[i]){
118                     dfs(i);
119                     num++;
120                 }
121             }*/
122             gao = -1;
123             rep(i, n) {
124                 if (!used[i]) {
125                     eular(i);
126                 }
127             }
128             m<<=1;
129             for(int i=1;i<m;i+=2){
130                 if (vis[i])putchar('1');
131                 else putchar('0');
132                 putchar('\n');
133             }
134
135         }
136     }
137 };
138
139
140 int main() {
141     //std::ios::sync_with_stdio(false);
142     //std::cin.tie(0);
143     hdu5348 solver;
144     //std::istream &in(std::cin);
145     //std::ostream &out(std::cout);
146     solver.solve();
147     return 0;
148 }

转载于:https://www.cnblogs.com/fraud/p/4705833.html

hdu5348 MZL's endless loop(欧拉回路)相关推荐

  1. 2015 Multi-University Training Contest 5

    1001 MZL's Circle Zhou 1002 MZL's xor 水题中最后一个做的.因为看成i<j了. 后来听学长说才发现i可以等于j.真是orz. 1 # include < ...

  2. python中变量的作用域有几种_Python中变量的作用域(variable scope)

    http://www.crifan.com/summary_python_variable_effective_scope/ 解释python中变量的作用域 示例: 1.代码版 #!/usr/bin/ ...

  3. (转)jLink使用ITM机制实现调试stm32单片机

    ----------------------------------------------------------------------------------------------- 作者:p ...

  4. Solr初始化源码分析-Solr初始化与启动

    用solr做项目已经有一年有余,但都是使用层面,只是利用solr现有机制,修改参数,然后监控调优,从没有对solr进行源码级别的研究.但是,最近手头的一个项目,让我感觉必须把solrn内部原理和扩展机 ...

  5. 在Struts2中使用OGNL

    OGNL是XWork引入的一个非常有效的数据处理的工具.我们已经了解了OGNL的基本操作和OGNL的内部结构,接下来,我们来看看XWork对OGNL做了什么样的加强,以及OGNL的体系在Struts2 ...

  6. skynet 控制台管理使用技巧

    skynet 自带了一个控制台服务.能够非常方便获取和调试 skynet 执行数据,并且能够热更新代码,所以.弄明确skynet控制台管理能够让你更好地使用skynet,甚至改进这个控制台服务.以满足 ...

  7. Keil MDK 中利用串口及c标准库函数printf为cortex-m3做调试输出(lpc1788)

    摘要: c标准库的printf是输出给显示器的,将printf函数进行修改,使其输出重定向至串口,就能实现目的.printf函数调用fputc函数完成实质输出单一字符的工作,因此将fputc函数修改使 ...

  8. 用Xlib库进行基本图形编程

    用Xlib库进行基本图形编程 用Xlib库进行基本图形编程 目录 1.前言 2.X Window系统的客户服务器模式 3.GUI编程-同步化的编程模型 4.基本的Xlib概念         1.X ...

  9. Hermite曲线与Bezier曲线的关系

    结论 最近在研究3次样条曲线.曲线由四个控制点控制,依次记为P0,P1,P2,P3.在绘制Hermite曲线的时候,发现如果令P0处的导数为3倍P1-P0,P3处的导数为3倍P3-P2,则P0,P1, ...

最新文章

  1. 客服人员控制台Console,Salesforce Service Cloud的核心
  2. PHP数组 转 对象/对象 转 数组
  3. 随机抽样java_实现随机抽样【随机数生成问题】
  4. oracle11查看dblink,配置oracle11g通过dblink+透明网关访问GBase
  5. 无熟人难办事?—迪米特法则
  6. K-means++算法的学习笔记~
  7. python 玩彩票程序 随机产生两位数与用户输入的相比较
  8. Firefox扩展开发
  9. u盘文件名乱码linux,U盘文件名乱码的原因和解决办法
  10. mac 连接android手机调试,mac连接华为手机,无法真机调试。
  11. 笔记本linux系统重装步骤(Centos7.0)
  12. 前后端通信:WebSocket之实时监控
  13. 宝宝泡药浴和直接吃药有什么区别吗?
  14. MSP430F149单片机-IAP升级
  15. js,jquery常用拼接html方法,js,jquery拼接字符串
  16. 【Linux 主机ssh远程连接暴力破解详解】
  17. Linux/Centos nethogs 按进程监控网络带宽
  18. 报如下错误:android.util.AndroidRuntimeException: You cannot combine custom titles with other title featur
  19. 17位行业影响力者的数字藏品2022年趋势研判!丨巴比特数字藏品高峰论坛金句实录...
  20. arcgis license manager点击启动无反应

热门文章

  1. 代码优化 5 大原则,第一条就是别优化了!!!
  2. 邪恶的Java帝国是怎么欺负小函数的?
  3. Java 实现单例模式的 9 种方法
  4. 过了所有技术面,却倒在 HR 一个问题上。。
  5. JAVA拾遗--关于SPI机制
  6. 详解 Java 中的三种代理模式
  7. IDEA中实用的快捷方式
  8. 多线程 python layer_在Caffe中加Python Layer的方法
  9. BODY background=自适应大小_自适应(电脑/平板/手机)网页,自适应网页设计练习总结...
  10. 数据中心自动化及其优势