2015 Multi-University Training Contest 5 F(MZL's endless loop)

探讨如何将任意无向图转化为有向图,确保每个节点的入度与出度差值绝对值不超过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.
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。

思路:来自:https://blog.youkuaiyun.com/k183000860/article/details/47312943

       如果对于某个点u,它的入度in[u]>=出度out[u],那么就正向搜索,我们找一个与u相连且不等于u的点v,假设点v的in[v]<out[v],那么刚好符合条件,我们就将u,v这条边置为1,使得out[u]++,in[v]++时间复杂度为O(n+m),要达到这个效率,我们用链式向前星来存储边,然后用head[u]=edge[i].next来删除已经访问过的边。但这道题目我不太知道怎么去证明无论如何都不会没有解,即都不会输出-1。

 

总之,找到奇度数的结点出发,奇度数结点结束,(一定有偶数个奇结点),相当于把此图分为若干欧拉图,毕竟欧拉图一定满足每个点的入度和出度之差绝对值不超过1。

代码:

const int maxn=1000005;
struct node
{
    int to;
    int cost;
    int next;
}G[maxn];
int head[maxn],cnt;
int n,m,sign,deg[maxn];
void add(int u,int v)
{
    G[cnt].to=v;
    G[cnt].cost=0;
    G[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int s)
{
    if(sign) return ;
    for(int i=head[s];i!=-1;i=G[i].next)
    {
        if(sign) return ;
        int temp=G[i].to;
        int flag=(G[i].cost^G[i^1].cost);//给边已经走过
        if(flag) continue;
        head[s]=G[i].next;//
        G[i].cost=1;
        if(deg[temp])
        {
            deg[temp]=0;
            sign=1;
            return ;
        }
        if(!sign)
        dfs(temp);
    }
    return ;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cnt=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        deg[i]=0,head[i]=-1;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
            deg[u]^=1;
            deg[v]^=1;
        }
        for(int i=1;i<=n;i++)
        {
            if(deg[i])
            {
                sign=0;
                deg[i]=0;
                dfs(i);
            }
        }
        sign=0;
        for(int i=1;i<=n;i++)
        {
            if(head[i]!=-1)//该点连的边有没标完的
            {
                dfs(i);
            }
        }
        for(int i=0;i<cnt;i+=2)
        {
            cout<<G[i].cost<<endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值