답안 #1078157

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1078157 2024-08-27T13:25:38 Z hung_nm Pipes (BOI13_pipes) C++17
100 / 100
178 ms 57652 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, root2, id;
queue<int> q;

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

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

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

    while (!q.empty())
    {
        int u = q.front(); q.pop();
        del[u] = 1;
        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);
            }
        }
    }
    long long cnt = 0;
    for(int i = 1; i <= n; ++i)
    {
        if(!del[i])
        {
            root = i;
            cnt++;
        }
    }

    if(cnt % 2 == 0)
    {
        cout << 0;
        return ;
    }


    for(auto [x, i] : ad[root])
    {
        if(del[x]) continue ;
        root2 = x;
        id = i;
        break ;
    }

    ok[root] = 1;
    dfs2(root, root2, 1);
    memset(ok, 0, sizeof(ok));
    dfs3(root, root2, id);
    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});
        deg[u]++;
        deg[v]++;
    }

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

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

    sub2();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23900 KB Output is correct
2 Correct 11 ms 23900 KB Output is correct
3 Correct 11 ms 24028 KB Output is correct
4 Correct 51 ms 33360 KB Output is correct
5 Correct 11 ms 23900 KB Output is correct
6 Correct 11 ms 23900 KB Output is correct
7 Correct 12 ms 23900 KB Output is correct
8 Correct 10 ms 23980 KB Output is correct
9 Correct 12 ms 23900 KB Output is correct
10 Correct 15 ms 23964 KB Output is correct
11 Correct 11 ms 23900 KB Output is correct
12 Correct 11 ms 24104 KB Output is correct
13 Correct 41 ms 31316 KB Output is correct
14 Correct 52 ms 32852 KB Output is correct
15 Correct 53 ms 33364 KB Output is correct
16 Correct 44 ms 31824 KB Output is correct
17 Correct 56 ms 33368 KB Output is correct
18 Correct 48 ms 33424 KB Output is correct
19 Correct 51 ms 37172 KB Output is correct
20 Correct 11 ms 23900 KB Output is correct
21 Correct 10 ms 23900 KB Output is correct
22 Correct 50 ms 33332 KB Output is correct
23 Correct 45 ms 31316 KB Output is correct
24 Correct 49 ms 33268 KB Output is correct
25 Correct 41 ms 31824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 31580 KB Output is correct
2 Correct 15 ms 31864 KB Output is correct
3 Correct 47 ms 33308 KB Output is correct
4 Correct 41 ms 32080 KB Output is correct
5 Correct 48 ms 31828 KB Output is correct
6 Correct 139 ms 57652 KB Output is correct
7 Correct 14 ms 31576 KB Output is correct
8 Correct 13 ms 31632 KB Output is correct
9 Correct 11 ms 23900 KB Output is correct
10 Correct 12 ms 23900 KB Output is correct
11 Correct 11 ms 23860 KB Output is correct
12 Correct 11 ms 23900 KB Output is correct
13 Correct 11 ms 23900 KB Output is correct
14 Correct 15 ms 31580 KB Output is correct
15 Correct 12 ms 31980 KB Output is correct
16 Correct 14 ms 31832 KB Output is correct
17 Correct 12 ms 23896 KB Output is correct
18 Correct 11 ms 23896 KB Output is correct
19 Correct 12 ms 24056 KB Output is correct
20 Correct 11 ms 23900 KB Output is correct
21 Correct 11 ms 24156 KB Output is correct
22 Correct 15 ms 31836 KB Output is correct
23 Correct 54 ms 44136 KB Output is correct
24 Correct 69 ms 46420 KB Output is correct
25 Correct 53 ms 33364 KB Output is correct
26 Correct 42 ms 32308 KB Output is correct
27 Correct 44 ms 32084 KB Output is correct
28 Correct 44 ms 32592 KB Output is correct
29 Correct 115 ms 51024 KB Output is correct
30 Correct 67 ms 45140 KB Output is correct
31 Correct 67 ms 48976 KB Output is correct
32 Correct 57 ms 43572 KB Output is correct
33 Correct 45 ms 33876 KB Output is correct
34 Correct 42 ms 32088 KB Output is correct
35 Correct 40 ms 32080 KB Output is correct
36 Correct 43 ms 32340 KB Output is correct
37 Correct 178 ms 57428 KB Output is correct
38 Correct 61 ms 45716 KB Output is correct
39 Correct 55 ms 43088 KB Output is correct
40 Correct 58 ms 45904 KB Output is correct
41 Correct 45 ms 33736 KB Output is correct
42 Correct 48 ms 32084 KB Output is correct
43 Correct 48 ms 32144 KB Output is correct
44 Correct 44 ms 31832 KB Output is correct
45 Correct 115 ms 53588 KB Output is correct
46 Correct 66 ms 45140 KB Output is correct
47 Correct 68 ms 46160 KB Output is correct
48 Correct 64 ms 48720 KB Output is correct
49 Correct 43 ms 33364 KB Output is correct
50 Correct 42 ms 32084 KB Output is correct
51 Correct 41 ms 32080 KB Output is correct
52 Correct 40 ms 32080 KB Output is correct
53 Correct 110 ms 53176 KB Output is correct
54 Correct 65 ms 44736 KB Output is correct