Submission #935904


Source Code Expand

#include<bits/stdc++.h>

using namespace std;

int A, B, nr1, N, M, sub[100009][2], col[100009], ap[100009], sz[100009];
long long currCost, ans;
vector < int > v[100009];

void dfsTree (int nod, int tata)
{
    sub[nod][col[nod]] = 1;
    for (auto it = v[nod].begin (); it != v[nod].end (); it ++)
        if (*it != tata)
            col[*it] = col[nod] ^ 1, dfsTree (*it, nod), sub[nod][0] += sub[*it][0], sub[nod][1] += sub[*it][1];
    int mod = sub[nod][0] - sub[nod][1];
    if (mod < 0) mod = -mod;
    ans += mod;
}

void dfs (int nod, int tata)
{
    ap[nod] = 1, sub[nod][0] = (col[nod] == 0), nr1 += (col[nod] == 1);
    for (auto it = v[nod].begin (); it != v[nod].end (); it ++)
        if (ap[*it] == 0) col[*it] = col[nod] ^ 1, dfs (*it, nod), sub[nod][0] += sub[*it][0];
        else
        if (*it != tata) A = nod, B = *it;
}

void dfsEven (int nod, int tata)
{
    ap[nod] = 1;
    for (auto it = v[nod].begin (); it != v[nod].end (); it ++)
        if (ap[*it] == 0) dfsEven (*it, nod), sz[nod] += sz[*it];
    int mod = sub[nod][0] - sz[nod];
    if (mod < 0) mod = -mod;
    currCost += mod;
}

void dfsOdd (int nod, int tata)
{
    ap[nod] = 1;
    for (auto it = v[nod].begin (); it != v[nod].end (); it ++)
        if (ap[*it] == 0) dfsOdd (*it, nod), sz[nod] += sz[*it];
    int mod = sub[nod][0] - sz[nod];
    if (mod < 0) mod = -mod;
    ans += mod;
}

long long EvenF (int F)
{
    for (int i=1; i<=N; i++)
        sz[i] = col[i], ap[i] = 0;
    sz[A] += F, sz[B] -= F, currCost = F;
    if (F < 0) currCost = -F;
    dfsEven (1, -1);
//    printf ("%d -> %lld\n", F, currCost);
    if (currCost < ans) ans = currCost;
    return currCost;
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d %d\n", &N, &M);
for (int i=1; i<=M; i++)
{
    int x, y;
    scanf ("%d %d", &x, &y);
    v[x].push_back (y);
    v[y].push_back (x);
}
if (M == N - 1)
{
    dfsTree (1, -1);
    if (sub[1][0] != sub[1][1]) printf ("-1\n");
    else printf ("%lld\n", ans);
    return 0;
}
dfs (1, -1);
if (col[A] != col[B])
{
    ///Even cycle
    if (sub[1][0] != nr1)
    {
        printf ("-1\n");
        return 0;
    }
    ///let F pe the flow on edge B->A with -nr1 <= F <= nr1, we can just use the same algo as for tree,
    ///increasing the number of tokens in A by F and decreasing it in B
    ///thie way, we'll get a sum of expressions of the form |F - constant| + some constant that we need to minimize
    ///there are multiple ways of doing it(F is the median of the constants for example), but the easiest to implement
    ///is ternary search
    int st = -nr1, dr = nr1;
    ans = 1LL << 50;
    while (1)
    {
        if (dr - st <= 2)
        {
            for (int i=st; i<=dr; i++)
                EvenF (i);
            break;
        }
        int mid1 = st + (dr - st) / 3, mid2 = dr - (dr - st) / 3;
        long long C1 = EvenF (mid1), C2 = EvenF (mid2);
        if (C1 < C2) dr = mid2;
        else st = mid1;
    }
    printf ("%lld\n", ans);
    return 0;
}
///Odd cycle
if (sub[1][0] % 2 != nr1 % 2)
{
    printf ("-1\n");
    return 0;
}
int K = (sub[1][0] - nr1) / 2;
for (int i=1; i<=N; i++)
    sz[i] = col[i], ap[i] = 0;
sz[A] += K, sz[B] += K, ans = K;
if (ans < 0) ans = -ans;
dfsOdd (1, -1);
printf ("%lld\n", ans);
return 0;
}

Submission Info

Submission Time
Task F - Namori
User geniucos
Language C++14 (GCC 5.4.1)
Score 2200
Code Size 3450 Byte
Status AC
Exec Time 564 ms
Memory 12288 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:66:26: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 scanf ("%d %d\n", &N, &M);
                          ^
./Main.cpp:70:28: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf ("%d %d", &x, &y);
                            ^

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 5 ms 2560 KB
0_01.txt AC 5 ms 2560 KB
0_02.txt AC 5 ms 2560 KB
0_03.txt AC 5 ms 2560 KB
1_00.txt AC 5 ms 2560 KB
1_01.txt AC 55 ms 11392 KB
1_02.txt AC 44 ms 8320 KB
1_03.txt AC 39 ms 7288 KB
1_04.txt AC 50 ms 9088 KB
1_05.txt AC 50 ms 8832 KB
1_06.txt AC 50 ms 8832 KB
1_07.txt AC 49 ms 8320 KB
1_08.txt AC 51 ms 9216 KB
1_09.txt AC 45 ms 7296 KB
1_10.txt AC 47 ms 7040 KB
1_11.txt AC 48 ms 7040 KB
1_12.txt AC 51 ms 6912 KB
1_13.txt AC 49 ms 7296 KB
1_14.txt AC 48 ms 7040 KB
1_15.txt AC 50 ms 6912 KB
1_16.txt AC 49 ms 6912 KB
1_17.txt AC 48 ms 6912 KB
2_00.txt AC 6 ms 2560 KB
2_01.txt AC 540 ms 12288 KB
2_02.txt AC 332 ms 10240 KB
2_03.txt AC 140 ms 8056 KB
2_04.txt AC 395 ms 10112 KB
2_05.txt AC 412 ms 10112 KB
2_06.txt AC 439 ms 9984 KB
2_07.txt AC 411 ms 10112 KB
2_08.txt AC 438 ms 9984 KB
2_09.txt AC 227 ms 7936 KB
2_10.txt AC 289 ms 7808 KB
2_11.txt AC 338 ms 7808 KB
2_12.txt AC 372 ms 7680 KB
2_13.txt AC 55 ms 9344 KB
2_14.txt AC 49 ms 7552 KB
2_15.txt AC 376 ms 7680 KB
2_16.txt AC 50 ms 7296 KB
2_17.txt AC 374 ms 7680 KB
2_18.txt AC 5 ms 2560 KB
2_19.txt AC 57 ms 10236 KB
2_20.txt AC 42 ms 8056 KB
2_21.txt AC 564 ms 12288 KB
2_22.txt AC 57 ms 10240 KB
2_23.txt AC 58 ms 10112 KB
2_24.txt AC 59 ms 9984 KB
2_25.txt AC 56 ms 10240 KB
2_26.txt AC 59 ms 9984 KB
2_27.txt AC 48 ms 8064 KB
2_28.txt AC 51 ms 7808 KB
2_29.txt AC 53 ms 7808 KB
2_30.txt AC 54 ms 7680 KB
2_31.txt AC 51 ms 8832 KB
2_32.txt AC 55 ms 7808 KB
2_33.txt AC 54 ms 7680 KB
2_34.txt AC 49 ms 7296 KB
2_35.txt AC 54 ms 7680 KB
2_36.txt AC 137 ms 8056 KB
2_37.txt AC 138 ms 8056 KB
2_38.txt AC 137 ms 8056 KB
2_39.txt AC 139 ms 8056 KB
2_40.txt AC 137 ms 8056 KB
2_41.txt AC 139 ms 8056 KB
2_42.txt AC 137 ms 8056 KB
2_43.txt AC 136 ms 8056 KB