Submission #712400

# Submission time Handle Problem Language Result Execution time Memory
712400 2023-03-18T18:13:59 Z Ronin13 Swapping Cities (APIO20_swap) C++14
100 / 100
307 ms 104308 KB
#include "swap.h"
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
#define ull unsigned ll
#include <vector>

/*
5 6
0 1 4
0 2 4
1 2 1
1 3 2
1 4 10
2 3 3
3
1 2
2 4
0 1

*/
using namespace std;


const int nmax = 1000000;

vector <vector <pii> > g(nmax);

vector <vector <int> > cmp(nmax);

vector <pii> par[nmax];



int val[nmax];
int l[nmax], r[nmax];
void make_set(int v){
    par[v].pb({v, 0});
    cmp[v].pb(v);
    val[v] = 1e9 + 1;
    l[v] = r[v] = v;
}

void union_sets(int a, int b, int len){
    if(par[a].back().f == par[b].back().f){
        if(val[a] == 1e9 +1){
            int x = par[a].back().f;
            for(int to : cmp[x]){
                val[to] = len;
            }
        }
        return;
    }
    int x = par[a].back().f, y = par[b].back().f;
    if(cmp[x].size() < cmp[y].size())
        swap(x, y), swap(a, b);
    for(int to : cmp[y])
        cmp[x].pb(to), par[to].pb({x, len});
    if(val[x] == 1e9 + 1 || val[y] == 1e9 + 1){
        int mn = min(val[x], val[y]);
        if(mn != 1e9 + 1){
            if(mn == val[y])
            for(int to : cmp[x])
                val[to] = min(val[to], len);
            else for(int to : cmp[y])
                val[to] = min(val[to], len);
            return;
        }
    }
    if(val[x] == 1e9 + 1 && val[y] == 1e9 + 1){
    if(l[x] == a && l[y] == b){
        l[x] = r[y];
        return;
    }
    if(l[x] == a && r[y] == b){
        l[x] = l[y];
        return;
    }
    if(r[x] == a && l[y] == b){
        r[x] = r[y];
        return;
    }
    if(r[x] == a && r[y] == b){
        r[x] = l[y];
        return;
    }
    for(int to : cmp[x])
        val[to] = len;
    }
}

void init(int N, int M,
          std::vector<int> U, std::vector<int> V, std::vector<int> W) {
    int n = N;
    vector <pair <int, pii> > ed;
    for(int i = 0; i < n; i++)
        make_set(i);
    for(int i = 0; i < M; i++){
        ed.pb({W[i], {U[i], V[i]}});
    }
    sort(ed.begin(), ed.end());
    for(int i = 0; i < ed.size(); i++){
        union_sets(ed[i].s.f, ed[i].s.s, ed[i].f);
        //cout << 1;
    }
}

int getMinimumFuelCapacity(int X, int Y) {
    if(val[X] == 1e9 + 1)
        return -1;
    int x = X, y = Y;
    int sz = par[x].size() - 1;
    int sz2 = par[y].size() - 1;
    int ans = 1e9 + 1;
    while(sz >= 0 && sz2 >= 0){

        if(par[x][sz].f== par[y][sz2].f)
            ans = min(ans, max(par[x][sz].s, par[y][sz2].s));
        sz--;
        sz2--;
    }
  int u = max(min(val[X],val[y]), ans);
  if(u == 1e9 + 1)
    u = -1;
  return u;
}

Compilation message

swap.cpp: In function 'void init(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
swap.cpp:107:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |     for(int i = 0; i < ed.size(); i++){
      |                    ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Correct 33 ms 70724 KB Output is correct
3 Correct 33 ms 70656 KB Output is correct
4 Correct 34 ms 70788 KB Output is correct
5 Correct 36 ms 70956 KB Output is correct
6 Correct 39 ms 70912 KB Output is correct
7 Correct 36 ms 70848 KB Output is correct
8 Correct 38 ms 70856 KB Output is correct
9 Correct 149 ms 87848 KB Output is correct
10 Correct 188 ms 91244 KB Output is correct
11 Correct 183 ms 90936 KB Output is correct
12 Correct 182 ms 92136 KB Output is correct
13 Correct 137 ms 84656 KB Output is correct
14 Correct 170 ms 87840 KB Output is correct
15 Correct 220 ms 93300 KB Output is correct
16 Correct 221 ms 92584 KB Output is correct
17 Correct 239 ms 94020 KB Output is correct
18 Correct 174 ms 86304 KB Output is correct
19 Correct 96 ms 77216 KB Output is correct
20 Correct 264 ms 93528 KB Output is correct
21 Correct 240 ms 92992 KB Output is correct
22 Correct 251 ms 94376 KB Output is correct
23 Correct 189 ms 86672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Correct 33 ms 70724 KB Output is correct
3 Correct 174 ms 83840 KB Output is correct
4 Correct 166 ms 88112 KB Output is correct
5 Correct 178 ms 88232 KB Output is correct
6 Correct 182 ms 88012 KB Output is correct
7 Correct 186 ms 88304 KB Output is correct
8 Correct 167 ms 87796 KB Output is correct
9 Correct 166 ms 88048 KB Output is correct
10 Correct 166 ms 87604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Correct 33 ms 70724 KB Output is correct
3 Correct 33 ms 70656 KB Output is correct
4 Correct 34 ms 70788 KB Output is correct
5 Correct 36 ms 70956 KB Output is correct
6 Correct 39 ms 70912 KB Output is correct
7 Correct 36 ms 70848 KB Output is correct
8 Correct 38 ms 70856 KB Output is correct
9 Correct 36 ms 70652 KB Output is correct
10 Correct 35 ms 70864 KB Output is correct
11 Correct 36 ms 70860 KB Output is correct
12 Correct 40 ms 70912 KB Output is correct
13 Correct 36 ms 70904 KB Output is correct
14 Correct 37 ms 70892 KB Output is correct
15 Correct 35 ms 70860 KB Output is correct
16 Correct 35 ms 70896 KB Output is correct
17 Correct 35 ms 70868 KB Output is correct
18 Correct 35 ms 70984 KB Output is correct
19 Correct 35 ms 70860 KB Output is correct
20 Correct 38 ms 70956 KB Output is correct
21 Correct 37 ms 70908 KB Output is correct
22 Correct 39 ms 70832 KB Output is correct
23 Correct 35 ms 70836 KB Output is correct
24 Correct 36 ms 70920 KB Output is correct
25 Correct 36 ms 71044 KB Output is correct
26 Correct 36 ms 70992 KB Output is correct
27 Correct 36 ms 70900 KB Output is correct
28 Correct 35 ms 70992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 70652 KB Output is correct
2 Correct 33 ms 70740 KB Output is correct
3 Correct 33 ms 70724 KB Output is correct
4 Correct 33 ms 70656 KB Output is correct
5 Correct 34 ms 70788 KB Output is correct
6 Correct 36 ms 70956 KB Output is correct
7 Correct 39 ms 70912 KB Output is correct
8 Correct 36 ms 70848 KB Output is correct
9 Correct 38 ms 70856 KB Output is correct
10 Correct 149 ms 87848 KB Output is correct
11 Correct 188 ms 91244 KB Output is correct
12 Correct 183 ms 90936 KB Output is correct
13 Correct 182 ms 92136 KB Output is correct
14 Correct 137 ms 84656 KB Output is correct
15 Correct 35 ms 70864 KB Output is correct
16 Correct 36 ms 70860 KB Output is correct
17 Correct 40 ms 70912 KB Output is correct
18 Correct 36 ms 70904 KB Output is correct
19 Correct 37 ms 70892 KB Output is correct
20 Correct 35 ms 70860 KB Output is correct
21 Correct 35 ms 70896 KB Output is correct
22 Correct 35 ms 70868 KB Output is correct
23 Correct 35 ms 70984 KB Output is correct
24 Correct 35 ms 70860 KB Output is correct
25 Correct 38 ms 70956 KB Output is correct
26 Correct 37 ms 70908 KB Output is correct
27 Correct 39 ms 70832 KB Output is correct
28 Correct 35 ms 70836 KB Output is correct
29 Correct 36 ms 70920 KB Output is correct
30 Correct 36 ms 71044 KB Output is correct
31 Correct 36 ms 70992 KB Output is correct
32 Correct 36 ms 70900 KB Output is correct
33 Correct 35 ms 70992 KB Output is correct
34 Correct 47 ms 73216 KB Output is correct
35 Correct 193 ms 92012 KB Output is correct
36 Correct 179 ms 91788 KB Output is correct
37 Correct 189 ms 91336 KB Output is correct
38 Correct 179 ms 90484 KB Output is correct
39 Correct 177 ms 90064 KB Output is correct
40 Correct 161 ms 88176 KB Output is correct
41 Correct 196 ms 92548 KB Output is correct
42 Correct 184 ms 92436 KB Output is correct
43 Correct 121 ms 83944 KB Output is correct
44 Correct 173 ms 91968 KB Output is correct
45 Correct 153 ms 87948 KB Output is correct
46 Correct 215 ms 93632 KB Output is correct
47 Correct 170 ms 90164 KB Output is correct
48 Correct 142 ms 87648 KB Output is correct
49 Correct 92 ms 80532 KB Output is correct
50 Correct 82 ms 78152 KB Output is correct
51 Correct 127 ms 85068 KB Output is correct
52 Correct 218 ms 96536 KB Output is correct
53 Correct 204 ms 92944 KB Output is correct
54 Correct 244 ms 99860 KB Output is correct
55 Correct 129 ms 86080 KB Output is correct
56 Correct 175 ms 91660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 70740 KB Output is correct
2 Correct 33 ms 70724 KB Output is correct
3 Correct 33 ms 70656 KB Output is correct
4 Correct 34 ms 70788 KB Output is correct
5 Correct 36 ms 70956 KB Output is correct
6 Correct 39 ms 70912 KB Output is correct
7 Correct 36 ms 70848 KB Output is correct
8 Correct 38 ms 70856 KB Output is correct
9 Correct 149 ms 87848 KB Output is correct
10 Correct 188 ms 91244 KB Output is correct
11 Correct 183 ms 90936 KB Output is correct
12 Correct 182 ms 92136 KB Output is correct
13 Correct 137 ms 84656 KB Output is correct
14 Correct 170 ms 87840 KB Output is correct
15 Correct 220 ms 93300 KB Output is correct
16 Correct 221 ms 92584 KB Output is correct
17 Correct 239 ms 94020 KB Output is correct
18 Correct 174 ms 86304 KB Output is correct
19 Correct 174 ms 83840 KB Output is correct
20 Correct 166 ms 88112 KB Output is correct
21 Correct 178 ms 88232 KB Output is correct
22 Correct 182 ms 88012 KB Output is correct
23 Correct 186 ms 88304 KB Output is correct
24 Correct 167 ms 87796 KB Output is correct
25 Correct 166 ms 88048 KB Output is correct
26 Correct 166 ms 87604 KB Output is correct
27 Correct 35 ms 70864 KB Output is correct
28 Correct 36 ms 70860 KB Output is correct
29 Correct 40 ms 70912 KB Output is correct
30 Correct 36 ms 70904 KB Output is correct
31 Correct 37 ms 70892 KB Output is correct
32 Correct 35 ms 70860 KB Output is correct
33 Correct 35 ms 70896 KB Output is correct
34 Correct 35 ms 70868 KB Output is correct
35 Correct 35 ms 70984 KB Output is correct
36 Correct 47 ms 73216 KB Output is correct
37 Correct 193 ms 92012 KB Output is correct
38 Correct 179 ms 91788 KB Output is correct
39 Correct 189 ms 91336 KB Output is correct
40 Correct 179 ms 90484 KB Output is correct
41 Correct 177 ms 90064 KB Output is correct
42 Correct 161 ms 88176 KB Output is correct
43 Correct 196 ms 92548 KB Output is correct
44 Correct 184 ms 92436 KB Output is correct
45 Correct 121 ms 83944 KB Output is correct
46 Correct 173 ms 91968 KB Output is correct
47 Correct 51 ms 73744 KB Output is correct
48 Correct 260 ms 97916 KB Output is correct
49 Correct 267 ms 97148 KB Output is correct
50 Correct 263 ms 97144 KB Output is correct
51 Correct 262 ms 96580 KB Output is correct
52 Correct 225 ms 94720 KB Output is correct
53 Correct 186 ms 89868 KB Output is correct
54 Correct 262 ms 98548 KB Output is correct
55 Correct 272 ms 98528 KB Output is correct
56 Correct 191 ms 90176 KB Output is correct
57 Correct 240 ms 95948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 70652 KB Output is correct
2 Correct 33 ms 70740 KB Output is correct
3 Correct 33 ms 70724 KB Output is correct
4 Correct 33 ms 70656 KB Output is correct
5 Correct 34 ms 70788 KB Output is correct
6 Correct 36 ms 70956 KB Output is correct
7 Correct 39 ms 70912 KB Output is correct
8 Correct 36 ms 70848 KB Output is correct
9 Correct 38 ms 70856 KB Output is correct
10 Correct 149 ms 87848 KB Output is correct
11 Correct 188 ms 91244 KB Output is correct
12 Correct 183 ms 90936 KB Output is correct
13 Correct 182 ms 92136 KB Output is correct
14 Correct 137 ms 84656 KB Output is correct
15 Correct 170 ms 87840 KB Output is correct
16 Correct 220 ms 93300 KB Output is correct
17 Correct 221 ms 92584 KB Output is correct
18 Correct 239 ms 94020 KB Output is correct
19 Correct 174 ms 86304 KB Output is correct
20 Correct 174 ms 83840 KB Output is correct
21 Correct 166 ms 88112 KB Output is correct
22 Correct 178 ms 88232 KB Output is correct
23 Correct 182 ms 88012 KB Output is correct
24 Correct 186 ms 88304 KB Output is correct
25 Correct 167 ms 87796 KB Output is correct
26 Correct 166 ms 88048 KB Output is correct
27 Correct 166 ms 87604 KB Output is correct
28 Correct 35 ms 70864 KB Output is correct
29 Correct 36 ms 70860 KB Output is correct
30 Correct 40 ms 70912 KB Output is correct
31 Correct 36 ms 70904 KB Output is correct
32 Correct 37 ms 70892 KB Output is correct
33 Correct 35 ms 70860 KB Output is correct
34 Correct 35 ms 70896 KB Output is correct
35 Correct 35 ms 70868 KB Output is correct
36 Correct 35 ms 70984 KB Output is correct
37 Correct 47 ms 73216 KB Output is correct
38 Correct 193 ms 92012 KB Output is correct
39 Correct 179 ms 91788 KB Output is correct
40 Correct 189 ms 91336 KB Output is correct
41 Correct 179 ms 90484 KB Output is correct
42 Correct 177 ms 90064 KB Output is correct
43 Correct 161 ms 88176 KB Output is correct
44 Correct 196 ms 92548 KB Output is correct
45 Correct 184 ms 92436 KB Output is correct
46 Correct 121 ms 83944 KB Output is correct
47 Correct 173 ms 91968 KB Output is correct
48 Correct 51 ms 73744 KB Output is correct
49 Correct 260 ms 97916 KB Output is correct
50 Correct 267 ms 97148 KB Output is correct
51 Correct 263 ms 97144 KB Output is correct
52 Correct 262 ms 96580 KB Output is correct
53 Correct 225 ms 94720 KB Output is correct
54 Correct 186 ms 89868 KB Output is correct
55 Correct 262 ms 98548 KB Output is correct
56 Correct 272 ms 98528 KB Output is correct
57 Correct 191 ms 90176 KB Output is correct
58 Correct 240 ms 95948 KB Output is correct
59 Correct 96 ms 77216 KB Output is correct
60 Correct 264 ms 93528 KB Output is correct
61 Correct 240 ms 92992 KB Output is correct
62 Correct 251 ms 94376 KB Output is correct
63 Correct 189 ms 86672 KB Output is correct
64 Correct 35 ms 70860 KB Output is correct
65 Correct 38 ms 70956 KB Output is correct
66 Correct 37 ms 70908 KB Output is correct
67 Correct 39 ms 70832 KB Output is correct
68 Correct 35 ms 70836 KB Output is correct
69 Correct 36 ms 70920 KB Output is correct
70 Correct 36 ms 71044 KB Output is correct
71 Correct 36 ms 70992 KB Output is correct
72 Correct 36 ms 70900 KB Output is correct
73 Correct 35 ms 70992 KB Output is correct
74 Correct 153 ms 87948 KB Output is correct
75 Correct 215 ms 93632 KB Output is correct
76 Correct 170 ms 90164 KB Output is correct
77 Correct 142 ms 87648 KB Output is correct
78 Correct 92 ms 80532 KB Output is correct
79 Correct 82 ms 78152 KB Output is correct
80 Correct 127 ms 85068 KB Output is correct
81 Correct 218 ms 96536 KB Output is correct
82 Correct 204 ms 92944 KB Output is correct
83 Correct 244 ms 99860 KB Output is correct
84 Correct 129 ms 86080 KB Output is correct
85 Correct 175 ms 91660 KB Output is correct
86 Correct 82 ms 77980 KB Output is correct
87 Correct 268 ms 96864 KB Output is correct
88 Correct 272 ms 96992 KB Output is correct
89 Correct 200 ms 90692 KB Output is correct
90 Correct 154 ms 84288 KB Output is correct
91 Correct 157 ms 84656 KB Output is correct
92 Correct 180 ms 88592 KB Output is correct
93 Correct 293 ms 99544 KB Output is correct
94 Correct 269 ms 97180 KB Output is correct
95 Correct 307 ms 104308 KB Output is correct
96 Correct 202 ms 90208 KB Output is correct
97 Correct 226 ms 93792 KB Output is correct