HDU 6311 Cover (无向图最小路径覆盖)

HDU 6311 Cover (无向图最小路径覆盖)

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

题目解析

题意是问一个图最少几笔能够画完,把每一笔输出出来。

有一个规律很容易发现,就是一个不存在欧拉回路的图,需要奇点数/2笔画完,具体是怎么操作的呢

  1. 建图
  2. 每两个奇点之间连一条边
  3. 从原奇点开始,DFS欧拉回路,删去加进来的边,得到结果
  4. DFS剩下的联通块,得到结果

代码

#include<iostream>
#include<vector>
#include<cstring>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 100010;
struct Edge
{
    int from,to,next;
    int num;
    int d;
}edge[maxn<<4];
int vis[maxn<<4];
int head[maxn];
int du[maxn];
int tol;
int n,m,cnt;
vector<int> st;
vector<int> ans[maxn];

void init()
{
    cnt = 1;
    tol = 0;
    CLR(head,-1);
    CLR(vis,0);
    CLR(du,0);
    for(int i = 1; i <= maxn; i++)
        ans[i].clear();
    st.clear();
}

void addedge(int u,int v,int num,int d)
{
    edge[tol].from = u;
    edge[tol].to = v;
    edge[tol].num = num;
    edge[tol].d = d;
    edge[tol].next = head[u];
    head[u] = tol++;
}

void dfs(int st)
{
    for(int i = head[st]; i!=-1; i = edge[i].next){
        if( vis[edge[i].num]) continue;
        vis[edge[i].num] = 1;

        dfs(edge[i].to);
        if(edge[i].d == 0){
            cnt++;
        }
        else{
            ans[cnt].push_back(edge[i].d*edge[i].num*-1);
        }
    }
}


int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        for(int i = 1; i <= m; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v,i,1);
            addedge(v,u,i,-1);
            du[v]++;
            du[u]++;
        }

        int sig = m;
        int tmp = 0;
        for(int i = 1; i <= n; i++){
            if(tmp == 0 && du[i]%2 == 1){
                tmp = i;
                du[tmp]++;
            }
            else if(tmp && du[i]%2 == 1){
                addedge(i,tmp,++sig,0);
                addedge(tmp,i,sig,0);
                du[i]++;
                st.push_back(tmp);
                tmp = 0;
            }
        }

        for(int i = 0; i < st.size(); i++){
            dfs(st[i]);
        }
        for(int i = 0; i < tol; i++){
            if(!vis[edge[i].num]){
                dfs(edge[i].from);
                cnt++;
            }
        }

        printf("%d\n",cnt-1);
        for(int i = 1; i < cnt; i++){
            printf("%d ",ans[i].size());
            for(int j = 0; j < ans[i].size(); j++){
                printf("%d ",ans[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
posted @ 2018-07-30 11:08  Kurumi33  阅读(380)  评论(0编辑  收藏  举报