Live2d Test Env

HDU - 6311:Cover(欧拉回路,最少的一笔画覆盖无向图)

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.

InputThere 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.
1n,m100000
OutputFor 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

 

思路:把奇点配对,然后求欧拉回路。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=500010;
int Laxt[maxn],Next[maxn<<1],To[maxn<<1],cnt,num;
int x[maxn],y[maxn],ind[maxn]; bool used[maxn],vis[maxn<<1];
vector<int>G[maxn];  int tot,M;
void add(int u,int v)
{
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; vis[cnt]=0;
}
void dfs(int u,int f)
{
    used[u]=1;
    for(int &i=Laxt[u];i;i=Next[i]){
        if(!vis[i]){
            vis[i]=vis[i^1]=1;
            dfs(To[i],i);
        }
    }
    if(!f) return ;
    if(f<=(M<<1|1)) G[tot].push_back(f&1?(f>>1):-(f>>1));
    else tot++;
}
int main()
{
    int N;
    while(~scanf("%d%d",&N,&M)){
      rep(i,1,M) scanf("%d%d",&x[i],&y[i]);
      cnt=1; tot=0;
      rep(i,1,N) Laxt[i]=used[i]=ind[i]=0;
      rep(i,1,M) {
         add(x[i],y[i]); add(y[i],x[i]);
         ind[x[i]]++; ind[y[i]]++;
      }
      int x=0;
      rep(i,1,N) {
          if(ind[i]&1){
             if(x) add(x,i), add(i,x),x=0;
             else x=i;
          }
      }
      rep(i,1,N) if(!used[i]&&(ind[i]&1)){
          tot++; dfs(i,0); tot--;
      }
      rep(i,1,N) if(!used[i]&&ind[i]) {
          tot++; dfs(i,0);
      }
      printf("%d\n",tot);
      rep(i,1,tot) {
         printf("%d",G[i].size()); int L=G[i].size();
         rep(j,0,L-1) printf(" %d",G[i][j]); puts("");
      }
      rep(i,1,tot) G[i].clear();
    }
    return 0;
}

 

posted @ 2018-12-27 10:29  nimphy  阅读(295)  评论(0编辑  收藏  举报