hdu 6311 2018多校第二场C题(欧拉路径)

Cover

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

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 

本来很并不算难的一个题。越改越差劲。。

思路: 对于每一个联通块来考虑,如果该联通块所有点的入度全部为偶数,那么就直接可以dfs找欧拉路径。

如果有两个奇度顶点,其他的都为偶数,那么也可以选择任意的一个奇度顶点来跑欧拉路径。但是如果一个联通块的奇度顶点大于2,那么就需要对奇度顶点之间进行连边,然后选择一个奇度顶点跑欧拉路径,然后最后将多连的边删去就好了。

代码: 

#include<bits/stdc++.h>

using namespace std;
const int N =1e5+5;
struct node
{
    int v;
    int val;
};
vector< node >ve[N];
int vis[2*N];
int inde[N];
int n,m;
int book[N];
vector< int > ans;
vector< int >aa[N];
vector< int >p;

void init()
{
    memset(book,0,sizeof(book));
    memset(vis,0,sizeof(vis));
    memset(inde,0,sizeof(inde));
    for(int i=0; i<=n; i++) ve[i].clear();
}

/*
void dfs(int u)
{
    //cout<<"*** u "<<u<<endl;
    ss.push(u);
    for(int i=0;i<ve[u].size();i++){
        if(vis[abs(ve[u][i].val)]) continue;
        vis[ abs(ve[u][i].val) ]=1;
        sss.push(ve[u][i]);
        //cout<<"u "<<u<<" v "<<ve[u][i].v<<" val "<<ve[u][i].val<<endl;
        dfs(ve[u][i].v);
        break;
    }
}

void fleury(int st)
{
    ss.push(st);
    while(!ss.empty()){
        int f=0;
        int u=ss.top(); ss.pop();
        if(!sss.empty()){
            node tmp=sss.top(); sss.pop();
            ans.push_back(tmp.val);
        }
        for(int i=0;i<ve[u].size();i++){
            if(vis[abs(ve[u][i].val)]) continue;
            f=1; break;
        }
        if(!f){
            //cout<<"hahah "<<u<<" "<<endl;
        }
        else dfs(u);
    }
}
*/

void dfs1(int u,int fa)
{
    if(book[u]) return ;
    if(inde[u]&1) p.push_back(u);
    book[u]=1;
    for(int i=0; i<ve[u].size(); i++)
    {
        int v=ve[u][i].v;
        if(v==fa) continue;
        dfs1(v,u);
    }
}


void fleury(int u)
{
    int v,i;
    for(i=0; i<ve[u].size(); i++)
    {
        int in=abs(ve[u][i].val);
        if(!vis[in])
        {
            vis[in]=1;
            fleury(ve[u][i].v);
            ans.push_back(ve[u][i].val);
        }
    }
}



int main()
{
    int u,v;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init();
        node tmp;
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d",&u,&v);
            inde[v]++;
            inde[u]++;
            tmp.v=v;
            tmp.val=i;
            ve[u].push_back(tmp);
            tmp.v=u;
            tmp.val=-i;
            ve[v].push_back(tmp);
        }

        for(int i=0; i<=n; i++) aa[i].clear();

        int res=0;
        int lim=m;
        for (int i = 1; i <= n; i++) {
            if (book[i] || inde[i] == 0) continue;
            p.clear();
            dfs1(i,-1);
            for (int j = 2; j < p.size(); j += 2){
                tmp.v=p[j+1]; tmp.val=++lim;
                ve[p[j] ].push_back(tmp);
                tmp.v=p[j];  tmp.val=lim;
                ve[p[j+1] ].push_back(tmp);
            }
            ans.clear();

            int t = (p.empty() ? i : p[0]);
            fleury(t);

            for (int j = ans.size() - 1; j >= 0; j--) {
                if (ans[j]>m) continue;
                res++;
                while (ans[j] <= m&& j >= 0) {
                    aa[res].push_back(ans[j]);
                    j--;
                }
            }
        }

        cout<<res<<endl;
        for(int i=1; i<=res; i++)
        {
            cout<<aa[i].size();
            for(int j=0; j<aa[i].size(); j++)
            {
                cout<<" "<<aa[i][j];
            }
            cout<<endl;
        }

    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值