답안 #462440

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
462440 2021-08-10T14:43:30 Z Yeboi Pipes (BOI13_pipes) C++14
62.2222 / 100
497 ms 36472 KB
#include <bits/stdc++.h>
#define ll long long
#define mod 998244353
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i,s) for(ll i=0; i<s ; i++)
#define f(i,a,b) for(ll i(a); i<b ; i++)
const ll INF = 1000000000000000000;
const ll N = 100001;
const ll MOD = 1000000007;
const ll modi = 1000000007;
const ll MAXN = 10000000000;    
const ll rootval = 319;
using namespace std;

ll upedge[N];
ll ngood[N];
vector<ll> adj[N];
ll arr[N];
ll par[N];
ll n;
void dfs1(ll v, ll p){
    ll sum = 0;
    for(auto u : adj[v]){
        if(u == p){
            continue;
        }
        else{
            if(upedge[u] == -1){
                dfs1(u,v);
                sum += upedge[u];
            }
        }
    }
    upedge[v] = arr[v] - sum;
}
void dfs2(ll v, ll p){
    ll sum = 0;
    for(auto u : adj[v]){
        if(u == p || ngood[u] == -1){
            continue;
        }
        else{
            if(upedge[u] == -1){
                dfs2(u,v);
                sum += upedge[u];
            }
        }
    }
    upedge[v] = arr[v] - sum;
}
void parents(ll v, ll p){
    for(auto u : adj[v]){
        if(u == p){
            continue;
        }
        else{
            par[u] = v;
            parents(u,v);
        }
    }
}
void parents2(ll v, ll p){
    for(auto u : adj[v]){
        if(u == p || ngood[u] == -1){
            continue;
        }
        else{
            par[u] = v;
            parents2(u,v);
        }
    }
}
vector<bool> visited;
vector<ll> parent;
ll cycle_start, cycle_end;

bool dfs(ll v, ll par) { // passing vertex and its parent vertex
    visited[v] = true;
    for (ll u : adj[v]) {
        if(u == par) continue; // skipping edge to parent vertex
        if (visited[u]) {
            cycle_end = v;
            cycle_start = u;
            return true;
        }
        parent[u] = v;
        if (dfs(u, parent[u]))
            return true;
    }
    return false;
}

vector<ll> find_cycle() {
    visited.assign(n, false);
    parent.assign(n, -1);
    cycle_start = -1;
    vector<ll> cycle;
    for (ll v = 0; v < n; v++) {
        if (!visited[v] && dfs(v, parent[v]))
            break;
    }

    if (cycle_start == -1) {
        return cycle;
    } else {
        vector<ll> cycle;
        cycle.push_back(cycle_start);
        for (ll v = cycle_end; v != cycle_start; v = parent[v]){
            cycle.push_back(v);
        }
        reverse(cycle.begin(), cycle.end());
        return cycle;
    }
}
int main(){
    fastio();
    ll m;
    cin >> n >> m;
    vector<pair<ll,ll>> edges;
    rep(i,n){
        cin >> arr[i];
        upedge[i] = -1;
    }
    rep(i,m){
        ll u,v;
        cin >> u >> v;
        --u,--v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        edges.push_back(make_pair(u,v));
    }
    if(m == n-1){
        assert(0 == 1);
        rep(i,n){
            if(adj[i].size() == 1){
                upedge[i] = arr[i];
            }
        }
        dfs1(0,-1);
        parents(0,-1);
        par[0] = -1;
        map<pair<ll,ll>,ll> mp;
        rep(i,n){
            if(par[i] == -1){
                continue;
            }
            else{
                mp[{i,par[i]}] = upedge[i];
                mp[{par[i],i}] = upedge[i];
            }
        }
        rep(i,edges.size()){
            ll u = edges[i].first;
            ll v = edges[i].second;
            cout << 2*mp[{u,v}] << endl;
        }
    }
    else if(m == n){
        // one cycle exists so just find that one cycle done?
        vector<ll> cycle = find_cycle();
        rep(i,cycle.size()){
            ngood[cycle[i]] = -1;
        }
        if(cycle.size() % 2 == 0){
            cout << 0 << endl;
        }
        else{
            rep(j,cycle.size()){
                ll i = cycle[j];
                    dfs2(i,-1);
                    parents2(i,-1);
                    par[i] = -1;
            }
            map<pair<ll,ll>,ll> mp;
            map<pair<ll,ll>,ll> mp2;
            rep(i,n){
                if(par[i] != -1){
                    mp[{par[i],i}] = upedge[i];
                    mp[{i,par[i]}] = upedge[i];
                    mp2[{par[i],i}] = 2*upedge[i];
                    mp2[{i,par[i]}] = 2*upedge[i];
                }
            }
            rep(i,cycle.size()){
                for(auto u : adj[cycle[i]]){
                    if(ngood[u] == -1){
                        continue;
                    }
                    else{
                        arr[cycle[i]] = arr[cycle[i]] - mp[{cycle[i],u}];
                    }
                }
            }
            ll val = arr[cycle[0]];
            f(i,1,cycle.size()){
                if(i%2 == 1){
                    val += arr[cycle[i]];
                }
                else{
                    val = val - arr[cycle[i]];
                }
            }
            mp2[{cycle[0],cycle[1]}] = val;
            f(i,1,cycle.size()-1){
                mp2[{cycle[i],cycle[i+1]}] = 2*arr[cycle[i]] - mp2[{cycle[i-1],cycle[i]}];
                mp2[{cycle[i+1],cycle[i]}] = mp2[{cycle[i],cycle[i+1]}];
            }
            mp2[{cycle[cycle.size()-1],cycle[0]}] = 2*arr[cycle[cycle.size()-1]] - mp2[{cycle[cycle.size()-1],cycle[cycle.size()-2]}];
            mp2[{cycle[0],cycle[cycle.size()-1]}] = mp2[{cycle[cycle.size()-1],cycle[0]}];
            rep(i,edges.size()){
                cout << mp2[{edges[i].first,edges[i].second}] << endl;
            }
        }
    }
    else{
        cout << 0 << endl;
    }
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:5:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,s) for(ll i=0; i<s ; i++)
......
  152 |         rep(i,edges.size()){
      |             ~~~~~~~~~~~~~~     
pipes.cpp:152:9: note: in expansion of macro 'rep'
  152 |         rep(i,edges.size()){
      |         ^~~
pipes.cpp:5:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,s) for(ll i=0; i<s ; i++)
......
  161 |         rep(i,cycle.size()){
      |             ~~~~~~~~~~~~~~     
pipes.cpp:161:9: note: in expansion of macro 'rep'
  161 |         rep(i,cycle.size()){
      |         ^~~
pipes.cpp:5:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,s) for(ll i=0; i<s ; i++)
......
  168 |             rep(j,cycle.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:168:13: note: in expansion of macro 'rep'
  168 |             rep(j,cycle.size()){
      |             ^~~
pipes.cpp:5:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,s) for(ll i=0; i<s ; i++)
......
  184 |             rep(i,cycle.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:184:13: note: in expansion of macro 'rep'
  184 |             rep(i,cycle.size()){
      |             ^~~
pipes.cpp:6:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define f(i,a,b) for(ll i(a); i<b ; i++)
......
  195 |             f(i,1,cycle.size()){
      |               ~~~~~~~~~~~~~~~~  
pipes.cpp:195:13: note: in expansion of macro 'f'
  195 |             f(i,1,cycle.size()){
      |             ^
pipes.cpp:6:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    6 | #define f(i,a,b) for(ll i(a); i<b ; i++)
......
  204 |             f(i,1,cycle.size()-1){
      |               ~~~~~~~~~~~~~~~~~~
pipes.cpp:204:13: note: in expansion of macro 'f'
  204 |             f(i,1,cycle.size()-1){
      |             ^
pipes.cpp:5:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    5 | #define rep(i,s) for(ll i=0; i<s ; i++)
......
  210 |             rep(i,edges.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:210:13: note: in expansion of macro 'rep'
  210 |             rep(i,edges.size()){
      |             ^~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 5 ms 5196 KB Execution killed with signal 6
2 Runtime error 4 ms 5196 KB Execution killed with signal 6
3 Runtime error 5 ms 5428 KB Execution killed with signal 6
4 Runtime error 60 ms 19144 KB Execution killed with signal 6
5 Runtime error 5 ms 5196 KB Execution killed with signal 6
6 Runtime error 6 ms 5244 KB Execution killed with signal 6
7 Runtime error 5 ms 5240 KB Execution killed with signal 6
8 Runtime error 5 ms 5196 KB Execution killed with signal 6
9 Runtime error 5 ms 5324 KB Execution killed with signal 6
10 Runtime error 6 ms 5340 KB Execution killed with signal 6
11 Runtime error 5 ms 5316 KB Execution killed with signal 6
12 Runtime error 5 ms 5324 KB Execution killed with signal 6
13 Runtime error 50 ms 16288 KB Execution killed with signal 6
14 Runtime error 55 ms 18352 KB Execution killed with signal 6
15 Runtime error 62 ms 19204 KB Execution killed with signal 6
16 Runtime error 49 ms 17024 KB Execution killed with signal 6
17 Runtime error 56 ms 19052 KB Execution killed with signal 6
18 Runtime error 62 ms 19080 KB Execution killed with signal 6
19 Runtime error 57 ms 17952 KB Execution killed with signal 6
20 Runtime error 4 ms 5196 KB Execution killed with signal 6
21 Runtime error 5 ms 5324 KB Execution killed with signal 6
22 Runtime error 55 ms 19120 KB Execution killed with signal 6
23 Runtime error 46 ms 16220 KB Execution killed with signal 6
24 Runtime error 60 ms 19200 KB Execution killed with signal 6
25 Runtime error 63 ms 16820 KB Execution killed with signal 6
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 5 ms 2892 KB Output is correct
3 Correct 54 ms 13304 KB Output is correct
4 Correct 53 ms 8968 KB Output is correct
5 Correct 48 ms 9140 KB Output is correct
6 Correct 214 ms 25972 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2636 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Incorrect 2 ms 2636 KB Output isn't correct
15 Correct 4 ms 2892 KB Output is correct
16 Correct 4 ms 2892 KB Output is correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 3 ms 2636 KB Output is correct
19 Correct 2 ms 2636 KB Output is correct
20 Correct 2 ms 2764 KB Output is correct
21 Correct 3 ms 2764 KB Output is correct
22 Correct 5 ms 2892 KB Output is correct
23 Incorrect 350 ms 26928 KB Output isn't correct
24 Correct 472 ms 33252 KB Output is correct
25 Correct 63 ms 13244 KB Output is correct
26 Correct 59 ms 9052 KB Output is correct
27 Correct 48 ms 8868 KB Output is correct
28 Correct 58 ms 9432 KB Output is correct
29 Correct 197 ms 21660 KB Output is correct
30 Incorrect 436 ms 35016 KB Output isn't correct
31 Incorrect 417 ms 30596 KB Output isn't correct
32 Correct 486 ms 36016 KB Output is correct
33 Correct 58 ms 14260 KB Output is correct
34 Correct 55 ms 9064 KB Output is correct
35 Correct 60 ms 8992 KB Output is correct
36 Correct 49 ms 9296 KB Output is correct
37 Correct 211 ms 25852 KB Output is correct
38 Correct 497 ms 33936 KB Output is correct
39 Correct 474 ms 36340 KB Output is correct
40 Correct 439 ms 33748 KB Output is correct
41 Correct 62 ms 16304 KB Output is correct
42 Correct 50 ms 8828 KB Output is correct
43 Correct 51 ms 8944 KB Output is correct
44 Correct 64 ms 9208 KB Output is correct
45 Correct 173 ms 22664 KB Output is correct
46 Incorrect 467 ms 36472 KB Output isn't correct
47 Correct 445 ms 33496 KB Output is correct
48 Correct 411 ms 30924 KB Output is correct
49 Correct 57 ms 11448 KB Output is correct
50 Correct 50 ms 9136 KB Output is correct
51 Correct 56 ms 9148 KB Output is correct
52 Correct 51 ms 8968 KB Output is correct
53 Correct 172 ms 22884 KB Output is correct
54 Incorrect 481 ms 35512 KB Output isn't correct