Submission #1356163


Source Code Expand

// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 1e5 + 5;

struct Edge {
    int v, next;
    Edge() { }
    Edge(int v_, int next_): v(v_), next(next_) { }
};

int N, M;
int tote, head[MAXN];
Edge edge[MAXN * 2 + 5];

inline void addEdge(int u, int v)
{
    edge[++tote] = Edge(v, head[u]);
    head[u] = tote;
}

void input()
{
    read(N); read(M);
    tote = 1;
    for (int i = 1; i <= M; ++i) {
        int u, v;
        read(u); read(v);
        addEdge(u, v);
        addEdge(v, u);
    }
}

int dep[MAXN], fa[MAXN], size[MAXN], tot[MAXN];
int S, T;

void dfs(int u)
{
    dep[u] = dep[fa[u]] + 1;
    size[u] = 1;
    tot[u] = dep[u] & 1;
    for (int i = head[u]; i; i = edge[i].next) {
        int v = edge[i].v;
        if (v == fa[u]) continue;
        if (dep[v]) {
            S = u;
            T = v;
        } else {
            fa[v] = u;
            dfs(v);
            size[u] += size[v];
            tot[u] += tot[v];
        }
    }
}

void solve()
{
    dfs(1);

    if (!S) {                   // tree
        if (size[1] - tot[1] * 2 != 0) {
            puts("-1");
            return;
        }

        LL ans = 0;
        for (int u = 1; u <= N; ++u) {
            ans += std::abs(size[u] - tot[u] * 2);
            // printf("%d: contrib %d\n", u, std::abs(size[u] - tot[u] * 2));
        }
        printf("%lld\n", ans);

    } else {
        memset(fa, 0, sizeof(fa));
        memset(dep, 0, sizeof(dep));
        int S0 = S;
        dfs(S);
        if (S != S0)
            std::swap(S, T);
        assert(S == S0);

        if (dep[S] % 2 == dep[T] % 2) { // odd cycle
            if ((size[S] - tot[S] * 2) % 2 != 0) {
                puts("-1");
                return;
            }

            // printf("S=%d, T=%d\n", S, T);
            int d = (size[S] - tot[S] * 2) / 2;
            LL ans = abs(d);
            for (int u = 1; u <= N; ++u) {
                if (u != S)
                    ans += abs(size[u] - tot[u] * 2);
            }
            for (int u = T; u != S; u = fa[u]) {
                ans -= abs(size[u] - tot[u] * 2);
                ans += abs(size[u] - tot[u] * 2 - d);
            }

            printf("%lld\n", ans);

        } else {                    // even cycle
            if (size[S] - tot[S] * 2 != 0) {
                puts("-1");
                return;
            }

            vector<int> list;
            list.push_back(0);
            for (int u = T; u != S; u = fa[u]) {
                // printf("%d: %d\n", u, size[u] - tot[u] * 2);
                list.push_back(size[u] - tot[u] * 2);
            }
            std::sort(ALL(list));
            int x = list[SZ(list) / 2];
            // printf("S=%d, T=%d, x=%d\n", S, T, x);

            LL ans = abs(x);
            for (int u = 1; u <= N; ++u) {
                ans += abs(size[u] - tot[u] * 2);
            }
            for (int u = T; u != S; u = fa[u]) {
                ans -= abs(size[u] - tot[u] * 2);
                ans += abs(size[u] - tot[u] * 2 - x);
            }
            printf("%lld\n", ans);
        }
    }
}

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 子曰:“学而不思则罔,思而不学则殆。”
// --论语,为政篇

Submission Info

Submission Time
Task F - Namori
User Ivlleiooq
Language C++14 (GCC 5.4.1)
Score 2200
Code Size 5510 Byte
Status AC
Exec Time 28 ms
Memory 9084 KB

Judge Result

Set Name Sample Subtask1 All
Score / Max Score 0 / 0 1500 / 1500 700 / 700
Status
AC × 4
AC × 20
AC × 66
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
Subtask1 0_00.txt, 0_01.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 2_00.txt, 2_01.txt, 2_02.txt, 2_03.txt, 2_04.txt, 2_05.txt, 2_06.txt, 2_07.txt, 2_08.txt, 2_09.txt, 2_10.txt, 2_11.txt, 2_12.txt, 2_13.txt, 2_14.txt, 2_15.txt, 2_16.txt, 2_17.txt, 2_18.txt, 2_19.txt, 2_20.txt, 2_21.txt, 2_22.txt, 2_23.txt, 2_24.txt, 2_25.txt, 2_26.txt, 2_27.txt, 2_28.txt, 2_29.txt, 2_30.txt, 2_31.txt, 2_32.txt, 2_33.txt, 2_34.txt, 2_35.txt, 2_36.txt, 2_37.txt, 2_38.txt, 2_39.txt, 2_40.txt, 2_41.txt, 2_42.txt, 2_43.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 256 KB
0_01.txt AC 1 ms 256 KB
0_02.txt AC 2 ms 1024 KB
0_03.txt AC 2 ms 1024 KB
1_00.txt AC 1 ms 256 KB
1_01.txt AC 16 ms 8320 KB
1_02.txt AC 12 ms 4992 KB
1_03.txt AC 8 ms 3712 KB
1_04.txt AC 14 ms 5760 KB
1_05.txt AC 14 ms 5632 KB
1_06.txt AC 14 ms 5632 KB
1_07.txt AC 14 ms 5120 KB
1_08.txt AC 14 ms 6016 KB
1_09.txt AC 10 ms 3712 KB
1_10.txt AC 11 ms 3712 KB
1_11.txt AC 11 ms 3712 KB
1_12.txt AC 12 ms 3712 KB
1_13.txt AC 12 ms 4096 KB
1_14.txt AC 12 ms 3840 KB
1_15.txt AC 12 ms 3712 KB
1_16.txt AC 12 ms 3712 KB
1_17.txt AC 12 ms 3712 KB
2_00.txt AC 2 ms 1024 KB
2_01.txt AC 28 ms 9084 KB
2_02.txt AC 19 ms 6528 KB
2_03.txt AC 10 ms 3712 KB
2_04.txt AC 24 ms 6528 KB
2_05.txt AC 24 ms 6528 KB
2_06.txt AC 24 ms 6528 KB
2_07.txt AC 24 ms 6528 KB
2_08.txt AC 24 ms 6528 KB
2_09.txt AC 14 ms 3712 KB
2_10.txt AC 15 ms 3712 KB
2_11.txt AC 16 ms 3712 KB
2_12.txt AC 17 ms 3712 KB
2_13.txt AC 20 ms 5888 KB
2_14.txt AC 17 ms 3968 KB
2_15.txt AC 17 ms 3840 KB
2_16.txt AC 17 ms 3712 KB
2_17.txt AC 17 ms 3712 KB
2_18.txt AC 2 ms 1024 KB
2_19.txt AC 17 ms 6144 KB
2_20.txt AC 9 ms 3712 KB
2_21.txt AC 28 ms 9084 KB
2_22.txt AC 20 ms 6144 KB
2_23.txt AC 21 ms 6144 KB
2_24.txt AC 22 ms 6144 KB
2_25.txt AC 20 ms 6144 KB
2_26.txt AC 22 ms 6144 KB
2_27.txt AC 15 ms 3712 KB
2_28.txt AC 16 ms 3712 KB
2_29.txt AC 16 ms 3712 KB
2_30.txt AC 17 ms 3840 KB
2_31.txt AC 20 ms 5376 KB
2_32.txt AC 17 ms 3968 KB
2_33.txt AC 17 ms 3840 KB
2_34.txt AC 17 ms 3712 KB
2_35.txt AC 17 ms 3712 KB
2_36.txt AC 11 ms 3712 KB
2_37.txt AC 10 ms 3712 KB
2_38.txt AC 10 ms 3712 KB
2_39.txt AC 10 ms 3712 KB
2_40.txt AC 10 ms 3712 KB
2_41.txt AC 10 ms 3712 KB
2_42.txt AC 10 ms 3840 KB
2_43.txt AC 10 ms 3712 KB