HDU 5348 MZL's endless loop(思想用的是深搜)经典

MZL's endless loop

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

题意:给一个无向图,把这个图变成有向图,要求每个点的|入度-出度|<=1。如果有则输出每个边〈u , v 〉的状态(1:表示原输入的无向边为 <u , v > 。0:则原输入为<v  , u >)。

解题:

直接进行搜索,对每个点进行出度和入度的判断,如果出度大,就先进行反向的搜索(每搜索一条边<u,v>就认为这是一条v到u的有向边),反之,进行正向搜索(每搜到一条边<u,v>认为这是一条u到v的有向边),一直搜索到找不到边能继续走为止。一定会找到满足条件的一组答案。

注意:

已经使用过的边为了防止再次被遍历,可以修改head,head[u] = edge[i].next

#include<stdio.h>
#include<string.h>
const int MAXN = 210005;
const int MAXM = 710005;
struct EDG{
    int to,next;
    int id,flag; //每条边的编号,flag=1表示当前边与输入的顺序一至,否则顺序相反
}edg[MAXM];
int eid,head[MAXN];
int in[MAXN],out[MAXN] , ans[MAXM];

void init()
{
    eid=0;
    memset(head , -1 ,sizeof(head));
    memset(ans , -1 ,sizeof(ans));
    memset(out , 0 , sizeof(out));
    memset(in  , 0 , sizeof(in));
}
void addEdg(int u,int v,int id)
{
    edg[eid].to=v; edg[eid].next=head[u];
    edg[eid].id=id; edg[eid].flag=1; head[u]=eid++;

    edg[eid].to=u; edg[eid].next=head[v];
    edg[eid].id=id; edg[eid].flag=0; head[v]=eid++;
}
// u -> v , 正着走
void outIn(int u)
{
    int i , v;
    while(head[u]!=-1){
        i=head[u];
        v=edg[i].to;
        if(ans[edg[i].id]==-1){ //每条边只能走一次
            ans[edg[i].id]=edg[i].flag;
            out[u]++;
            in[v]++;
            head[u]=edg[i].next; //走完后直接可以
            u=v;
        }
        else
            head[u]=edg[i].next;
    }
}
// u <- v,反着走
void inOut(int u)
{
    int i , v;
    while(head[u]!=-1){
        i=head[u];
        v=edg[i].to;
        if(ans[edg[i].id]==-1){
            ans[edg[i].id]=!edg[i].flag; //从u -> v 实则 v -> u
            in[u]++;
            out[v]++;
            head[u]=edg[i].next;
            u=v;
        }
        else
            head[u]=edg[i].next;
    }
}
int main()
{
    int T,n,m,u,v;
    scanf("%d",&T);
    while(T--){

        scanf("%d%d",&n,&m);
        init();

        for(int i=1; i<=m; i++){
            scanf("%d%d",&u,&v);
            addEdg(u , v ,i);
        }
        for(int u=1; u<=n; u++){
            while(head[u]!=-1){
                if(in[u]>=out[u])
                    outIn(u);
                else
                    inOut(u);
            }
        }
        for(int i=1; i<=m; i++)
            printf("%d\n",ans[i]);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值