답안 #88710

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
88710 2018-12-07T18:11:41 Z popovicirobert Pipes (BOI13_pipes) C++14
45.3704 / 100
311 ms 112196 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

const int MAXM = (int) 5e5;
const int MAXN = (int) 1e5;

vector < pair <int, int> > g[MAXN + 1];
int even[MAXN + 1], father[MAXN + 1], n;
bool vis[MAXN + 1];

void dfs(int nod) {
    vis[nod] = 1;
    for(auto it : g[nod]) {
        if(vis[it.first] == 0) {
            even[it.first] = (even[nod] ^ 1);
            father[it.first] = nod;
            dfs(it.first);
        }
    }
}

inline void detect_cycle(bool &cycle) {
    memset(even, -1, sizeof(even));
    even[1] = 0;
    dfs(1);
    int cnt = 0;
    for(int i = 1; i <= n; i++) {
        for(auto it : g[i]) {
            if(even[i] != even[it.first] && !(father[it.first] == i || father[i] == it.first)) {
                cout << 0;
                exit(0);
            }
            if(even[i] == even[it.first]) {
                cnt++;
            }
        }
    }
    cnt /= 2;
    if(cnt > 1) {
        cout << 0;
        exit(0);
    }
    if(cnt) {
        cycle = 1;
    }
}

ll c[MAXN + 1];
ll sol[MAXM + 1];

int deg[MAXN + 1];

inline void check() {
    for(int i = 1; i <= n; i++) {
        if(c[i] != 0) {
            cout << 0;
            exit(0);
        }
    }
}

inline void solve_tree() {
    int i;
    memset(vis, 0, sizeof(vis));
    queue <int> Q;
    for(i = 1; i <= n; i++) {
        if(g[i].size() == 1) {
            Q.push(i);
            vis[i] = 1;
        }
    }
    while(Q.size()) {
        int nod = Q.front();
        Q.pop();
        for(auto it : g[nod]) {
            if(vis[it.first] == 0) {
                deg[it.first]++;
                sol[it.second] = c[nod];
                c[it.first] -= c[nod];
                c[nod] = 0;
                if(deg[it.first] + 1 == g[i].size()) {
                    vis[it.first] = 1;
                    Q.push(it.first);
                }
            }
        }
    }
}

void dfs1(int nod, vector < pair <int, int> > &nodes, int par, int root) {
    vis[nod] = 1;
    for(auto it : g[nod]) {
        if(it.first == par) {
            continue;
        }
        if(vis[it.first] == 0) {
            nodes.push_back({nod, it.second});
            dfs1(it.first, nodes, nod, root);
        }
        else if(it.first == root) {
            nodes.push_back({nod, it.second});
        }
    }
}

inline void solve_cycle(bool cycle) {
    if(cycle == 0) {
        return ;
    }
    int i, nod;
    for(i = 1; i <= n; i++) {
        if(vis[i] == 0) {
            nod = i;
            break;
        }
    }
    vector < pair <int, int> > nodes;
    dfs1(nod, nodes, 0, nod);
    int sz = nodes.size();
    ll tot = 0;
    for(auto it : nodes) {
        tot += c[it.first];
    }
    tot /= 2;
    ll sum = 0;
    for(i = 0; i + 1 < sz; i += 2) {
        sum += c[nodes[i].first];
    }
    sol[nodes[sz - 2].second] = tot - sum;
    c[nodes[sz - 2].first] -= tot - sum;
    for(i = sz - 3; i >= 0; i--) {
        sol[nodes[i].second] = c[nodes[i + 1].first];
        c[nodes[i].first] -= sol[nodes[i].second];
    }
    sum = 0;
    for(auto it : nodes) {
        sum += sol[it.second];
    }
    sol[nodes.back().second] = tot - sum;
    memset(c, 0, sizeof(c));
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for(i = 1; i <= n; i++) {
        cin >> c[i];
        c[i] *= 2LL;
    }
    for(i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back({y, i});
        g[y].push_back({x, i});
    }
    bool cycle = 0;
    detect_cycle(cycle);
    solve_tree();
    solve_cycle(cycle);
    check();
    for(i = 1; i <= m; i++) {
        cout << sol[i] << "\n";
    }
    //cin.close();
    //cout.close();
    return 0;
}

Compilation message

pipes.cpp: In function 'void solve_tree()':
pipes.cpp:87:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(deg[it.first] + 1 == g[i].size()) {
                    ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
pipes.cpp: In function 'void solve_cycle(bool)':
pipes.cpp:124:9: warning: 'nod' may be used uninitialized in this function [-Wmaybe-uninitialized]
     dfs1(nod, nodes, 0, nod);
     ~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 3324 KB Output isn't correct
2 Incorrect 4 ms 3428 KB Output isn't correct
3 Incorrect 5 ms 3668 KB Output isn't correct
4 Incorrect 75 ms 11468 KB Output isn't correct
5 Incorrect 4 ms 11468 KB Output isn't correct
6 Incorrect 4 ms 11468 KB Output isn't correct
7 Incorrect 5 ms 11468 KB Output isn't correct
8 Incorrect 4 ms 11468 KB Output isn't correct
9 Incorrect 5 ms 11468 KB Output isn't correct
10 Incorrect 5 ms 11468 KB Output isn't correct
11 Incorrect 6 ms 11468 KB Output isn't correct
12 Incorrect 5 ms 11468 KB Output isn't correct
13 Incorrect 59 ms 11636 KB Output isn't correct
14 Incorrect 82 ms 14032 KB Output isn't correct
15 Incorrect 79 ms 15896 KB Output isn't correct
16 Incorrect 71 ms 16320 KB Output isn't correct
17 Incorrect 83 ms 18804 KB Output isn't correct
18 Incorrect 75 ms 20368 KB Output isn't correct
19 Incorrect 93 ms 23652 KB Output isn't correct
20 Incorrect 4 ms 23652 KB Output isn't correct
21 Incorrect 5 ms 23652 KB Output isn't correct
22 Incorrect 73 ms 23652 KB Output isn't correct
23 Incorrect 61 ms 23652 KB Output isn't correct
24 Incorrect 77 ms 26380 KB Output isn't correct
25 Incorrect 65 ms 26692 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 26692 KB Output isn't correct
2 Incorrect 5 ms 26692 KB Output isn't correct
3 Correct 74 ms 29412 KB Output is correct
4 Correct 82 ms 32564 KB Output is correct
5 Correct 68 ms 32564 KB Output is correct
6 Correct 311 ms 51160 KB Output is correct
7 Incorrect 5 ms 51160 KB Output isn't correct
8 Correct 5 ms 51160 KB Output is correct
9 Correct 4 ms 51160 KB Output is correct
10 Correct 4 ms 51160 KB Output is correct
11 Correct 4 ms 51160 KB Output is correct
12 Correct 4 ms 51160 KB Output is correct
13 Correct 4 ms 51160 KB Output is correct
14 Incorrect 5 ms 51160 KB Output isn't correct
15 Incorrect 5 ms 51160 KB Output isn't correct
16 Incorrect 5 ms 51160 KB Output isn't correct
17 Correct 4 ms 51160 KB Output is correct
18 Correct 4 ms 51160 KB Output is correct
19 Correct 5 ms 51160 KB Output is correct
20 Correct 5 ms 51160 KB Output is correct
21 Correct 5 ms 51160 KB Output is correct
22 Incorrect 5 ms 51160 KB Output isn't correct
23 Incorrect 91 ms 51160 KB Output isn't correct
24 Incorrect 113 ms 51160 KB Output isn't correct
25 Correct 72 ms 51160 KB Output is correct
26 Correct 81 ms 51160 KB Output is correct
27 Correct 78 ms 51160 KB Output is correct
28 Correct 83 ms 51160 KB Output is correct
29 Correct 238 ms 62808 KB Output is correct
30 Incorrect 131 ms 62948 KB Output isn't correct
31 Incorrect 113 ms 64304 KB Output isn't correct
32 Incorrect 105 ms 64304 KB Output isn't correct
33 Correct 74 ms 64304 KB Output is correct
34 Correct 97 ms 64304 KB Output is correct
35 Correct 87 ms 64304 KB Output is correct
36 Correct 89 ms 64304 KB Output is correct
37 Correct 298 ms 82844 KB Output is correct
38 Incorrect 129 ms 82844 KB Output isn't correct
39 Incorrect 100 ms 82844 KB Output isn't correct
40 Incorrect 127 ms 82844 KB Output isn't correct
41 Correct 83 ms 82844 KB Output is correct
42 Correct 92 ms 82844 KB Output is correct
43 Correct 92 ms 82844 KB Output is correct
44 Correct 73 ms 82844 KB Output is correct
45 Correct 218 ms 95136 KB Output is correct
46 Incorrect 105 ms 97020 KB Output isn't correct
47 Incorrect 109 ms 97020 KB Output isn't correct
48 Incorrect 119 ms 99120 KB Output isn't correct
49 Correct 73 ms 99120 KB Output is correct
50 Correct 74 ms 99120 KB Output is correct
51 Correct 61 ms 99120 KB Output is correct
52 Correct 85 ms 99120 KB Output is correct
53 Correct 264 ms 112196 KB Output is correct
54 Incorrect 131 ms 112196 KB Output isn't correct