码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-05 16:20:41      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:算法   搜索   

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 u技术分享i技术分享技术分享 and v技术分享i技术分享技术分享, which describe an edge of the graph.
T100技术分享, 1n10技术分享5技术分享技术分享, 1m3?10技术分享5技术分享技术分享, n2?10技术分享5技术分享技术分享, m7?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技术分享v技术分享i技术分享技术分享, 0技术分享 for u技术分享i技术分享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
 

题意:给一个无向图,把这个图变成有向图,要求每个点的|入度-出度|<=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]);
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:算法   搜索   

原文地址:http://blog.csdn.net/u010372095/article/details/47298019

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!