답안 #698542

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698542 2023-02-13T18:17:33 Z _martynas Pipes (BOI13_pipes) C++11
72.7778 / 100
178 ms 23996 KB
#include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define PB push_back

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

const int MXN = 1e5+5;

int n, m;
ll C[MXN];
vector<pii> adj[MXN];
int degree[MXN];
ll E[5*MXN];
bool visited[MXN];

void dfs(int u, vi &comp) {
    visited[u] = true;
    comp.PB(u);
    for(auto p : adj[u]) {
        int v = p.F;
        if(visited[v]) continue;
        dfs(v, comp);
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> C[i];
    for(int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        adj[a].PB({b, i});
        adj[b].PB({a, i});
    }
    queue<int> Q;
    for(int i = 1; i <= n; i++) {
        degree[i] = adj[i].size();
        if(degree[i] == 1) Q.push(i);
    }
    while(!Q.empty()) {
        int u = Q.front();
        Q.pop();
        degree[u] = 0;
        for(auto p : adj[u]) {
            if(degree[p.F]) {
                // there should only be one v that satisfies this condition
                E[p.S] = C[u];
                degree[p.F]--;
                C[p.F] -= C[u];
                if(degree[p.F] == 1) Q.push(p.F);
            }
        }
    }
    // all remaining vertices should be disjoint cycles of odd length
    for(int i = 1; i <= n; i++) {
        if(degree[i] != 0 && degree[i] != 2) {
            cout << "0\n";
            return 0;
        }
    }
    for(int i = 1; i <= n; i++) {
        if(degree[i] && !visited[i]) {
            vi comp;
            dfs(i, comp);
            if(comp.size() % 2 == 0) {
                cout << "0\n";
                return 0;
            }
            ll sum = 0;
            for(int u : comp) sum += C[u];
            sum /= 2;
            // keep edge cost between 0 and 1
            for(int u = 2; u < comp.size(); u += 2) {
                sum -= C[comp[u]];
            }
            ll last = 0;
            for(auto p : adj[comp[0]]) {
                if(p.F == comp[1]) {
                    E[p.S] = sum;
                    last = E[p.S];
                }
            }
            for(int u = 1; u < comp.size(); u++) {
                for(auto p : adj[comp[u]]) {
                    if(p.F == comp[(u+1)%comp.size()]) {
                        E[p.S] = C[comp[u]]-last;
                        last = E[p.S];
                    }
                }
            }
        }
    }
    for(int i = 0; i < m; i++) {
        cout << 2*E[i] << "\n";
    }
    return 0;
}
/*
3 2
1 2 1
1 2
2 3

3 3
8 10 12
1 2
2 3
1 3

3 7 5
*/

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:80:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   80 |             for(int u = 2; u < comp.size(); u += 2) {
      |                            ~~^~~~~~~~~~~~~
pipes.cpp:90:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |             for(int u = 1; u < comp.size(); u++) {
      |                            ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 62 ms 10568 KB Output is correct
5 Correct 2 ms 2680 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2676 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2772 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 40 ms 8884 KB Output is correct
14 Correct 50 ms 10108 KB Output is correct
15 Correct 56 ms 10556 KB Output is correct
16 Correct 45 ms 9340 KB Output is correct
17 Correct 50 ms 10552 KB Output is correct
18 Correct 52 ms 10540 KB Output is correct
19 Correct 61 ms 9824 KB Output is correct
20 Correct 2 ms 2644 KB Output is correct
21 Correct 2 ms 2692 KB Output is correct
22 Correct 53 ms 10576 KB Output is correct
23 Correct 44 ms 8940 KB Output is correct
24 Correct 55 ms 10568 KB Output is correct
25 Correct 44 ms 9168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2644 KB Output isn't correct
2 Incorrect 2 ms 2772 KB Output isn't correct
3 Correct 60 ms 12724 KB Output is correct
4 Correct 39 ms 9508 KB Output is correct
5 Correct 40 ms 9676 KB Output is correct
6 Correct 152 ms 23996 KB Output is correct
7 Incorrect 2 ms 2644 KB Output isn't correct
8 Incorrect 1 ms 2684 KB Output isn't correct
9 Correct 2 ms 2680 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 1 ms 2676 KB Output is correct
13 Correct 2 ms 2684 KB Output is correct
14 Incorrect 2 ms 2676 KB Output isn't correct
15 Incorrect 2 ms 2812 KB Output isn't correct
16 Incorrect 2 ms 2684 KB Output isn't correct
17 Correct 2 ms 2688 KB Output is correct
18 Correct 2 ms 2644 KB Output is correct
19 Correct 2 ms 2644 KB Output is correct
20 Correct 2 ms 2696 KB Output is correct
21 Correct 2 ms 2772 KB Output is correct
22 Incorrect 2 ms 2772 KB Output isn't correct
23 Incorrect 50 ms 12256 KB Output isn't correct
24 Incorrect 49 ms 13552 KB Output isn't correct
25 Correct 47 ms 12660 KB Output is correct
26 Correct 43 ms 9452 KB Output is correct
27 Correct 40 ms 9296 KB Output is correct
28 Correct 45 ms 10040 KB Output is correct
29 Correct 122 ms 20544 KB Output is correct
30 Incorrect 60 ms 13580 KB Output isn't correct
31 Incorrect 87 ms 15688 KB Output isn't correct
32 Incorrect 68 ms 11692 KB Output isn't correct
33 Correct 48 ms 13728 KB Output is correct
34 Correct 36 ms 9444 KB Output is correct
35 Correct 37 ms 9436 KB Output is correct
36 Correct 41 ms 9836 KB Output is correct
37 Correct 178 ms 23884 KB Output is correct
38 Incorrect 53 ms 14732 KB Output isn't correct
39 Incorrect 69 ms 11320 KB Output isn't correct
40 Incorrect 49 ms 13388 KB Output isn't correct
41 Incorrect 74 ms 15848 KB Output isn't correct
42 Correct 37 ms 9272 KB Output is correct
43 Correct 36 ms 9304 KB Output is correct
44 Correct 41 ms 9688 KB Output is correct
45 Correct 113 ms 20812 KB Output is correct
46 Incorrect 60 ms 15944 KB Output isn't correct
47 Incorrect 48 ms 13512 KB Output isn't correct
48 Incorrect 61 ms 15580 KB Output isn't correct
49 Correct 47 ms 11272 KB Output is correct
50 Correct 43 ms 9548 KB Output is correct
51 Correct 40 ms 9728 KB Output is correct
52 Correct 38 ms 9348 KB Output is correct
53 Correct 118 ms 21120 KB Output is correct
54 Incorrect 57 ms 13576 KB Output isn't correct