답안 #559764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
559764 2022-05-10T14:24:08 Z hoanghq2004 Pipes (BOI13_pipes) C++14
100 / 100
167 ms 35636 KB
#include <bits/stdc++.h>

using namespace std;

int n, m, vis[100010];
set <int> adj[100010];
int w[100010], s[100010];
map <int, int> res[100010];
vector <int> circle;
pair <int, int> e[500010];

void dfs(int u) {
    vis[u] = 1;
    circle.push_back(u);
    for (auto v: adj[u]) {
        if (vis[v]) continue;
        dfs(v);
    }
}

int main() {
    scanf("%d%d", &n, &m);
    if (m > n) {
        printf("0\n");
        exit(0);
    }
    for (int i = 1; i <= n; ++i) scanf("%d", &w[i]);
    for (int i = 1; i <= m; ++i) {
        int u, v, w;
        scanf("%d%d", &u, &v);
        adj[u].insert(v);
        adj[v].insert(u);
        e[i] = {u, v};
    }
    queue <int> q;
    for (int i = 1; i <= n; ++i)
        if (adj[i].size() == 1) q.push(i);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        int p = *adj[u].begin();
        res[u][p] = res[p][u] = w[u];
        w[p] -= w[u];
        adj[p].erase(u);
        adj[u].clear();
        if (adj[p].size() == 1) q.push(p);
    }
    int root = 0;
    for (int i = 1; i <= n; ++i)
        if (adj[i].size()) root = i;
    dfs(root);
    if (!circle.empty() && circle.size() % 2 == 0) {
        printf("0\n");
        exit(0);
    }
    for (int i = 0; i < circle.size(); ++i) s[i + 1] = (s[i]) + (i % 2 == 0 ? 1 : -1) * w[circle[i]];
    for (int i = 0; i < circle.size(); ++i) {
        int u = circle[i], v = circle[(i + 1) % circle.size()];
        res[u][v] = res[v][u] = (s[i + 1] - s[circle.size()] + s[i + 1]) * (i % 2 == 0 ? 1 : -1) / 2;
    }
    for (int i = 1; i <= m; ++i) {
        int u = e[i].first, v = e[i].second;
        printf("%d\n", res[u][v] * 2);
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:29:19: warning: unused variable 'w' [-Wunused-variable]
   29 |         int u, v, w;
      |                   ^
pipes.cpp:56:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     for (int i = 0; i < circle.size(); ++i) s[i + 1] = (s[i]) + (i % 2 == 0 ? 1 : -1) * w[circle[i]];
      |                     ~~^~~~~~~~~~~~~~~
pipes.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 0; i < circle.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~~~
pipes.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |     scanf("%d%d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:27:39: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     for (int i = 1; i <= n; ++i) scanf("%d", &w[i]);
      |                                  ~~~~~^~~~~~~~~~~~~
pipes.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 5 ms 9652 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 149 ms 22448 KB Output is correct
5 Correct 5 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9708 KB Output is correct
8 Correct 5 ms 9712 KB Output is correct
9 Correct 6 ms 9724 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 5 ms 9776 KB Output is correct
12 Correct 6 ms 9724 KB Output is correct
13 Correct 111 ms 19736 KB Output is correct
14 Correct 140 ms 21748 KB Output is correct
15 Correct 145 ms 22452 KB Output is correct
16 Correct 117 ms 20492 KB Output is correct
17 Correct 160 ms 22428 KB Output is correct
18 Correct 150 ms 22544 KB Output is correct
19 Correct 117 ms 22208 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 153 ms 22488 KB Output is correct
23 Correct 106 ms 19820 KB Output is correct
24 Correct 148 ms 22476 KB Output is correct
25 Correct 121 ms 20280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9716 KB Output is correct
2 Correct 6 ms 9976 KB Output is correct
3 Correct 107 ms 23928 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9684 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9696 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 6 ms 9940 KB Output is correct
16 Correct 6 ms 9852 KB Output is correct
17 Correct 7 ms 9884 KB Output is correct
18 Correct 5 ms 9720 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9732 KB Output is correct
21 Correct 5 ms 9724 KB Output is correct
22 Correct 5 ms 9812 KB Output is correct
23 Correct 110 ms 28108 KB Output is correct
24 Correct 142 ms 30404 KB Output is correct
25 Correct 105 ms 23944 KB Output is correct
26 Correct 5 ms 9644 KB Output is correct
27 Correct 5 ms 9684 KB Output is correct
28 Correct 5 ms 9724 KB Output is correct
29 Correct 5 ms 9684 KB Output is correct
30 Correct 130 ms 29068 KB Output is correct
31 Correct 133 ms 35636 KB Output is correct
32 Correct 161 ms 25136 KB Output is correct
33 Correct 108 ms 25088 KB Output is correct
34 Correct 5 ms 9724 KB Output is correct
35 Correct 5 ms 9684 KB Output is correct
36 Correct 5 ms 9684 KB Output is correct
37 Correct 5 ms 9728 KB Output is correct
38 Correct 142 ms 29552 KB Output is correct
39 Correct 167 ms 24132 KB Output is correct
40 Correct 154 ms 29772 KB Output is correct
41 Correct 93 ms 26692 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 5 ms 9684 KB Output is correct
44 Correct 6 ms 9720 KB Output is correct
45 Correct 5 ms 9684 KB Output is correct
46 Correct 128 ms 28764 KB Output is correct
47 Correct 164 ms 29960 KB Output is correct
48 Correct 134 ms 35052 KB Output is correct
49 Correct 115 ms 22344 KB Output is correct
50 Correct 5 ms 9684 KB Output is correct
51 Correct 5 ms 9684 KB Output is correct
52 Correct 5 ms 9728 KB Output is correct
53 Correct 5 ms 9684 KB Output is correct
54 Correct 135 ms 27972 KB Output is correct