답안 #755536

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
755536 2023-06-10T09:04:20 Z dxz05 Pipes (BOI13_pipes) C++17
100 / 100
598 ms 81384 KB
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 5e5 + 3e2;

set<pair<int, int>> g[N];

int tin[N], low[N], timer = 0;
bool used[N];

bool bridge[N];

void dfs(int v, int p = -1){
    used[v] = true;
    tin[v] = low[v] = timer++;

    for (auto [u, i]: g[v]){
        if (u == p) continue;

        if (!used[u]){
            dfs(u, v);

            low[v] = min(low[v], low[u]);

            if (low[u] > tin[v]){
                bridge[i] = true;
            }
        } else {
            low[v] = min(low[v], tin[u]);
        }
    }

}

int cnt_v, cnt_e;
vector<int> comp;

void dfs2(int v){
    used[v] = true;
    cnt_v++;
    comp.push_back(v);

    for (auto [u, i] : g[v]){
        if (bridge[i]) continue;

        cnt_e++;
        if (!used[u]) dfs2(u);
    }
}

int c[N];
ll ans[N];
void solve(){
    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; i++) cin >> c[i];

    for (int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        --u, --v;

        g[u].emplace(v, i);
        g[v].emplace(u, i);
    }

    dfs(0);

    int cycles_count = 0;

    vector<int> cycle;

    fill(used, used + n, false);

    for (int i = 0; i < n; i++){
        if (used[i]) continue;

        cnt_v = cnt_e = 0;
        comp.clear();

        dfs2(i);

        if (cnt_v == 1) continue;
        cycle = comp;

        cycles_count++;
        cnt_e /= 2;

        if (cnt_e != cnt_v || cnt_v % 2 == 0 || cycles_count > 1){
            cout << 0 << endl;
            return;
        }

    }

    queue<int> q;
    for (int i = 0; i < n; i++){
        if (g[i].size() == 1) q.push(i);
    }

    while (!q.empty()){
        int v = q.front();
        q.pop();

        if (g[v].size() != 1) continue;

        auto [u, i] = *g[v].begin();
        ans[i] = 2 * c[v];
        c[u] -= c[v];
        g[u].erase(make_pair(v, i));

        if (g[u].size() == 1){
            q.push(u);
        }
    }

    int k = (int) cycle.size();

    if (k > 0) {
        ll tot = 0;
        for (int v : cycle) tot += c[v];

        vector<ll> dp(k, 0);
        for (int i = 0; i + 1 < k; i += 2) dp[0] += c[cycle[i]];
        for (int i = 1; i + 1 < k; i += 2) dp[1] += c[cycle[i]];

        for (int i = 2; i < k; i++){
            dp[i] = dp[i - 2] - c[cycle[i - 2]];
            int j = i + 2 * (k / 2 - 1);
            j %= k;
            dp[i] += c[cycle[j]];
        }

        for (int i = 0; i < k; i++) {
            int v = cycle[i], u = cycle[(i + 1) % k];

            int j = g[v].lower_bound(make_pair(u, 0))->second;
            ans[j] = tot - 2 * dp[(i + 2) % k];
        }
    }

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

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:165:13: warning: unused variable 'startTime' [-Wunused-variable]
  165 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 13 ms 23836 KB Output is correct
3 Correct 14 ms 24020 KB Output is correct
4 Correct 121 ms 36260 KB Output is correct
5 Correct 14 ms 23824 KB Output is correct
6 Correct 13 ms 23828 KB Output is correct
7 Correct 15 ms 23868 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 14 ms 23892 KB Output is correct
10 Correct 14 ms 23892 KB Output is correct
11 Correct 16 ms 23892 KB Output is correct
12 Correct 14 ms 23968 KB Output is correct
13 Correct 97 ms 33784 KB Output is correct
14 Correct 119 ms 35692 KB Output is correct
15 Correct 120 ms 36416 KB Output is correct
16 Correct 96 ms 34508 KB Output is correct
17 Correct 126 ms 36464 KB Output is correct
18 Correct 124 ms 36412 KB Output is correct
19 Correct 132 ms 40752 KB Output is correct
20 Correct 14 ms 23764 KB Output is correct
21 Correct 13 ms 23908 KB Output is correct
22 Correct 124 ms 36364 KB Output is correct
23 Correct 97 ms 33868 KB Output is correct
24 Correct 122 ms 36380 KB Output is correct
25 Correct 99 ms 34336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23880 KB Output is correct
2 Correct 15 ms 24100 KB Output is correct
3 Correct 98 ms 38904 KB Output is correct
4 Correct 107 ms 42780 KB Output is correct
5 Correct 81 ms 34872 KB Output is correct
6 Correct 598 ms 81384 KB Output is correct
7 Correct 13 ms 23832 KB Output is correct
8 Correct 13 ms 23836 KB Output is correct
9 Correct 13 ms 23832 KB Output is correct
10 Correct 13 ms 23784 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
12 Correct 13 ms 23812 KB Output is correct
13 Correct 13 ms 23832 KB Output is correct
14 Correct 13 ms 23836 KB Output is correct
15 Correct 14 ms 24020 KB Output is correct
16 Correct 14 ms 24020 KB Output is correct
17 Correct 16 ms 24080 KB Output is correct
18 Correct 15 ms 24000 KB Output is correct
19 Correct 14 ms 24020 KB Output is correct
20 Correct 14 ms 23892 KB Output is correct
21 Correct 15 ms 24232 KB Output is correct
22 Correct 15 ms 23980 KB Output is correct
23 Correct 113 ms 39368 KB Output is correct
24 Correct 145 ms 41708 KB Output is correct
25 Correct 95 ms 38952 KB Output is correct
26 Correct 101 ms 41328 KB Output is correct
27 Correct 108 ms 42544 KB Output is correct
28 Correct 86 ms 35480 KB Output is correct
29 Correct 452 ms 71736 KB Output is correct
30 Correct 138 ms 44672 KB Output is correct
31 Correct 156 ms 45460 KB Output is correct
32 Correct 154 ms 38028 KB Output is correct
33 Correct 118 ms 40744 KB Output is correct
34 Correct 114 ms 40480 KB Output is correct
35 Correct 129 ms 42820 KB Output is correct
36 Correct 86 ms 35112 KB Output is correct
37 Correct 589 ms 81364 KB Output is correct
38 Correct 137 ms 43616 KB Output is correct
39 Correct 131 ms 37436 KB Output is correct
40 Correct 140 ms 41268 KB Output is correct
41 Correct 112 ms 43924 KB Output is correct
42 Correct 110 ms 41292 KB Output is correct
43 Correct 117 ms 43620 KB Output is correct
44 Correct 82 ms 34868 KB Output is correct
45 Correct 506 ms 71508 KB Output is correct
46 Correct 137 ms 45596 KB Output is correct
47 Correct 132 ms 41384 KB Output is correct
48 Correct 146 ms 45056 KB Output is correct
49 Correct 84 ms 35796 KB Output is correct
50 Correct 102 ms 40612 KB Output is correct
51 Correct 97 ms 38428 KB Output is correct
52 Correct 98 ms 37272 KB Output is correct
53 Correct 546 ms 73736 KB Output is correct
54 Correct 123 ms 42800 KB Output is correct