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

MZL's endless loop(欧拉回路,欧拉路径)

2015-08-05 01:48 393 查看

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

题意: 给一个n个点,m条边的无向图,要求给m条边定方向,使得每个定点的出入度之差的绝对值小于等于1. 输出任意一种结果.(输出m条边的方向)

题解: 
      
     <1>定理: 一个图,肯定存在偶数个奇度点.
     通过<1>,我们可以找出若干条欧拉路径,直接按遍历的路径,定一个方向,对于环来说,那就更简单了,随便定那个方向都能符合题目的条件.

AC代码:
/* ***********************************************
Author        :xdlove
Created Time  :2015年07月31日 星期五 12时37分29秒
File Name     :a.cpp
 ************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

/**宏定义类
 * **/
#define FOR(i,s,t) for(int i = (s); i < (t); i++)
#define FOR_REV(i,s,t) for(int i = (s - 1); i >= (t); i--)
#define mid ((l + r) >> 1)
#define clr(a) memset(a,0,sizeof(a))
#define lson l,mid,u<<1
#define rson mid+1,r,u<<1|1
#define ls u<<1
#define rs u<<1|1

typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);

/**输入输出挂类模板
 * **/

class Fast
{
    public:
        inline void rd(int &ret)
        {
            char c;
            int sgn;
            if(c = getchar(),c == EOF) return;
            while(c != '-' && (c < '0' || c > '9')) c = getchar();
            sgn = (c == '-') ? -1 : 1;
            ret = (c == '-') ? 0 : (c - '0');
            while(c = getchar(),c >= '0' && c <= '9')
                ret = ret * 10 + c - '0';
            ret *= sgn;
        }

    public:
        inline void pt(int x)
        {
            if(x < 0)
            {
                putchar('-');
                x = -x;
            }
            if(x > 9) pt(x / 10);
            putchar(x % 10 + '0');
        }
};
Fast xd;

const int M = 1e5 + 5;
struct Edge
{
    int to,next,id;
}e[M * 6];
int tot,head[M],pos;
int del[M];

void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v)
{
    e[tot].to = v;
    e[tot].next = head[u];
    e[tot].id = 0;
    head[u] = tot++;
}

bool dfs(int u)
{
    int v,id;
    for(int &i = head[u]; ~i; i = e[i].next)
    {
        v = e[i].to;
        id = e[i].id ^ e[i^1].id;
        if(id) continue;
        e[i].id = 1;
        if(del[v])
        {
            del[v] = 0;
            return true;
        }
        if(dfs(v)) return true;
    }
    return false;
}
            

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        scanf("%d %d",&n,&m);
        init();
        FOR(i, 0, m)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            del[u] ^= 1;
            del[v] ^= 1;
            addedge(u,v);
            addedge(v,u);
        }
        for(int i = 1; i <= n; i++)
        {
            if(del[i])
            {
                del[i] = 0;
                dfs(i);
            }
        }
        for(int i = 1; i <= n; i++)
            while(~head[i]) dfs(i);  
        for(int i = 0; i < tot; i += 2)
        {
            if(e[i].id) puts("1");
            else puts("0");
        }
    }
    return 0;
}



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