hdu 5348 MZL's endless loop 欧拉回路
来源:互联网 发布:凯撒博尔吉亚 知乎 编辑:程序博客网 时间:2024/05/23 19:53
MZL's endless loop
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1502 Accepted Submission(s): 331
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 withn 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.
You are given an undirected graph with
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 integersn and m .
And the nextm 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 .
For each test case, the first line contains two integers
And the next
Output
For each test case, if there is no solution, print a single line with −1 , otherwise output m lines,.
Ini th line contains a integer 1 or 0 , 1 for direct the i th edge to ui→vi , 0 for ui←vi .
In
Sample Input
23 31 22 33 17 61 21 31 41 51 61 7
Sample Output
111010101
Source
2015 Multi-University Training Contest 5
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348
题目大意:将一个无向图构造出一个有向图,要求每个点的入度和出度之差绝对值不超过1。
思路,重点在于删边,边表删边相对于vector存储而言更方便一些,因为.next自带边号,每次把head[],以i为起点的第一条边存储的位置(实际上是最后输入的那条边的边号)改为edge[i].next,即实现了“拆边”。
AC代码:
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <vector>using namespace std;#define MAXN 100010#define MAXM 300010int t, n, m;int targ[MAXN][2];int degree[MAXN], ans[MAXM << 1];int head[MAXN], edge_cnt;struct node { int v, next;} edge[MAXM << 1];void init() { memset(head, -1, sizeof head); memset(degree, 0, sizeof degree); memset(targ, 0, sizeof targ); memset(ans, -1, sizeof ans); edge_cnt = 0;}void add_edge(int u, int v) { edge[edge_cnt].v = v; edge[edge_cnt].next = head[u]; head[u] = edge_cnt++;}void DFS(int u, int col) { for(int i = head[u]; ~i; i = edge[i].next) { if(ans[i] != -1) { head[u] = edge[i].next;//拆边 continue; } int v = edge[i].v; if(u != v && targ[v][col ^ 1] > targ[v][col]) continue; ans[i] = col; ans[i ^ 1] = col ^ 1; head[u] = edge[i].next; targ[u][col]++; targ[v][col ^ 1]++; DFS(v, col); break; }}int main() { scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); init(); int a, b; for(int i = 1; i <= m; i++) { scanf("%d%d", &a, &b); add_edge(a, b); add_edge(b, a); degree[a]++; degree[b]++; } for(int i = 1; i <= n; i++) { while(targ[i][0] + targ[i][1] < degree[i]) { if(targ[i][0] < targ[i][1]) DFS(i, 0); else DFS(i, 1); } } for(int i = 0; i < edge_cnt; i += 2) { printf("%d\n", ans[i]); } } return 0;}
0 0
- hdu 5348 MZL's endless loop 欧拉回路
- MZL's endless loop(欧拉回路,欧拉路径)
- hdu 5348 MZL's endless loop(欧拉路径+欧拉环)
- hdu 5348 MZL's endless loop
- HDU 5348 MZL's endless loop
- *HDU 5348 - MZL's endless loop(搜索)
- hdu 5348 MZL's endless loop
- hdu 5348 MZL's endless loop 暴搜
- hdu 5348 MZL's endless loop dfs
- Hdu 5348 MZL's endless loop (构造)
- hdu 5348 MZL's endless loop(dfs+图论)
- hdu 5348 MZL's endless loop(dfs)
- HDU 5348 MZL's endless loop(DFS+邻接表)
- HDU 5348 MZL's endless loop (dfs+删边)
- HDOJ 5348 MZL's endless loop 乱搞
- HDOJ 5348 MZL's endless loop
- HDU 5348 MZL's endless loop(思想用的是深搜)经典
- hdu 5348 MZL's endless loop(15多校第五场1006) 欧拉路
- Activiti 10分钟教程
- HTTP协议中PUT和POST使用上的区别
- ExpandLayout用法和封装自定义控件的一些小记
- HTML5桌面通知:notification api
- uva 10282 Babelfish(映射)
- hdu 5348 MZL's endless loop 欧拉回路
- mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file
- hdu4975 网络流及‘删边法’判是否为唯一流
- Java 并发专题 : Semaphore 实现 互斥 与 连接池
- 在Eclipse中使用Jersey和Tomcat构建RESTful WebService及其调用
- Python windows 下 入门操作
- J - Intelligent IME---(2015 summer training #3)
- 虚拟机的优化工具——VMware tools
- HDU 2870