HDU 6311 Cover 欧拉回路记录路径 2018杭电多校第二场

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

 

题意:首先给出一个n个点, m条边的无向图(该图不一定联通), 让你输出最少的路径使得这些路径可以覆盖所有的边。

 

思路:看了dls的讲解后才恍然大悟, 首先我们先来考虑一种特殊的图, 欧拉图, 对于欧拉图, 我们是可以一笔画出这个图, 也就是说只用一条路径就能覆盖掉所有的边, 那么我们根据贪心的思路来想, 我们不就可以将问题转化成再向这个图中加入多少边, 使得这个图可以变成欧拉图而一笔画出来就好了, 因此, 我们就直接统计点的度数, 对于一个联通块内的点, 我们在奇数度数的点之间连边(如果奇数度数的点大于等于2我们要空出两个奇数点, 并以这两个点为起始点和终止点), 之后我们在输出的时候每碰到一个添加的边就把路径数加一就好了

这个题的细节巨难处理。。。。。。真的麻烦。。。不愧多校。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

const int maxed = 100000 + 10;

struct E
{
    int v, bef, id, k;
    bool flag;
}e[maxed * 4];

std::vector<int> answer[maxed];
int n, m, ans, head[maxed], in[maxed];
int odd_cnt, odd[maxed];
int cnt;
bool vis[maxed];

int main()
{
    void add_(int x, int y, int id, int k);
    void slove_0(int u);
    void slove(int u);
    while (scanf("%d%d", &n, &m) != EOF) {
        for (int i = 0; i < maxed; ++i)
            answer[i].clear();
        memset(in, 0, sizeof(in));
        ans = 0;
        memset(head, -1, sizeof(head));
        int a, b;
        for (int i = 1; i <= m; ++i) {
            scanf("%d%d", &a, &b);
            add_(a, b, i, 1);
            add_(b, a, i, -1);
            in[a] += 1, in[b] += 1;
        }
        cnt = 1;
        memset(vis, false, sizeof(vis));
        for (int i = 1; i <= n; ++i)
            if (!vis[i]) {
                odd_cnt = 0;
                slove_0(i);
                int s = i;
                if (odd_cnt) {
                    if (odd_cnt > 2) {
                        for (int i = 2; i < odd_cnt; i += 2) {
                            add_(odd[i], odd[i + 1], 0, 0);
                            add_(odd[i + 1], odd[i], 0, 0);
                        }
                    }
                    s = odd[0];
                }
                if (answer[cnt].size())
                    cnt += 1;
                slove(s);
            }
        int sum = 0;
        for (int i = 1; i <= cnt; ++i)
            if (answer[i].size())
                sum += 1;
        printf("%d\n", sum);
        for (int i = 1; i <= cnt; ++i) {
            int len = answer[i].size();
            if (len) {
                printf("%d", len);
                for (int j = 0; j < len; ++j)
                    printf(" %d", answer[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}

void add_(int x, int y, int id, int k)
{
    e[ans].v = y, e[ans].id = id;
    e[ans].k = k, e[ans].flag = false;
    e[ans].bef = head[x];
    head[x] = ans++;
}

void slove_0(int u)
{
    vis[u] = true;
    if (in[u] % 2)
        odd[odd_cnt++] = u;
    for (int i = head[u]; i != -1; i = e[i].bef) {
        int v = e[i].v;
        if (!vis[v])
            slove_0(v);
    }
}

void slove(int u)
{
    for (int i = head[u]; i != -1; i = e[i].bef)
        if (!e[i].flag) {
            e[i].flag = true;
            e[i ^ 1].flag = true;
            slove(e[i].v);
            if (e[i].k == 0) {
                if (answer[cnt].size())
                    cnt += 1;
            }
            else
                answer[cnt].push_back(-e[i].id * e[i].k);
        }
}

/*
9 11
1 2
1 3
2 3
3 4
4 5
4 6
5 6
5 7
7 8
7 9
8 9
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值