2018 Multi-University Training Contest 2 &&HDU 6311 Cover【欧拉路径覆盖】

                                                   Cover

                               Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                      Special Judge

 

Problem Description

The Wall has down and the King in the north has to send his soldiers to sentinel.
The North can be regard as a undirected graph (not necessary to be connected), one soldier can cover one path. Today there's no so many people still breathing in the north, so the King wants to minimize the number of soldiers he sent to cover each edge exactly once. As a master of his, you should tell him how to arrange soldiers.

Input

There might be multiple test cases, no more than 20. You need to read till the end of input.
In the first line, two integers n and m, representing the number of nodes and edges in the graph.
In the following m lines, each contain two integers, representing two ends of an edge.
There are no parallel edges or self loops.
1≤n,m≤100000

Output

For each test case, the first line contains number of needed routes, p.
For the following p lines, an integer x in the beginning, followed by x integers, representing the list of used edges. Every integer should be a positive or negative integer. Its absolute value represents the number of chosen edge (1~n). If it's positive, it shows that this edge should be passed as the direction as the input, otherwise this edge should be passed in the direction different from the input. Edges should be in correct order.

Sample Input

3 3
1 2
1 3
2 3

Sample Output

1
3 1 3 -2

【题目链接】 Cover

【题意】

有一个图,n个顶点,m条边(不一定连通),现在问最少可以用多少条简单路径覆盖所有的边,并输出方案

【思路】

首先有一个结论,对于一个连通图(单个点除外),如果奇度数点个数为 k,那么至少需要max{k/2,1}条路径才能覆盖。

因为对于欧拉回路或者欧拉通路才能“一笔画”,所以我们先把图中所有奇度顶点两两连边(虚边)。然后如果有奇度顶点,从奇度顶点出发dfs其欧拉回路,经过的边和其反向均打上标记,并在dfs回溯时记录边的标号,由于采用的是是链式前向星,注意id应该取负。奇度顶点dfs完后对剩下的偶度顶点同理。

回溯时需要把虚边断开,相邻的边保存在不同路径中。

从奇度顶点遍历时需要注意,加边时其实加到只剩两个奇度顶点即可,我们全部连接了,所以最后应该要num--。即此时只要求欧拉通路即可,最后那条虚边可以不经过。

#include <cstdio>
#include <bits/stdc++.h>
#include <cmath>
#include <map>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef  long long ll;
const int maxn = 100015;
const ll mod = 1e18+7;
const int INF = 1e9;
const double eps = 1e-6;

int n,m,num,cnt;
int vis[maxn];
int deg[maxn];
int head[maxn];
vector<int>ans[maxn];

struct node
{
    int v,id,next,flag;
}e[maxn*4];

void add(int u,int v,int id)
{
    e[cnt].v=v;
    e[cnt].id=id;
    e[cnt].flag=0;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

void dfs(int u)
{
    vis[u]=1;
    for(int i=head[u];~i;i=e[i].next)
    {
        int v=e[i].v,id=e[i].id;
        if(e[i].flag==0)
        {
            e[i].flag=e[i^1].flag=1;
            dfs(v);
            if(id) ans[num].push_back(-id);
            else num++;
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<=n;i++)
        {
            deg[i]=vis[i]=0;
            head[i]=-1;
            ans[i].clear();
        }
        cnt=num=0;
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            deg[u]++,deg[v]++;
            add(u,v,i);
            add(v,u,-i);
        }
        int pre=0;
        for(int i=1;i<=n;i++)
        {
            if(deg[i]&1)
            {
                if(pre) add(pre,i,0),add(i,pre,0),pre=0;
                else pre=i;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&(deg[i]&1)) num++,dfs(i),num--;
        }
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&deg[i]) num++,dfs(i);
        }
        printf("%d\n",num);
        for(int i=1;i<=num;i++)
        {
            printf("%d",ans[i].size());
            for(int j=0;j<ans[i].size();j++) printf(" %d",ans[i][j]);
            puts("");
        }
    }
}
发布了259 篇原创文章 · 获赞 100 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/my_sunshine26/article/details/81263616