hdu 5348 MZL's endless loop 2015 Multi-University Training Contest 5

MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1830    Accepted Submission(s): 393
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.
T100 1n105 1m3105 n2105 m7105 .
 

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  uivi 0  for  uivi .
 

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
 



n个点,m条边,要将m条边变成有向边,使每个点的入度与出度差的绝对值不超过1。

比较容易想到成对的考虑出边和入边,那么对于每个点这样计算会剩下一条边或多算一条,

一条边对两个点的度数+1,所有点的度数和为偶数,那么度数为奇数的点有偶数个,记为2*k,我们把2*k个点,

分成k组,每组的两个点补一条边。这样每个点的度数为偶数,因此图中存在1个或多个欧拉回路,将所有边用dfs遍历一次,从一个点出发一次,必然会回到该点一次,可以满足每个点的出度和入度相等,再把补掉的变拆除,可以满足出度入度之差不超过1。


#include<cstdio>
#include<iostream>
#include<algorithm>
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#pragma comment(linker, "/STACK:102400000,102400000")


using namespace std;

const int M=  300000 +5 ;

int n,m,e_max;
int fir[M],u[M<<2],v[M<<2];
int in[M];
bool vis[M<<2];
int ans[M<<2];
int nex[M<<2];


inline void add_edge(int s,int t)
{
    int e=e_max++;
    u[e]=s;
    v[e]=t;
    nex[e]=fir[s];
    fir[s]=e;


}

void dfs(int x)
{

    for(int &e=fir[x];~e;)
    {
        if( vis[e]  ) {e=nex[e]; continue;}
        vis[e]=1;
        vis[e^1]=1;
        int y=v[e];
        in[y]--;
        in[x]--;
        ans[e>>1]=  1^(1&e) ;
        e=nex[e];
        dfs(y);

    }



}


void repair()
{
    int p=-1;
    for(int i=1;i<=n;i++)
    {
        if(in[i]&1 )
        {
            if(p!=-1)
                {
                  add_edge(p,i);
                  add_edge(i,p);
                  in[p]++;
                  in[i]++;
                  p=-1;
                }
            else
                p=i;
        }

    }
}

int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int x,y;
for(int i=1;i<=n;i++)  fir[i]=-1;

        e_max=0;
        FOR1(i,m)
        {
            scanf("%d%d",&x,&y);
            add_edge(x,y);
            add_edge(y,x);
            in[x]++;in[y]++;

        }

        repair();
        for(int i=0;i<e_max;i++)
            vis[i]=0;
        for(int i=1;i<=n;i++)
        {
            if(in[i])  dfs(i);
        }
        for(int e=0;e<m;e++)
        {
            printf("%d\n",ans[e]);
        }


    }


    return 0;
}




n个点,m条边,要将m条边变成有向边,使每个点的入度与出度差的绝对值不超过1。

比较容易想到成对的考虑出边和入边,那么对于每个点这样计算会剩下一条边或多算一条,

一条边对两个点的度数+1,所有点的度数和为偶数,那么度数为奇数的点有偶数个,记为2*k,我们把2*k个点,

分成k组,每组的两个点补一条边。这样每个点的度数为偶数,因此图中存在1个或多个欧拉回路,将所有边用dfs遍历一次,从一个点出发一次,必然会回到该点一次,可以满足每个点的出度和入度相等,再把补掉的变拆除,可以满足出度入度之差不超过1。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值