답안 #84231

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
84231 2018-11-14T02:03:16 Z atoiz Pipes (BOI13_pipes) C++14
100 / 100
121 ms 24452 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <stack>

using namespace std;

const int MAX_N = 100010, MAX_M = 500100;
bool dead[MAX_N];
vector<int> G[MAX_N];
int deg[MAX_N];
struct Edge { int u, v; } edges[MAX_M];
int n, m;
int64_t w_e[MAX_M], w_v[MAX_N], nxt_id[MAX_N];

void kill() { cout << 0; exit(0); }

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    if (m > n) kill();

    for (int u = 1; u <= n; ++u) cin >> w_v[u];
    for (int id = 0; id < m; ++id) {
        int u, v; cin >> u >> v;
        ++deg[u], ++deg[v];
        G[u].push_back(id); G[v].push_back(id);
        edges[id] = {u, v};
    }

    stack<int> st;
    for (int u = 1; u <= n; ++u) if (deg[u] == 1) st.push(u);
    while (!st.empty()) {
        int u = st.top(); st.pop();
		dead[u] = 1;
        for (int id : G[u]) {
            int v = edges[id].u == u ? edges[id].v : edges[id].u;
            if (dead[v]) continue;

            w_e[id] = w_v[u];
            w_v[u] -= w_e[id]; w_v[v] -= w_e[id];
            --deg[u]; --deg[v];

            if (deg[v] == 1) st.push(v);
        }
    }

    if (m < n) {
        for (int i = 0; i < m; ++i) cout << w_e[i] * 2 << '\n';
        return 0;
    }

    int start = 1;
    while (dead[start]) ++start;
	int u = start, p = -1, cnt = 0;
	int64_t cur = 0, sgn = 1;
    do {
        cur += w_v[u] * sgn; sgn = -sgn;

		int v;
        for (int id : G[u]) {
            v = edges[id].u == u ? edges[id].v : edges[id].u;
            if (p == v) continue;
            if (!dead[v]) {
                nxt_id[u] = id;
				break;
            }
        }
		p = u, u = v;
		++cnt;
    } while (u != start);

    if (!(cnt & 1)) kill();
    if (cur & 1) kill();
    cur >>= 1;

    u = start;
    do {
        cur = w_v[u] - cur;
        int id = nxt_id[u];
        w_e[id] = cur;
        u = edges[id].u == u ? edges[id].v : edges[id].u;
    } while (u != start);

	for (int i = 0; i < m; ++i) cout << w_e[i] * 2 << '\n';
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:86:16: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
     } while (u != start);
              ~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 8 ms 2936 KB Output is correct
3 Correct 5 ms 2992 KB Output is correct
4 Correct 101 ms 10964 KB Output is correct
5 Correct 4 ms 10964 KB Output is correct
6 Correct 4 ms 10964 KB Output is correct
7 Correct 4 ms 10964 KB Output is correct
8 Correct 4 ms 10964 KB Output is correct
9 Correct 4 ms 10964 KB Output is correct
10 Correct 4 ms 10964 KB Output is correct
11 Correct 5 ms 10964 KB Output is correct
12 Correct 4 ms 10964 KB Output is correct
13 Correct 75 ms 10964 KB Output is correct
14 Correct 81 ms 13296 KB Output is correct
15 Correct 96 ms 15232 KB Output is correct
16 Correct 75 ms 15528 KB Output is correct
17 Correct 92 ms 17756 KB Output is correct
18 Correct 81 ms 18228 KB Output is correct
19 Correct 94 ms 19676 KB Output is correct
20 Correct 4 ms 19676 KB Output is correct
21 Correct 4 ms 19676 KB Output is correct
22 Correct 85 ms 21420 KB Output is correct
23 Correct 115 ms 21420 KB Output is correct
24 Correct 80 ms 21596 KB Output is correct
25 Correct 67 ms 21596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 21596 KB Output is correct
2 Correct 4 ms 21596 KB Output is correct
3 Correct 73 ms 21596 KB Output is correct
4 Correct 4 ms 21596 KB Output is correct
5 Correct 4 ms 21596 KB Output is correct
6 Correct 4 ms 21596 KB Output is correct
7 Correct 5 ms 21596 KB Output is correct
8 Correct 4 ms 21596 KB Output is correct
9 Correct 4 ms 21596 KB Output is correct
10 Correct 4 ms 21596 KB Output is correct
11 Correct 4 ms 21596 KB Output is correct
12 Correct 4 ms 21596 KB Output is correct
13 Correct 4 ms 21596 KB Output is correct
14 Correct 4 ms 21596 KB Output is correct
15 Correct 4 ms 21596 KB Output is correct
16 Correct 4 ms 21596 KB Output is correct
17 Correct 5 ms 21596 KB Output is correct
18 Correct 4 ms 21596 KB Output is correct
19 Correct 4 ms 21596 KB Output is correct
20 Correct 4 ms 21596 KB Output is correct
21 Correct 4 ms 21596 KB Output is correct
22 Correct 4 ms 21596 KB Output is correct
23 Correct 65 ms 21596 KB Output is correct
24 Correct 121 ms 22188 KB Output is correct
25 Correct 64 ms 22188 KB Output is correct
26 Correct 4 ms 22188 KB Output is correct
27 Correct 4 ms 22188 KB Output is correct
28 Correct 4 ms 22188 KB Output is correct
29 Correct 4 ms 22188 KB Output is correct
30 Correct 84 ms 22188 KB Output is correct
31 Correct 119 ms 22188 KB Output is correct
32 Correct 74 ms 22316 KB Output is correct
33 Correct 64 ms 22316 KB Output is correct
34 Correct 4 ms 22316 KB Output is correct
35 Correct 4 ms 22316 KB Output is correct
36 Correct 5 ms 22316 KB Output is correct
37 Correct 4 ms 22316 KB Output is correct
38 Correct 74 ms 22316 KB Output is correct
39 Correct 74 ms 22316 KB Output is correct
40 Correct 80 ms 22764 KB Output is correct
41 Correct 64 ms 23728 KB Output is correct
42 Correct 4 ms 23728 KB Output is correct
43 Correct 4 ms 23728 KB Output is correct
44 Correct 4 ms 23728 KB Output is correct
45 Correct 4 ms 23728 KB Output is correct
46 Correct 78 ms 24228 KB Output is correct
47 Correct 80 ms 24452 KB Output is correct
48 Correct 77 ms 24452 KB Output is correct
49 Correct 61 ms 24452 KB Output is correct
50 Correct 4 ms 24452 KB Output is correct
51 Correct 4 ms 24452 KB Output is correct
52 Correct 4 ms 24452 KB Output is correct
53 Correct 5 ms 24452 KB Output is correct
54 Correct 81 ms 24452 KB Output is correct