Submission #87875

# Submission time Handle Problem Language Result Execution time Memory
87875 2018-12-03T01:31:12 Z updown1 Pipes (BOI13_pipes) C++17
100 / 100
386 ms 83296 KB
/*
http://boi2013.informatik-olympiade.de/wp-content/uploads/2013/05/pipes-spoiler.pdf
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, M)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e endl // "/n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
#define int ll
//500,000,000 operations
const int MAXN = 500000, INF = 1000000000;
//Global Variables
int N, M, out[MAXN], inp[MAXN];
set<pair<int, int> > adj[MAXN]; /// (node, edge #)
set<pair<int, int> >::iterator it;
queue<int> next1;

main() {
    //ifstream cin("test.in");
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> N >> M;
    if (M > N) {w<<0<<e; return 0;}
    ffi cin >> inp[i];
    ffj {int a, b; cin >> a >> b; a--; b--; adj[a].insert(mp(b, j)); adj[b].insert(mp(a, j));}
    ffi if (adj[i].size() == 1) next1.push(i);
    while (!next1.empty()) {
        int x = next1.front(); next1.pop();
        if (adj[x].size() != 1) continue;
        it = adj[x].begin();
        int y = (*it).a;
        inp[y] -= inp[x];
        out[(*it).b] = inp[x]*2;
        inp[x] = 0;
        adj[y].erase(mp(x, (*it).b));
        adj[x].erase(it);
        if (adj[y].size() == 1) next1.push(y);
    }
    if (N == M) {
        int cnt = 0;
        ffi if (adj[i].size() > 0) cnt++;
        if (cnt%2 == 0) {w<<0<<e; return 0;}
        int st;
        ffi if (adj[i].size() > 0) st = i;
        int tot = 0;
        int mul = 1;
        int at = -st, prev, p2;
        while (at != st) {
            if (at == -st) at = st;
            it = adj[at].begin();
            int y = (*it).a;
            tot += inp[at]*mul;
            mul *= -1;
            adj[y].erase(mp(at, (*it).b));
            prev = (*it).b;
            p2 = at;
            at = y;
        }
        out[prev] = tot;
        inp[p2] -= tot/2;
        inp[st] -= tot/2;
        adj[p2].erase(mp(st, prev));
        at = st;
        while (adj[at].size() > 0) {
            it = adj[at].begin();
            int y = (*it).a;
            int ed = (*it).b;
            out[ed] = inp[at]*2;
            inp[y] -= inp[at];
            inp[at] = 0;
            at = y;
        }
    }
    ffj w<< out[j]<<e;
}

Compilation message

pipes.cpp:28:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
pipes.cpp: In function 'int main()':
pipes.cpp:71:25: warning: 'prev' may be used uninitialized in this function [-Wmaybe-uninitialized]
         adj[p2].erase(mp(st, prev));
                         ^
pipes.cpp:56:29: warning: 'p2' may be used uninitialized in this function [-Wmaybe-uninitialized]
         int at = -st, prev, p2;
                             ^~
pipes.cpp:61:26: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
             tot += inp[at]*mul;
                    ~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23928 KB Output is correct
2 Correct 29 ms 24044 KB Output is correct
3 Correct 27 ms 24252 KB Output is correct
4 Correct 330 ms 40392 KB Output is correct
5 Correct 25 ms 40392 KB Output is correct
6 Correct 26 ms 40392 KB Output is correct
7 Correct 25 ms 40392 KB Output is correct
8 Correct 28 ms 40392 KB Output is correct
9 Correct 27 ms 40392 KB Output is correct
10 Correct 29 ms 40392 KB Output is correct
11 Correct 31 ms 40392 KB Output is correct
12 Correct 30 ms 40392 KB Output is correct
13 Correct 312 ms 40392 KB Output is correct
14 Correct 357 ms 42776 KB Output is correct
15 Correct 348 ms 45064 KB Output is correct
16 Correct 320 ms 45064 KB Output is correct
17 Correct 385 ms 48004 KB Output is correct
18 Correct 332 ms 49612 KB Output is correct
19 Correct 320 ms 51020 KB Output is correct
20 Correct 25 ms 51020 KB Output is correct
21 Correct 27 ms 51020 KB Output is correct
22 Correct 343 ms 52732 KB Output is correct
23 Correct 268 ms 52732 KB Output is correct
24 Correct 345 ms 55416 KB Output is correct
25 Correct 277 ms 55416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 55416 KB Output is correct
2 Correct 26 ms 55416 KB Output is correct
3 Correct 148 ms 57156 KB Output is correct
4 Correct 26 ms 57156 KB Output is correct
5 Correct 24 ms 57156 KB Output is correct
6 Correct 24 ms 57156 KB Output is correct
7 Correct 26 ms 57156 KB Output is correct
8 Correct 25 ms 57156 KB Output is correct
9 Correct 24 ms 57156 KB Output is correct
10 Correct 22 ms 57156 KB Output is correct
11 Correct 25 ms 57156 KB Output is correct
12 Correct 26 ms 57156 KB Output is correct
13 Correct 24 ms 57156 KB Output is correct
14 Correct 25 ms 57156 KB Output is correct
15 Correct 27 ms 57156 KB Output is correct
16 Correct 26 ms 57156 KB Output is correct
17 Correct 24 ms 57156 KB Output is correct
18 Correct 26 ms 57156 KB Output is correct
19 Correct 25 ms 57156 KB Output is correct
20 Correct 24 ms 57156 KB Output is correct
21 Correct 24 ms 57156 KB Output is correct
22 Correct 27 ms 57156 KB Output is correct
23 Correct 268 ms 57156 KB Output is correct
24 Correct 350 ms 61556 KB Output is correct
25 Correct 147 ms 61756 KB Output is correct
26 Correct 24 ms 61756 KB Output is correct
27 Correct 24 ms 61756 KB Output is correct
28 Correct 25 ms 61756 KB Output is correct
29 Correct 26 ms 61756 KB Output is correct
30 Correct 319 ms 64196 KB Output is correct
31 Correct 365 ms 66020 KB Output is correct
32 Correct 342 ms 67800 KB Output is correct
33 Correct 144 ms 68768 KB Output is correct
34 Correct 24 ms 68768 KB Output is correct
35 Correct 24 ms 68768 KB Output is correct
36 Correct 24 ms 68768 KB Output is correct
37 Correct 24 ms 68768 KB Output is correct
38 Correct 334 ms 70804 KB Output is correct
39 Correct 383 ms 72484 KB Output is correct
40 Correct 386 ms 74068 KB Output is correct
41 Correct 147 ms 75128 KB Output is correct
42 Correct 28 ms 75128 KB Output is correct
43 Correct 28 ms 75128 KB Output is correct
44 Correct 28 ms 75128 KB Output is correct
45 Correct 29 ms 75128 KB Output is correct
46 Correct 341 ms 77044 KB Output is correct
47 Correct 383 ms 78832 KB Output is correct
48 Correct 384 ms 80180 KB Output is correct
49 Correct 199 ms 80844 KB Output is correct
50 Correct 27 ms 80844 KB Output is correct
51 Correct 27 ms 80844 KB Output is correct
52 Correct 29 ms 80844 KB Output is correct
53 Correct 27 ms 80844 KB Output is correct
54 Correct 366 ms 83296 KB Output is correct