您的位置:首页 > 运维架构

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

/************************************************
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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: