HDU 5348 MZL's endless loop (dfs+思维)

MZL's endless loop

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3134    Accepted Submission(s): 741
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

题意:给你一个无向图(可能不连通、有重点、有重边),要你把所有边定向,如果每个点的出度和入度相差不超过1,那么就输出你定向后每条边的方向。否则-1。1表示正向,0表示反向(其实反过来也可以)

思路:其实不存在-1的情况。即每条边定向后一定存在解。因为度数为奇数的点只能有偶数个。

于是直接对每一个点暴搜一条路径,使路径上的点入度出度相差不超过1。直到该点的入度==出度。注意如果一个边已经被定向,必须删除这条边(否则会T)。还有一个点假设缺少入度,那你就不能把它和出度大于入度的点连边,因为这样会导致不合法。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=200005;
inline int read()
{
register int c=getchar(),fg=1,sum=0;
while(c<='9'&&c>='0'){sum=sum*10+c-'0';c=getchar();}
    return fg*sum;
}
int n,m;
int vis[maxn*4],d[2][maxn],he[maxn],t,du[maxn],ans[maxn*4];
struct node
{
    int to,nex;
}a[maxn*4];
void add(int x,int y)
{
    a[t].to=y;
    a[t].nex=he[x];
    he[x]=t++;
}
void init()
{
    memset(vis,0,sizeof(vis));
    memset(he,-1,sizeof(he));
    memset(d,0,sizeof(d));
    memset(du,0,sizeof(du));
    memset(ans,-1,sizeof(ans));
    t=0;
}
void dfs(int u,int f)
{
    for(int i=he[u];i!=-1;i=a[i].nex)
    {
        if(vis[i])
        {
            he[u]=a[i].nex;
            continue;
        }
        int v=a[i].to;
        if(u!=v&&d[f][v]<d[f^1][v]) continue;
        if(i&1) ans[i/2]=f^1;
        else ans[i/2]=f;
        d[f][u]++;
        d[f^1][v]++;
        vis[i]=vis[i^1]=1;
        he[u]=a[i].nex;
        dfs(v,f);
        break;
    }
}
int main()
{
    int T;
    T=read();
    while(T--)
    {
        init();
        n=read();m=read();
        for(int i=0;i<m;i++)
        {
            int u,v;
            u=read();v=read();
            add(u,v);
            add(v,u);
            du[u]++;du[v]++;
        }
        for(int i=1;i<=n;i++)
        {
            while(d[0][i]+d[1][i]<du[i])
            {
                if(d[0][i]<=d[1][i]) dfs(i,0);
                else dfs(i,1);
            }
        }
        for(int i=0;i<m;i++)
        printf("%d\n",ans[i]);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值