2015 多校联赛 ——HDU5348(搜索)

本文提供了一种算法来调整无向图中的边,使得每个节点的出度与入度之差不超过1,通过搜索和调整边的方向,确保图的平衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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 01 for direct the ith edge to uivi0 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


题:给出n个点,m条无向边,现在要求将无向边变为有向边,要保证每个点的出度和入度的差不超过1


思路来自:http://blog.youkuaiyun.com/winddreams/article/details/47281397


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


对于已经判断过的边删去:head[u] = edge[i].next;

证明不会有-1的情况,对于一个点v,假设入度比出度多2,那么,第一:就会有一条边搜索到v后找不到后继,导致v的入度比出度多1,第二:又有一条边搜索到v,导致v的入度比出度多2,但是这样的话就会和第一条找不到后继冲突,所以不会出现入度比出度多2的情况,(其他情况也是类似),所以不会有-1的结果。

(果然稍难一点自己就不知道怎么办了  好坑orz)



#include <iostream>
#include <cstdio>
#include<algorithm>
#include<cstring>
#include<functional>
#include<queue>
typedef long long ll;
using namespace std;

struct node
{
    int u,v,ci;
    int next;
} edge[700000];
int n,m,to;
int head[100010] , vis[700000] ;
int in[100010] , out[100010] , num[100010] ;
int ans[700000]  ;

void add(int u,int v,int c)
{
    edge[to].u= u;
    edge[to].v= v;
    edge[to].ci = c;
    edge[to].next = head[u];
    head[u] = to++;
}



void dfs1(int u)                                   //正向搜索
{
    for(int i = head[u]; ~i; i = edge[i].next)
    {
        if(vis[i]) 
        {
            head[u] = edge[i].next;               //删边
            continue;
        }
        int v = edge[i].v;
        if(u != v && in[v] > out[v])       //u->v,in[v] > out[v]时,跳过
            continue;
        vis[i] = vis[i^1] = 1;        //将u,v之间的两条边标记
        if(i %2)                      //有输入可知,i为偶时u->v;  i为奇时,v->u
            ans[i/2] = 0;
        else
            ans[i/2] = 1;
        out[u]++;
        in[v]++;
        head[u] = edge[i].next;
        dfs1(v);
        break;
    }
}

void dfs2(int u)
{
    for(int i = head[u]; ~i; i = edge[i].next)
    {
        if(vis[i])
        {
            head[u] = edge[i].next;
            continue;
        }
        int v = edge[i].v;
        if(u != v && in[v] < out[v])
            continue;
        vis[i] = vis[i^1] = 1;
        if(i %2)
            ans[i/2] = 1;
        else
            ans[i/2] = 0;
        out[v]++;
        in[u]++;
        head[u] = edge[i].next;
        dfs2(v);
        break;
    }
}

int main()
{
    int T;
    int a, b;
    scanf("%d",&T);
    while(T--)
    {
        to = 0;
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        for(int i = 0;i <= n;i++)
        {
            in[i] = out[i] = num[i] = 0;head[i] = -1;
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d",&a,&b);
            add(a,b,i);
            add(b,a,i);
            num[a]++;
            num[b]++;
        }

        for(int i = 1; i <= n; i++)
        {
            while(in[i] + out[i] < num[i])
            {
                if(in[i] >= out[i])         //正反不停搜,直到找不到边为止
                    dfs1(i);
                else
                    dfs2(i);
            }
        }
        for(int i = 0; i < m; i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值