Submission #462435

# Submission time Handle Problem Language Result Execution time Memory
462435 2021-08-10T14:29:05 Z Yeboi Pipes (BOI13_pipes) C++14
44.0741 / 100
525 ms 38244 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){
        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(i,cycle.size()){
                cout << cycle[i] << " ";
            }
            cout << endl;
            rep(i,n){
                if(ngood[i] == -1){
                    dfs2(i,-1);
                    parents2(i,-1);
                    par[i] = -1;
                }
                else{
                    continue;
                }
            }
            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++)
......
  151 |         rep(i,edges.size()){
      |             ~~~~~~~~~~~~~~     
pipes.cpp:151:9: note: in expansion of macro 'rep'
  151 |         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++)
......
  160 |         rep(i,cycle.size()){
      |             ~~~~~~~~~~~~~~     
pipes.cpp:160:9: note: in expansion of macro 'rep'
  160 |         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++)
......
  167 |             rep(i,cycle.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:167:13: note: in expansion of macro 'rep'
  167 |             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++)
......
  191 |             rep(i,cycle.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:191:13: note: in expansion of macro 'rep'
  191 |             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++)
......
  202 |             f(i,1,cycle.size()){
      |               ~~~~~~~~~~~~~~~~  
pipes.cpp:202:13: note: in expansion of macro 'f'
  202 |             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++)
......
  211 |             f(i,1,cycle.size()-1){
      |               ~~~~~~~~~~~~~~~~~~
pipes.cpp:211:13: note: in expansion of macro 'f'
  211 |             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++)
......
  217 |             rep(i,edges.size()){
      |                 ~~~~~~~~~~~~~~ 
pipes.cpp:217:13: note: in expansion of macro 'rep'
  217 |             rep(i,edges.size()){
      |             ^~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Incorrect 2 ms 2636 KB Output isn't correct
3 Incorrect 4 ms 2816 KB Output isn't correct
4 Incorrect 359 ms 24768 KB Output isn't correct
5 Incorrect 2 ms 2636 KB Output isn't correct
6 Incorrect 3 ms 2672 KB Output isn't correct
7 Incorrect 3 ms 2636 KB Output isn't correct
8 Incorrect 2 ms 2636 KB Output isn't correct
9 Incorrect 4 ms 2808 KB Output isn't correct
10 Incorrect 4 ms 2892 KB Output isn't correct
11 Incorrect 4 ms 2892 KB Output isn't correct
12 Incorrect 4 ms 2892 KB Output isn't correct
13 Incorrect 262 ms 20232 KB Output isn't correct
14 Incorrect 325 ms 23500 KB Output isn't correct
15 Incorrect 377 ms 24832 KB Output isn't correct
16 Incorrect 288 ms 21424 KB Output isn't correct
17 Incorrect 325 ms 24740 KB Output isn't correct
18 Incorrect 330 ms 24828 KB Output isn't correct
19 Incorrect 379 ms 28304 KB Output isn't correct
20 Incorrect 2 ms 2636 KB Output isn't correct
21 Incorrect 4 ms 2892 KB Output isn't correct
22 Incorrect 383 ms 24740 KB Output isn't correct
23 Incorrect 258 ms 20272 KB Output isn't correct
24 Incorrect 381 ms 24816 KB Output isn't correct
25 Incorrect 276 ms 21112 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2636 KB Output isn't correct
2 Incorrect 5 ms 2892 KB Output isn't correct
3 Correct 64 ms 14820 KB Output is correct
4 Correct 63 ms 10544 KB Output is correct
5 Correct 48 ms 10732 KB Output is correct
6 Correct 225 ms 32140 KB Output is correct
7 Incorrect 2 ms 2636 KB Output isn't correct
8 Incorrect 3 ms 2672 KB Output isn't correct
9 Correct 2 ms 2676 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 2668 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Incorrect 2 ms 2636 KB Output isn't correct
15 Incorrect 5 ms 2944 KB Output isn't correct
16 Incorrect 5 ms 2884 KB Output isn't correct
17 Correct 2 ms 2764 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 3 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 Incorrect 5 ms 3020 KB Output isn't correct
23 Incorrect 331 ms 28444 KB Output isn't correct
24 Incorrect 446 ms 35156 KB Output isn't correct
25 Correct 60 ms 14856 KB Output is correct
26 Correct 52 ms 10580 KB Output is correct
27 Correct 54 ms 10516 KB Output is correct
28 Correct 58 ms 11092 KB Output is correct
29 Correct 173 ms 26800 KB Output is correct
30 Incorrect 439 ms 36792 KB Output isn't correct
31 Incorrect 429 ms 32804 KB Output isn't correct
32 Incorrect 525 ms 37616 KB Output isn't correct
33 Correct 60 ms 15876 KB Output is correct
34 Correct 50 ms 10604 KB Output is correct
35 Correct 55 ms 10648 KB Output is correct
36 Correct 50 ms 10932 KB Output is correct
37 Correct 221 ms 32028 KB Output is correct
38 Incorrect 464 ms 35712 KB Output isn't correct
39 Incorrect 463 ms 37980 KB Output isn't correct
40 Incorrect 456 ms 35556 KB Output isn't correct
41 Correct 65 ms 17908 KB Output is correct
42 Correct 56 ms 10432 KB Output is correct
43 Correct 54 ms 10436 KB Output is correct
44 Correct 48 ms 10660 KB Output is correct
45 Correct 178 ms 28004 KB Output is correct
46 Incorrect 515 ms 38244 KB Output isn't correct
47 Incorrect 488 ms 35696 KB Output isn't correct
48 Incorrect 401 ms 33072 KB Output isn't correct
49 Correct 51 ms 12956 KB Output is correct
50 Correct 49 ms 10676 KB Output is correct
51 Correct 51 ms 10772 KB Output is correct
52 Correct 56 ms 10516 KB Output is correct
53 Correct 192 ms 28332 KB Output is correct
54 Incorrect 503 ms 37256 KB Output isn't correct