HDU5348——DFS——MZL's endless loop
2015-08-06 20:57
435 查看
[align=left]Problem Description[/align]
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|\leq 1$ is satisified.
The graph you are given maybe contains self loops or multiple edges.
[align=left]Input[/align]
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 $u_i$ and $v_i$, which describe an edge of the graph.
$T\leq 100$, $1\leq n\leq 10^5$, $1\leq m\leq 3*10^5$, $\sum n\leq 2*10^5$, $\sum m\leq 7*10^5$.
[align=left]Output[/align]
For each test case, if there is no solution, print a single line with $-1$, otherwise output $m$ lines,.
In $i$th line contains a integer $1$ or $0$, $1$ for direct the $i$th edge to $u_i\rightarrow v_i$, $0$ for $u_i\leftarrow v_i$.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
7 6
1 2
1 3
1 4
1 5
1 6
1 7
[align=left]Sample Output[/align]
1
1
1
0
1
0
1
0
1
[align=left]Source[/align]
2015 Multi-University Training Contest 5
详情见博文/article/6505495.html
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|\leq 1$ is satisified.
The graph you are given maybe contains self loops or multiple edges.
[align=left]Input[/align]
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 $u_i$ and $v_i$, which describe an edge of the graph.
$T\leq 100$, $1\leq n\leq 10^5$, $1\leq m\leq 3*10^5$, $\sum n\leq 2*10^5$, $\sum m\leq 7*10^5$.
[align=left]Output[/align]
For each test case, if there is no solution, print a single line with $-1$, otherwise output $m$ lines,.
In $i$th line contains a integer $1$ or $0$, $1$ for direct the $i$th edge to $u_i\rightarrow v_i$, $0$ for $u_i\leftarrow v_i$.
[align=left]Sample Input[/align]
2
3 3
1 2
2 3
3 1
7 6
1 2
1 3
1 4
1 5
1 6
1 7
[align=left]Sample Output[/align]
1
1
1
0
1
0
1
0
1
[align=left]Source[/align]
2015 Multi-University Training Contest 5
详情见博文/article/6505495.html
/************************************************ Author :powatr Created Time :2015-8-6 19:51:08 File Name :b.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> #pragma comment (linker, "/STACK:102400000,102400000"); using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAX = 1e5 + 10; const int MAXN = 7e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int head[MAX]; int du[2][MAX]; int sum[MAX]; int vis[MAXN], ans[MAXN]; int n, m, E, u, v; struct edge{ int u, v; int next; }a[MAXN]; void inti() { E = 0; memset(head, -1, sizeof(head)); memset(vis, 0, sizeof(vis)); memset(sum , 0, sizeof(sum)); memset(du, 0, sizeof(du)); memset(ans, 0, sizeof(ans)); } void add(int u, int v) { a[E].u = u; a[E].v = v; a[E].next = head[u]; head[u] = E++; } void dfs(int u, int y) { for(int i = head[u]; ~i; i = a[i].next){ if(vis[i]) { head[u] = a[i].next; //访问过就删去 continue; } int v = a[i].v; if(v!=u && du[y][v] < du[y^1][v]) continue; vis[i] = vis[i^1] = 1; if(i%2) ans[i/2] = y^1;//表示从u到v else ans[i/2] = y; du[y][u]++; du[y^1][v]++; head[u] = a[i].next; dfs(v,y); return; } } int main() { int T; scanf("%d", &T); while(T--){ scanf("%d%d", &n, &m); inti(); for(int i = 1 ; i <= m; i++){ scanf("%d%d", &u, &v); add(u, v); add(v, u); sum[v]++; sum[u]++; } for(int i = 1; i <= n; i++){ while(du[0][i] + du[1][i] < sum[i]){ if(du[0][i] <= du[1][i]) dfs(i, 0); else dfs(i, 1); } } for(int i = 0 ; i < m; i++) printf("%d\n", ans[i]); } return 0; }
相关文章推荐
- docker入门实践01
- Centos Mysql-5.6版本安装
- linux的启动顺序
- linux下备份日志脚本
- java集群优化——Nginx+tomcat集群配置-实践篇
- Linux uniq 命令
- centos 安装卸载软件命令 & yum安装LAMP环境
- win7(64位)php5.5-Apache2.4-环境安装
- iptables 实现流量监控,重定向: 一
- Linux学习之CentOS(二十八)--RAID原理基础及Linux下软件RAID配置
- linux不同gcc/G++版本之间的切换
- 关于 tomcat 集群中 session 共享的三种方法
- Linux - Bash shell的功能;内建命令type
- Linux - Bash shell的功能;内建命令type
- 八月六日 Linux操作系统学习笔记
- DedeAMPZ 网吧能安装却不能打开网站
- Ganymed SSH-2 java执行远程linux机器命令工具
- Vmware Workstation 安装linux (CentOs)系统
- 分别在Linux和Windows下设置JVM内存
- 记录:点击popwindow外部不消失bug解决