HDU 6311 2018HDU多校赛第二场 Cover(欧拉回路)

Cover

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1297    Accepted Submission(s): 252
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

Source

2018 Multi-University Training Contest 2

大致题意:给你一个图,不一定连通,问最少用几笔可以把它一笔画画完,要求输出对应的路径。

吐槽一下,这个题目描述的英语吧,现场的时候根本读不懂题目……一笔画问题就是欧拉路或者欧拉回路问题。根据定理一个连通图如果奇点个数大于2则不能一笔画,具体需要的次数是\frac{n}{2},其中这个n表示度为奇数的点的个数。那么问题的关键就是如何找这个路径了。

对于需要\frac{n}{2}个路径才可以覆盖的图,我们考虑把这\frac{n}{2}条路径的首尾两两相连,那么最后这样加了边之后的图就一定能够用一条路径遍历所有的边。也即,对于有n个奇点的图,我们把其中n-2个点两两相连,增加\frac{n-2}{2}条边,如此一来图中只有两个奇点,存在欧拉路,于是我们再把这条路径找出来。最后把路径中后增加的\frac{n-2}{2}条边去掉,就可以把整个路径分成\frac{n}{2}部分,对应\frac{n}{2}条路,这就是我们需要的结果。

需要注意的,是这个初始给的图不一定是一个连通图,因此还有特殊处理。另外可能存在没有度的点,这些也要特殊处理。欧拉回路的模板也要注意不要看错了。具体见代码:

#include<bits/stdc++.h>
#define N 200010

using namespace std;

struct Edge
{
    int y,dir;
};

vector<int> p,res[N];
vector<Edge> g[N];
bool v[N],vis[N];
int d[N],n,m,t;

void dfs(int x)
{
    vis[x]=1;
    if (d[x]&1) p.push_back(x);
    for(int i=0;i<g[x].size();i++)
    {
        if (vis[g[x][i].y]) continue;
        dfs(g[x][i].y);
    }
}

void get(int x)
{
    for(int i=0;i<g[x].size();i++)
    {
        if (v[abs(g[x][i].dir)]) continue;
        v[abs(g[x][i].dir)]=1;
        get(g[x][i].y);
        if (abs(g[x][i].dir)>m) t++;
            else res[t].push_back(-g[x][i].dir);

    }
}

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    while(cin>>n>>m)
    {
        t=0;
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++)
        {
            res[i].clear();
            g[i].clear();
            d[i]=vis[i]=0;
        }
        for(int i=1;i<=m;i++)
        {
            int x,y;
            cin>>x>>y;
            d[x]++; d[y]++; v[i]=0;
            g[x].push_back(Edge{y,i});
            g[y].push_back(Edge{x,-i});
        }
        int tt=m;
        for(int i=1;i<=n;i++)
        {
            if (!vis[i]&&d[i])
            {
                p.clear(); dfs(i); t++;
                if (p.size()==0) p.push_back(i);
                for(int j=2;j<p.size();j+=2)
                {
                    g[p[j]].push_back(Edge{p[j+1],++tt});
                    g[p[j+1]].push_back(Edge{p[j],-tt});
                }
                get(p[0]);
            }
        }
        cout<<t<<endl;
        for(int i=1;i<=t;i++)
        {
            cout<<res[i].size();
            for(int j=0;j<res[i].size();j++)
                cout<<' '<<res[i][j];
            cout<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013534123/article/details/81236431
今日推荐