Submission #651484

# Submission time Handle Problem Language Result Execution time Memory
651484 2022-10-19T01:23:33 Z perchuts Swapping Cities (APIO20_swap) C++17
43 / 100
308 ms 35800 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;

const int inf = 2e9+1;
const int mod = 1e9+7;
const int maxn = 1e5+100;

template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }

vector<tuple<int,int,int>>edges;

int lvl[maxn], par[maxn], when[maxn];

ii pontas[maxn];

vector<int>comp[maxn];
vector<ii>parents[maxn];

int findp(int x){return par[x] = (par[x]==x?x:findp(par[x]));}

void merge(int w,int u,int v){
    int pu = findp(u), pv = findp(v), ciclo = 0;
    if(lvl[pu]<lvl[pv])swap(pu,pv), swap(u,v);
    if(pu==pv)ciclo = 1;
    //check if someone's component turned into a non-path component
    par[pv] = pu;  
    if(when[pu]==inf&&when[pv]!=inf){
        for(auto x:comp[pu])when[x] = w;
    }else if(when[pu]!=inf&&when[pv]==inf){
        for(auto x:comp[pv])when[x] = w;
    }else if((ciclo||(u!=pontas[pu].first&&u!=pontas[pu].second)||
    (v!=pontas[pv].first&&v!=pontas[pv].second))&&when[pu]==inf&&
    when[pv]==inf){
        for(auto x:comp[pu])when[x] = w;
        if(!ciclo)for(auto x:comp[pv])when[x] = w;
    }
    if(ciclo)return;
    for(auto x:comp[pv]){
        comp[pu].pb(x);
        parents[x].pb({w,pu});
    }
    if(when[pu]==when[pv]&&when[pu]==inf){
        if(pontas[pu].first==u)swap(pontas[pu].first,pontas[pu].second);
        if(pontas[pv].first!=v)pontas[pu].second = pontas[pv].first;
        else pontas[pu].second = pontas[pv].second;
    }
    if(lvl[pu]==lvl[pv])++lvl[pu];
}

void init(int N, int M,vector<int> U, vector<int> V, vector<int> W) {

    for(int i=0;i<M;++i){
        edges.pb({W[i], U[i], V[i]});
    }

    for(int i=0;i<N;++i){
        par[i] = i, pontas[i] = {i,i}, when[i] = inf;
        parents[i].pb({0,i}), comp[i].pb(i);
    }

    sort(all(edges));

    for(auto [w,u,v]:edges){
        merge(w,u,v);
    }
    
    for(int i=0;i<N;++i)reverse(all(parents[i]));
}

int getMinimumFuelCapacity(int X, int Y) {
    if(when[X]==inf)return -1;
    int last = 0;
    while(last!=min(sz(parents[X]), sz(parents[Y]))-1){
        if(parents[X][last]!=parents[Y][last])break;
        ++last;
    }
    auto [custo,pai] = parents[X][last];
    assert(pai==parents[Y][last].second);
    ckmax(custo, parents[Y][last].first);
    return max(custo, min(when[X], when[Y]));
}

// int main() {
//   int N, M; 
//   assert(2 == scanf("%d %d", &N, &M));
  
//   std::vector<int> U(M), V(M), W(M);
//   for (int i = 0; i < M; ++i) {
//     assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
//   }

//   int Q;
//   assert(1 == scanf("%d", &Q));

//   std::vector<int> X(Q), Y(Q);
//   for (int i = 0; i < Q; ++i) {
//     assert(2 == scanf("%d %d", &X[i], &Y[i]));
//   }

//   init(N, M, U, V, W);
  
//   std::vector<int> minimum_fuel_capacities(Q);
//   for (int i = 0; i < Q; ++i) {
//     minimum_fuel_capacities[i] = getMinimumFuelCapacity(X[i], Y[i]);
//   }

//   for (int i = 0; i < Q; ++i) {
//     printf("%d\n", minimum_fuel_capacities[i]);
//   }
  
//   return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4996 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5144 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 139 ms 23712 KB Output is correct
10 Correct 180 ms 27712 KB Output is correct
11 Correct 179 ms 26692 KB Output is correct
12 Correct 226 ms 28440 KB Output is correct
13 Correct 118 ms 21504 KB Output is correct
14 Correct 178 ms 23592 KB Output is correct
15 Correct 265 ms 30064 KB Output is correct
16 Correct 238 ms 29336 KB Output is correct
17 Correct 298 ms 30644 KB Output is correct
18 Correct 199 ms 23604 KB Output is correct
19 Correct 65 ms 11904 KB Output is correct
20 Correct 252 ms 30528 KB Output is correct
21 Correct 282 ms 30360 KB Output is correct
22 Correct 308 ms 31780 KB Output is correct
23 Correct 168 ms 22532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4996 KB Output is correct
3 Runtime error 152 ms 35800 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4996 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5144 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 4 ms 5204 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 4 ms 5204 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 4 ms 5076 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 3 ms 5076 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
21 Correct 5 ms 5076 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 4 ms 5204 KB Output is correct
25 Correct 4 ms 5204 KB Output is correct
26 Correct 5 ms 5204 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 4 ms 5144 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 139 ms 23712 KB Output is correct
11 Correct 180 ms 27712 KB Output is correct
12 Correct 179 ms 26692 KB Output is correct
13 Correct 226 ms 28440 KB Output is correct
14 Correct 118 ms 21504 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5204 KB Output is correct
17 Correct 4 ms 5204 KB Output is correct
18 Correct 4 ms 5076 KB Output is correct
19 Correct 4 ms 5076 KB Output is correct
20 Correct 4 ms 5204 KB Output is correct
21 Correct 3 ms 5204 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 3 ms 5076 KB Output is correct
25 Correct 4 ms 5204 KB Output is correct
26 Correct 5 ms 5076 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 3 ms 5076 KB Output is correct
29 Correct 4 ms 5204 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 5 ms 5204 KB Output is correct
32 Correct 4 ms 5076 KB Output is correct
33 Correct 4 ms 5204 KB Output is correct
34 Correct 18 ms 7700 KB Output is correct
35 Correct 231 ms 28932 KB Output is correct
36 Correct 249 ms 28156 KB Output is correct
37 Correct 242 ms 27104 KB Output is correct
38 Correct 158 ms 26004 KB Output is correct
39 Correct 229 ms 26192 KB Output is correct
40 Correct 181 ms 23632 KB Output is correct
41 Correct 249 ms 28968 KB Output is correct
42 Correct 254 ms 29056 KB Output is correct
43 Correct 127 ms 20436 KB Output is correct
44 Correct 215 ms 26220 KB Output is correct
45 Correct 161 ms 19764 KB Output is correct
46 Correct 187 ms 28276 KB Output is correct
47 Correct 186 ms 23968 KB Output is correct
48 Correct 149 ms 20784 KB Output is correct
49 Correct 60 ms 11840 KB Output is correct
50 Correct 52 ms 10168 KB Output is correct
51 Correct 96 ms 17268 KB Output is correct
52 Correct 240 ms 28896 KB Output is correct
53 Correct 174 ms 24372 KB Output is correct
54 Correct 222 ms 31924 KB Output is correct
55 Correct 102 ms 19064 KB Output is correct
56 Correct 187 ms 23268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4996 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5144 KB Output is correct
6 Correct 5 ms 5204 KB Output is correct
7 Correct 4 ms 5204 KB Output is correct
8 Correct 3 ms 5076 KB Output is correct
9 Correct 139 ms 23712 KB Output is correct
10 Correct 180 ms 27712 KB Output is correct
11 Correct 179 ms 26692 KB Output is correct
12 Correct 226 ms 28440 KB Output is correct
13 Correct 118 ms 21504 KB Output is correct
14 Correct 178 ms 23592 KB Output is correct
15 Correct 265 ms 30064 KB Output is correct
16 Correct 238 ms 29336 KB Output is correct
17 Correct 298 ms 30644 KB Output is correct
18 Correct 199 ms 23604 KB Output is correct
19 Runtime error 152 ms 35800 KB Execution killed with signal 6
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4952 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4996 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 5076 KB Output is correct
6 Correct 4 ms 5144 KB Output is correct
7 Correct 5 ms 5204 KB Output is correct
8 Correct 4 ms 5204 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 139 ms 23712 KB Output is correct
11 Correct 180 ms 27712 KB Output is correct
12 Correct 179 ms 26692 KB Output is correct
13 Correct 226 ms 28440 KB Output is correct
14 Correct 118 ms 21504 KB Output is correct
15 Correct 178 ms 23592 KB Output is correct
16 Correct 265 ms 30064 KB Output is correct
17 Correct 238 ms 29336 KB Output is correct
18 Correct 298 ms 30644 KB Output is correct
19 Correct 199 ms 23604 KB Output is correct
20 Runtime error 152 ms 35800 KB Execution killed with signal 6
21 Halted 0 ms 0 KB -