답안 #253845

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253845 2020-07-28T22:10:41 Z aZvezda Pipes (BOI13_pipes) C++14
44.0741 / 100
1000 ms 31096 KB
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
template<class T> inline void fix(T &x) {if(x >= mod | x <= -mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl


//If we have an even cycle then we can add 1 on odd edges and -1 on even edges so the solutions stay the same. We can remove leaves one by one

const ll MAX_N = 1e5 + 10;
set<pair<ll, ll> > g[MAX_N];
ll ans[MAX_N], arr[MAX_N];

struct cmp {
    bool operator ()(const ll &a, const ll &b) const {
        if(g[a].size() == g[b].size()) {
            return a < b;
        }
        return g[a].size() < g[b].size();
    }
};

multiset<ll, cmp> pq;

void removeBranches() {
    while(!pq.empty() && g[*(pq.begin())].size() == 1) {
        ll curr = *(pq.begin()); pq.erase(pq.begin());
        auto other = *(g[curr].begin());
        pq.erase(other.first);
        ans[other.second] = 2 * arr[curr];
        arr[other.first] -= arr[curr];
        g[other.first].erase({curr, other.second});
        pq.insert(other.first);
    }
}

int rret = 0;

void dfs(int x, int p, int start, int flag) {
    rret += arr[x] * flag;
    //cout << x << " " << p << " " << ans[x] << " " << rret << endl;
    for(auto it : g[x]) {
        if(it.first == p || it.first == start) {continue;}
        dfs(it.first, x, start, -flag);
    }
}

void removeCycle() {
    if(pq.empty()) {return;}
    auto now = pq.begin();
    if(g[*now].size() > 2) {
        cout << 0 << endl;
        exit(0);
    }
    now = pq.end(); now --;
    if(g[*now].size() > 2) {
        cout << 0 << endl;
        exit(0);
    }
    if(pq.size() % 2 == 0) {
        cout << 0 << endl;
        exit(0);
    }
    int curr = *(pq.begin());
    dfs(curr, (*(g[curr].begin())).first, curr, 1);
    rret /= 2;
    auto sec = (g[curr].begin());
    arr[curr] -= rret;
    arr[(*sec).first] -= rret;
    ans[(*sec).second] = rret;
    g[curr].erase(sec);
    g[(*sec).first].erase({curr, (*sec).second});
    //cout << curr << " " << (*sec).first << " " << rret << endl;
}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll n, m;
    cin >> n >> m;
    if(m > n) {cout << 0 << endl; return 0;}
    for(ll i = 1; i <= n; i ++) {
        cin >> arr[i];
    }
    for(ll i = 0; i < m; i ++) {
        ll a, b;
        cin >> a >> b;
        g[a].insert({b, i});
        g[b].insert({a, i});
    }
    for(ll i = 1; i <= n; i ++) {
        pq.insert(i);
    }
    removeBranches();
    removeCycle();
    removeBranches();
    for(ll i = 0; i < m; i ++) {
        cout << ans[i] << endl;
    }
    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1092 ms 4992 KB Time limit exceeded
2 Execution timed out 1093 ms 4992 KB Time limit exceeded
3 Execution timed out 1093 ms 5248 KB Time limit exceeded
4 Execution timed out 1072 ms 23672 KB Time limit exceeded
5 Execution timed out 1097 ms 4992 KB Time limit exceeded
6 Execution timed out 1085 ms 4992 KB Time limit exceeded
7 Execution timed out 1099 ms 4992 KB Time limit exceeded
8 Execution timed out 1099 ms 4992 KB Time limit exceeded
9 Execution timed out 1099 ms 5248 KB Time limit exceeded
10 Execution timed out 1100 ms 5248 KB Time limit exceeded
11 Execution timed out 1099 ms 5248 KB Time limit exceeded
12 Execution timed out 1099 ms 5248 KB Time limit exceeded
13 Execution timed out 1088 ms 19960 KB Time limit exceeded
14 Execution timed out 1094 ms 22696 KB Time limit exceeded
15 Execution timed out 1091 ms 23800 KB Time limit exceeded
16 Execution timed out 1083 ms 21048 KB Time limit exceeded
17 Execution timed out 1097 ms 23672 KB Time limit exceeded
18 Execution timed out 1101 ms 23800 KB Time limit exceeded
19 Execution timed out 1085 ms 23800 KB Time limit exceeded
20 Execution timed out 1076 ms 4992 KB Time limit exceeded
21 Execution timed out 1094 ms 5248 KB Time limit exceeded
22 Execution timed out 1088 ms 23932 KB Time limit exceeded
23 Execution timed out 1100 ms 19960 KB Time limit exceeded
24 Execution timed out 1098 ms 23800 KB Time limit exceeded
25 Execution timed out 1096 ms 20600 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4992 KB Output isn't correct
2 Incorrect 4 ms 5248 KB Output isn't correct
3 Correct 159 ms 22648 KB Output is correct
4 Correct 3 ms 5120 KB Output is correct
5 Correct 3 ms 5120 KB Output is correct
6 Correct 4 ms 5120 KB Output is correct
7 Incorrect 3 ms 4992 KB Output isn't correct
8 Incorrect 3 ms 4992 KB Output isn't correct
9 Correct 3 ms 4992 KB Output is correct
10 Correct 3 ms 4992 KB Output is correct
11 Correct 3 ms 4992 KB Output is correct
12 Correct 3 ms 4992 KB Output is correct
13 Correct 3 ms 4992 KB Output is correct
14 Incorrect 3 ms 4992 KB Output isn't correct
15 Incorrect 5 ms 5248 KB Output isn't correct
16 Incorrect 4 ms 5248 KB Output isn't correct
17 Correct 4 ms 5248 KB Output is correct
18 Correct 3 ms 4992 KB Output is correct
19 Correct 3 ms 4992 KB Output is correct
20 Correct 3 ms 4992 KB Output is correct
21 Correct 3 ms 4992 KB Output is correct
22 Incorrect 4 ms 5248 KB Output isn't correct
23 Incorrect 164 ms 24696 KB Output isn't correct
24 Incorrect 210 ms 28408 KB Output isn't correct
25 Correct 152 ms 22652 KB Output is correct
26 Correct 3 ms 5120 KB Output is correct
27 Correct 4 ms 5120 KB Output is correct
28 Correct 4 ms 5120 KB Output is correct
29 Correct 4 ms 5120 KB Output is correct
30 Incorrect 204 ms 27384 KB Output isn't correct
31 Incorrect 230 ms 31096 KB Output isn't correct
32 Incorrect 245 ms 25592 KB Output isn't correct
33 Correct 162 ms 23800 KB Output is correct
34 Correct 3 ms 5120 KB Output is correct
35 Correct 3 ms 5120 KB Output is correct
36 Correct 3 ms 5120 KB Output is correct
37 Correct 3 ms 5120 KB Output is correct
38 Incorrect 194 ms 27896 KB Output isn't correct
39 Incorrect 218 ms 24952 KB Output isn't correct
40 Incorrect 211 ms 28024 KB Output isn't correct
41 Correct 112 ms 23780 KB Output is correct
42 Correct 4 ms 5120 KB Output is correct
43 Correct 3 ms 5120 KB Output is correct
44 Correct 3 ms 5120 KB Output is correct
45 Correct 3 ms 5120 KB Output is correct
46 Incorrect 188 ms 27384 KB Output isn't correct
47 Incorrect 221 ms 28152 KB Output isn't correct
48 Incorrect 231 ms 30968 KB Output isn't correct
49 Correct 203 ms 22648 KB Output is correct
50 Correct 3 ms 5120 KB Output is correct
51 Correct 3 ms 5120 KB Output is correct
52 Correct 3 ms 5120 KB Output is correct
53 Correct 4 ms 5120 KB Output is correct
54 Incorrect 199 ms 26872 KB Output isn't correct