Submission #1077724

# Submission time Handle Problem Language Result Execution time Memory
1077724 2024-08-27T08:43:14 Z hung_nm Pipes (BOI13_pipes) C++17
67.5926 / 100
117 ms 50968 KB
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pipii pair<int, pair<int, int>>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define inp(name) if(fopen(name, "r")) freopen(name, "r", stdin);
#define out(name) if(fopen(name, "w")) freopen(name, "w", stdout);
#define int long long
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
using namespace std;

long long n, m, c[N], ans[N];
vpi ad[N];

void dfs(int u, int p, int id = 0)
{
    long long res = 0;
    for (auto [v, pos] : ad[u])
    {
        if (v == p) continue;
        dfs(v, u, pos);
        res += ans[pos];
    }
    ans[id] = c[u] - res;
}

void sub1()
{
    dfs(1, 0);
    for (int i = 1; i <= m; ++i)
    {
        cout << 2 * ans[i] << "\n";
    }
}

long long del[N], deg[N], ok[N], tmp, x, root, vis[N];
queue<int> q;

void dfs2(int u, int p, int dau)
{
    tmp += dau * c[u];
    ok[u] = 1;
    for (auto [v, i] : ad[u])
    {
        if (v == p || del[v]) continue;
        if (ok[v] == 0)
        {
            dfs2(v, u, dau * (-1));
        }
        else
        {
            ans[x] = tmp;
            root = v;
            vis[u] = 1;
            break;
        }
    }
}

void dfs3(int u = root, int p = root, int pos = x)
{
    vis[u] = 1;
    for (auto [v, i] : ad[u])
    {
        if (v == p || del[v] || vis[v]) continue;
        ans[i] = 2 * c[u] - ans[pos];
        dfs3(v, u, i);
    }
}

void sub2()
{
    for (int i = 1; i <= n; ++i)
    {
        if (ad[i].size() == 1)
        {
            q.push(i);
        }
        deg[i] = ad[i].size();
    }

    if ((n - q.size()) % 2 == 0)
    {
        cout << 0;
        return;
    }

    while (!q.empty())
    {
        int u = q.front(); q.pop();
        for (auto [v, i] : ad[u])
        {
            if (del[v]) continue;
            ans[i] = 2 * c[u];
            c[v] -= c[u];
            deg[v]--;
            if (deg[v] == 1)
            {
                q.push(v);
            }
        }
        del[u] = 1;
    }

    long long des = -1;
    for (int i = 1; i <= n; ++i)
    {
        if (!del[i])
        {
            des = i;
            break;
        }
    }

    if (des != -1)
    {
        dfs2(des, des, 1);
        dfs3();
    }

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

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        cin >> c[i];
    }

    for (int i = 1; i <= m; ++i)
    {
        int u, v;
        cin >> u >> v;
        ad[u].pb({v, i});
        ad[v].pb({u, i});
    }

    if (m > n)
    {
        cout << 0;
        return 0;
    }

    if (m == (n - 1))
    {
        sub1();
        return 0;
    }

    sub2();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 24152 KB Output is correct
2 Correct 6 ms 24152 KB Output is correct
3 Correct 6 ms 24236 KB Output is correct
4 Correct 43 ms 31316 KB Output is correct
5 Correct 5 ms 24152 KB Output is correct
6 Correct 7 ms 24156 KB Output is correct
7 Correct 7 ms 24156 KB Output is correct
8 Correct 6 ms 24156 KB Output is correct
9 Correct 6 ms 24272 KB Output is correct
10 Correct 6 ms 24156 KB Output is correct
11 Correct 7 ms 24156 KB Output is correct
12 Correct 6 ms 24412 KB Output is correct
13 Correct 39 ms 29968 KB Output is correct
14 Correct 38 ms 30916 KB Output is correct
15 Correct 40 ms 31312 KB Output is correct
16 Correct 36 ms 30292 KB Output is correct
17 Correct 40 ms 31324 KB Output is correct
18 Correct 40 ms 31312 KB Output is correct
19 Correct 49 ms 35136 KB Output is correct
20 Correct 6 ms 24156 KB Output is correct
21 Correct 5 ms 24156 KB Output is correct
22 Correct 44 ms 31316 KB Output is correct
23 Correct 32 ms 29780 KB Output is correct
24 Correct 44 ms 31400 KB Output is correct
25 Correct 34 ms 30112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 24152 KB Output isn't correct
2 Incorrect 6 ms 24156 KB Output isn't correct
3 Incorrect 55 ms 37600 KB Output isn't correct
4 Correct 36 ms 30044 KB Output is correct
5 Correct 43 ms 29856 KB Output is correct
6 Correct 117 ms 50768 KB Output is correct
7 Incorrect 6 ms 24156 KB Output isn't correct
8 Incorrect 6 ms 24156 KB Output isn't correct
9 Incorrect 9 ms 24156 KB Output isn't correct
10 Correct 5 ms 24156 KB Output is correct
11 Correct 6 ms 24156 KB Output is correct
12 Correct 6 ms 24156 KB Output is correct
13 Correct 6 ms 24156 KB Output is correct
14 Incorrect 5 ms 24156 KB Output isn't correct
15 Incorrect 6 ms 24408 KB Output isn't correct
16 Incorrect 6 ms 24412 KB Output isn't correct
17 Correct 7 ms 24156 KB Output is correct
18 Correct 7 ms 24248 KB Output is correct
19 Correct 6 ms 24156 KB Output is correct
20 Correct 6 ms 24184 KB Output is correct
21 Correct 6 ms 24408 KB Output is correct
22 Incorrect 6 ms 24152 KB Output isn't correct
23 Incorrect 51 ms 36692 KB Output isn't correct
24 Incorrect 54 ms 38736 KB Output isn't correct
25 Incorrect 47 ms 37712 KB Output isn't correct
26 Correct 38 ms 30288 KB Output is correct
27 Correct 37 ms 30032 KB Output is correct
28 Correct 36 ms 30348 KB Output is correct
29 Correct 104 ms 45516 KB Output is correct
30 Incorrect 51 ms 37716 KB Output isn't correct
31 Incorrect 62 ms 41328 KB Output isn't correct
32 Incorrect 32 ms 31320 KB Output isn't correct
33 Incorrect 49 ms 39108 KB Output isn't correct
34 Correct 45 ms 30156 KB Output is correct
35 Correct 34 ms 30036 KB Output is correct
36 Correct 30 ms 30192 KB Output is correct
37 Correct 109 ms 50968 KB Output is correct
38 Incorrect 45 ms 38220 KB Output isn't correct
39 Incorrect 45 ms 35668 KB Output isn't correct
40 Incorrect 52 ms 38480 KB Output isn't correct
41 Incorrect 60 ms 41412 KB Output isn't correct
42 Correct 34 ms 30032 KB Output is correct
43 Correct 40 ms 30036 KB Output is correct
44 Correct 28 ms 30044 KB Output is correct
45 Correct 89 ms 48268 KB Output is correct
46 Incorrect 37 ms 30800 KB Output isn't correct
47 Incorrect 31 ms 31068 KB Output isn't correct
48 Incorrect 36 ms 31080 KB Output isn't correct
49 Correct 34 ms 30812 KB Output is correct
50 Correct 32 ms 30032 KB Output is correct
51 Correct 40 ms 30044 KB Output is correct
52 Correct 32 ms 30136 KB Output is correct
53 Correct 117 ms 47444 KB Output is correct
54 Incorrect 48 ms 37460 KB Output isn't correct