HDU5348——DFS——MZL's endless loop

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|\leq 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 $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$.

Output

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$.

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

Source

2015 Multi-University Training Contest 5

详情见博文http://www.cnblogs.com/alihenaixiao/p/4704074.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;
}

  

HDU5348——DFS——MZL's endless loop

时间: 2024-08-05 19:07:59

HDU5348——DFS——MZL's endless loop的相关文章

hdu5348 MZL&#39;s endless loop(欧拉回路)

转载请注明出处: 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): 369Special Judge Problem Descripti

hdu5348(2015多校5)--MZL&#39;s endless loop(搜索)

题目链接:点击打开链接 题目大意:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1 直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边u,v就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边u,v认为这是一条u到v的有向边),一直搜索到找不到边能继续为止. 注意: 1.已经使用过的边为了防止再次被遍历,可以修改head,head[u] = edge[i].next 2.注意自环,因为搜索是判断能不能继续向

[2015hdu多校联赛补题]hdu5348 MZL&#39;s endless loop

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 题意:给你一个无向图,要你将无向图的边变成有向边,使得得到的图,出度和入度差的绝对值小于等于1,如果无解输出-1 解:考虑奇数度的点一定会成对出现(因为所有度数和肯定是偶数个->因为一条边产生两度~),那么我们可以将奇数度的点两两一连消除掉(两奇数度点的出度入读差的绝对值都为1, 路径上的点的差绝对值为0) 然后偶数度的点可以成环,那么可以搜出所有的环 1 /* 2 * Problem: 3

Hdu 5348 MZL&#39;s endless loop (dfs)

题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)<2.如果能输出任意一种合法方案. 解题思路: 其实仔细考虑一下,每个无向图都会存在合法方案的.证明:度数和为奇数的点只能为起点或者终点,度数为偶数的只能是环上的起点或者终点或者是中间点.有m条边,一共有2*m个端点.所以呢?当然是度数和为奇数的个肯定是偶数个,任意两个相结合都可以形成一条路.并不会有两条

图论 HDOJ 5348 MZL&#39;s endless loop

题目传送门 1 /* 2 题意:给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果 3 图论:一个图,必定存在偶数个奇度顶点.那么从一个奇度定点深搜,当碰到另外一个奇度顶点时结束,这样能保证度数差<=1 3.5 详细解释:http://blog.csdn.net/ZSGG_ACM/article/details/47287681 4 */ 5 /*********************************************

MZL&#39;s endless loop(欧拉回路,欧拉路径)

MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 751    Accepted Submission(s): 138 Special Judge Problem Description As we all kown, MZL hates the endless loop deeply, and

HDU 5348 MZL&#39;s endless loop

乱搞题...第一直觉是混合图的欧拉通路,但是感觉并没有多大关系.最终AC的做法是不断的寻找欧拉通路,然后给边标号.所有边访问了一遍,所有点访问了一遍,效率是o(n+m).不存在-1的情况. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int maxn=100000

hdu 5348 MZL&#39;s endless loop 暴搜

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5348 MZL's endless loop Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1426    Accepted Submission(s): 319 Special Judge Problem Description As w

hdu 5348 MZL&#39;s endless loop 欧拉回路

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): 331Special Judge Problem Description As we all kown, MZL hates the endless loop deeply, and h