답안 #283178

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283178 2020-08-25T10:56:49 Z egekabas Pipes (BOI13_pipes) C++14
100 / 100
109 ms 17148 KB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
ll n, m;
ll vert[100009];
ll deg[100009];
vector<pll> g[100009];
ll vis[100009];
ll ans[500009];
pll val[500009];
void dfs1(ll v){
    vis[v] = 1;
    for(auto u : g[v]){
        if(vis[u.ff])
            continue;
        ans[u.ss] = vert[v];
        vert[v] -= ans[u.ss];
        vert[u.ff] -= ans[u.ss];
        deg[v]--;
        deg[u.ff]--;
        if(deg[u.ff] <= 1)
            dfs1(u.ff);
    }
}
void dfs2(ll v, pll curval){
    vis[v] = 1;
    for(auto u : g[v]){
        if(deg[u.ff] == 0)
            continue;
        if(val[u.ss].ff == 0)
            val[u.ss] = curval;
        if(vis[u.ff])
            continue;
        dfs2(u.ff, {-curval.ff, vert[u.ff]-curval.ss});
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n >> m;
    if(m > n){
        cout << "0\n";
        return 0;
    }
    for(ll i = 1; i <= n; ++i)
        cin >> vert[i];
    for(ll i = 0; i < m; ++i){
        ll x, y;
        cin >> x >> y;
        deg[x]++;
        deg[y]++;
        g[x].pb({y, i});
        g[y].pb({x, i});
    }
    for(ll i = 1; i <= n; ++i)
        if(vis[i] == 0 && deg[i] == 1)
            dfs1(i);
    ll var;
    for(ll i = 1; i <= n; ++i)
        if(vis[i] == 0){
            vector<ll> ed;
            for(auto u : g[i])
                if(vis[u.ff] == 0)
                    ed.pb(u.ss);
            dfs2(i, {1, 0});
            pll curval = {val[ed[0]].ff+val[ed[1]].ff, val[ed[0]].ss+val[ed[1]].ss};
            if(curval.ff == 0){
                cout << "0\n";
                return 0;
            }
            var = (vert[i]-curval.ss)/curval.ff;
        }
    for(int i = 0; i < m; ++i){
        if(val[i].ff != 0)
            ans[i] = var*val[i].ff+val[i].ss;
        cout << 2*ans[i] << '\n';
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:90:25: warning: 'var' may be used uninitialized in this function [-Wmaybe-uninitialized]
   90 |             ans[i] = var*val[i].ff+val[i].ss;
      |                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2816 KB Output is correct
4 Correct 109 ms 11384 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 2 ms 2688 KB Output is correct
9 Correct 2 ms 2816 KB Output is correct
10 Correct 3 ms 2816 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 2 ms 2816 KB Output is correct
13 Correct 60 ms 9592 KB Output is correct
14 Correct 77 ms 11000 KB Output is correct
15 Correct 79 ms 11384 KB Output is correct
16 Correct 65 ms 10104 KB Output is correct
17 Correct 78 ms 11384 KB Output is correct
18 Correct 91 ms 11676 KB Output is correct
19 Correct 91 ms 12536 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2816 KB Output is correct
22 Correct 82 ms 11388 KB Output is correct
23 Correct 75 ms 9596 KB Output is correct
24 Correct 85 ms 11384 KB Output is correct
25 Correct 64 ms 9976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2816 KB Output is correct
3 Correct 74 ms 14116 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2816 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 2 ms 2816 KB Output is correct
8 Correct 3 ms 2684 KB Output is correct
9 Correct 2 ms 2688 KB Output is correct
10 Correct 2 ms 2688 KB Output is correct
11 Correct 2 ms 2688 KB Output is correct
12 Correct 2 ms 2688 KB Output is correct
13 Correct 2 ms 2688 KB Output is correct
14 Correct 2 ms 2688 KB Output is correct
15 Correct 2 ms 2816 KB Output is correct
16 Correct 2 ms 2816 KB Output is correct
17 Correct 2 ms 2816 KB Output is correct
18 Correct 2 ms 2688 KB Output is correct
19 Correct 2 ms 2688 KB Output is correct
20 Correct 2 ms 2688 KB Output is correct
21 Correct 2 ms 2688 KB Output is correct
22 Correct 2 ms 2816 KB Output is correct
23 Correct 70 ms 13560 KB Output is correct
24 Correct 91 ms 15568 KB Output is correct
25 Correct 74 ms 14072 KB Output is correct
26 Correct 2 ms 2688 KB Output is correct
27 Correct 2 ms 2688 KB Output is correct
28 Correct 2 ms 2688 KB Output is correct
29 Correct 2 ms 2688 KB Output is correct
30 Correct 87 ms 14924 KB Output is correct
31 Correct 90 ms 17148 KB Output is correct
32 Correct 85 ms 13736 KB Output is correct
33 Correct 77 ms 15224 KB Output is correct
34 Correct 2 ms 2688 KB Output is correct
35 Correct 2 ms 2688 KB Output is correct
36 Correct 2 ms 2816 KB Output is correct
37 Correct 2 ms 2688 KB Output is correct
38 Correct 86 ms 15224 KB Output is correct
39 Correct 89 ms 13432 KB Output is correct
40 Correct 94 ms 15352 KB Output is correct
41 Correct 78 ms 16632 KB Output is correct
42 Correct 2 ms 2688 KB Output is correct
43 Correct 2 ms 2688 KB Output is correct
44 Correct 2 ms 2720 KB Output is correct
45 Correct 2 ms 2720 KB Output is correct
46 Correct 87 ms 14840 KB Output is correct
47 Correct 88 ms 15480 KB Output is correct
48 Correct 91 ms 17016 KB Output is correct
49 Correct 76 ms 12616 KB Output is correct
50 Correct 2 ms 2688 KB Output is correct
51 Correct 2 ms 2688 KB Output is correct
52 Correct 2 ms 2688 KB Output is correct
53 Correct 2 ms 2688 KB Output is correct
54 Correct 90 ms 14584 KB Output is correct