Submission #83253

# Submission time Handle Problem Language Result Execution time Memory
83253 2018-11-06T12:16:07 Z teomrn Pipes (BOI13_pipes) C++14
0 / 100
462 ms 132096 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
const int NMAX = 100010;

vector <pair <int, int>> adia[NMAX + 10];
int tata[NMAX + 10];
int taken[NMAX + 10];
int h[NMAX + 10];
bool ok = 1;
i64 ce_s_vrea[NMAX + 10];
int ans[5 * NMAX + 10];

vector <vector <int>> centers;

bool dfs(int nod, bool did)
{
    for (auto i : adia[nod]) {
        if (i.first == tata[nod])
            continue;
        if (tata[i.first]) {
            if (h[i.first] > h[nod])
                continue;
            if (did) {
                ok = 0;
                return 1;
            }
            vector <int> c;
            for (int j = nod; j != tata[i.first]; j = tata[j]) {
                if (taken[j]) {
                    ok = 0;
                    return 1;
                }
                taken[j] = 1;
                c.push_back(j);
            }
            centers.push_back(c);
            did = 1;
        }
        else {
            tata[i.first] = nod;
            h[i.first] = 1 + h[nod];
            did |= dfs(i.first, did);
            if (!ok)
                return 1;
        }
    }
    return did;
}

int solve_arb(int nod, int tata)
{
    int ce_tata;
    for (auto i : adia[nod]) {
        if (i.first != tata) {
            ans[i.second] = solve_arb(i.first, nod);
            ce_s_vrea[nod] -= ans[i.second];
        }
    }
    return ce_s_vrea[nod];
}

void solve_ciclu(vector <int> ciclu)
{
    vector <int> a(ciclu.size());
    vector <i64> s_st(ciclu.size()), s_dr(ciclu.size());

    int n = ciclu.size();
    for (int i = 0; i < n; i++) {
        for (auto j : adia[ciclu[i]]) {
            if (j.first != ciclu[(i + 1) % n] && j.first != ciclu[(i + n - 1) % n]) {
                int x = solve_arb(j.first, ciclu[i]);
                ce_s_vrea[ciclu[i]] -= x;
                ans[j.second] = x;
            }
            else if (j.first == ciclu[(i + n - 1) % n])
                a[i] = j.second;
        }
    }
    i64 s_total = 0;
    for (auto i : ciclu)
        s_total += ce_s_vrea[i];

    if ((s_total & 1LL) || s_total && n == 1) {
        ok = 0;
        return;
    }

    s_st[0] = ce_s_vrea[ciclu[0]];
    for (int i = 1; i < n; i++)
        s_st[i] = ce_s_vrea[ciclu[i]] - s_st[i - 1];
    s_dr[n - 1] = ce_s_vrea[ciclu[n - 1]];
    for (int i = n - 2; i >= 0; i--)
        s_dr[i] = ce_s_vrea[ciclu[i]] - s_dr[i + 1];

    for (int i = 0; i < n; i++)
        ans[a[i]] = (s_st[i] + s_dr[i] - ce_s_vrea[ciclu[i]]) / 2;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m, a, b;

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> ce_s_vrea[i];

    for (int i = 1; i <= m; i++) {
        cin >> a >> b;
        adia[a].push_back({ b, i });
        adia[b].push_back({ a, i });
    }

    for (int i = 1; i <= n; i++) {
        if (!tata[i]) {
            tata[i] = -1;
            auto x = dfs(i, 0);
            if (!x)
            centers.push_back({ i });
        }
    }

    for (auto i : centers)
        solve_ciclu(i);



    if (!ok) {
        cout << 0 << '\n';
        return 0;
    }

    for (int i = 1; i <= m; i++)
        cout << ans[i] << '\n';

    return 0;
}

Compilation message

pipes.cpp: In function 'int solve_arb(int, int)':
pipes.cpp:54:9: warning: unused variable 'ce_tata' [-Wunused-variable]
     int ce_tata;
         ^~~~~~~
pipes.cpp: In function 'void solve_ciclu(std::vector<int>)':
pipes.cpp:85:36: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     if ((s_total & 1LL) || s_total && n == 1) {
                            ~~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2808 KB Output isn't correct
2 Incorrect 5 ms 2808 KB Output isn't correct
3 Incorrect 4 ms 2976 KB Output isn't correct
4 Incorrect 75 ms 10636 KB Output isn't correct
5 Incorrect 4 ms 10636 KB Output isn't correct
6 Incorrect 4 ms 10636 KB Output isn't correct
7 Incorrect 4 ms 10636 KB Output isn't correct
8 Incorrect 4 ms 10636 KB Output isn't correct
9 Incorrect 4 ms 10636 KB Output isn't correct
10 Incorrect 4 ms 10636 KB Output isn't correct
11 Incorrect 4 ms 10636 KB Output isn't correct
12 Incorrect 5 ms 10636 KB Output isn't correct
13 Incorrect 63 ms 10912 KB Output isn't correct
14 Incorrect 92 ms 13408 KB Output isn't correct
15 Incorrect 97 ms 15444 KB Output isn't correct
16 Incorrect 68 ms 15708 KB Output isn't correct
17 Incorrect 81 ms 18192 KB Output isn't correct
18 Incorrect 79 ms 19716 KB Output isn't correct
19 Incorrect 96 ms 27732 KB Output isn't correct
20 Incorrect 5 ms 27732 KB Output isn't correct
21 Incorrect 4 ms 27732 KB Output isn't correct
22 Incorrect 101 ms 27732 KB Output isn't correct
23 Incorrect 68 ms 27732 KB Output isn't correct
24 Incorrect 79 ms 27732 KB Output isn't correct
25 Incorrect 67 ms 27732 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 27732 KB Output isn't correct
2 Incorrect 5 ms 27732 KB Output isn't correct
3 Incorrect 87 ms 35784 KB Output isn't correct
4 Runtime error 433 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
5 Runtime error 228 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
6 Runtime error 450 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
7 Runtime error 4 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
8 Runtime error 4 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
9 Runtime error 4 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
10 Runtime error 126 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
11 Runtime error 122 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
12 Runtime error 125 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
13 Runtime error 119 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
14 Runtime error 4 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
15 Runtime error 5 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
16 Runtime error 5 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
17 Runtime error 4 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
18 Runtime error 144 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
19 Runtime error 134 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
20 Runtime error 126 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
21 Runtime error 126 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
22 Runtime error 5 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
23 Runtime error 74 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
24 Runtime error 86 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
25 Runtime error 85 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
26 Runtime error 323 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
27 Runtime error 280 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
28 Runtime error 185 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
29 Runtime error 321 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
30 Runtime error 94 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
31 Runtime error 96 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
32 Runtime error 82 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
33 Runtime error 90 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
34 Runtime error 317 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
35 Runtime error 462 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
36 Runtime error 225 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
37 Runtime error 407 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
38 Runtime error 146 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
39 Runtime error 106 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
40 Runtime error 113 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
41 Runtime error 115 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
42 Runtime error 359 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
43 Runtime error 300 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
44 Runtime error 249 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
45 Runtime error 334 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
46 Runtime error 116 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
47 Runtime error 99 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
48 Runtime error 115 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
49 Runtime error 81 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.
50 Runtime error 349 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
51 Runtime error 337 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
52 Runtime error 326 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
53 Runtime error 335 ms 132096 KB Execution killed with signal 9 (could be triggered by violating memory limits)
54 Runtime error 103 ms 132096 KB Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience.