Submission #357744

# Submission time Handle Problem Language Result Execution time Memory
357744 2021-01-24T15:12:45 Z AriaH Swapping Cities (APIO20_swap) C++11
100 / 100
460 ms 66668 KB
/** Im the best because i work as hard as i possibly can **/
 
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
 
typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
#define endl                        "\n"
 
const int N = 5e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 21;
 
struct edge
{
    int v, u, c;
} E[N];
 
bool cmp(edge a, edge b) { return a.c < b.c; }
 
int _n, ptr, D[N], par[N], ok[N], P[LOG][N], H[N], up[N];
 
vector < int > G[N];
 
int get(int x) { return (x == par[x]? x : par[x] = get(par[x])); }
 
void unify(int i)
{
    ++ ptr;
    D[E[i].v] ++;
    D[E[i].u] ++;
    int v = get(E[i].v), u = get(E[i].u);
    if(v == u)
    {
	ok[ptr] = 1;
	G[ptr].push_back(v);
	P[0][v] = par[v] = ptr;
	return;
    }
    ok[ptr] = max({ok[v], ok[u], int(D[E[i].v] > 2), int(D[E[i].u] > 2)});
    P[0][v] = P[0][u] = par[u] = par[v] = ptr;
    G[ptr].push_back(v);
    G[ptr].push_back(u);
}
 
void dfs(int v)
{
    if(ok[v] == 0) up[v] = up[P[0][v]];
    else up[v] = v;
    H[v] = H[P[0][v]] + 1;
    for(int T = 1; T < LOG; T ++)
	P[T][v] = P[T - 1][P[T - 1][v]];
    for(auto u : G[v])
	dfs(u);
}
 
int LCA(int v, int u)
{
    if(H[v] > H[u]) swap(u, v);
    int del = (H[u] - H[v]);
    for(int T = 0; T < LOG; T ++)
    {
	if(del >> T & 1)
	{
	    u = P[T][u];
	}
    }
    if(v == u) return v;
    for(int T = LOG - 1; ~T; T --)
    {
	if(P[T][v] != P[T][u])
	{
	    u = P[T][u];
	    v = P[T][v];
	}
    }
    return P[0][v];
}
 
void init(int n, int m, vector<int> U, vector<int> V, vector<int> W)
{
    _n = n;
    ptr = n;
    for(int i = 1; i <= m; i ++)
    {
	E[i] = {V[i - 1] + 1, U[i - 1] + 1, W[i - 1]};
    }
    iota(par, par + N, 0);
    sort(E + 1, E + m + 1, cmp);
    for(int i = 1; i <= m; i ++)
    {
	unify(i);
    }
    dfs(ptr);
}
 
int getMinimumFuelCapacity(int u, int v)
{
    u ++;
    v ++;
    int lca = LCA(u, v);
    if(up[lca] == 0)
    {
	return -1;
    }
    return E[up[lca] - _n].c;
}

/*
case 1:
5 6
1 2 4
1 3 4
3 4 3
4 2 2
2 5 10
2 3 1
3
2 3
3 5
1 2
case 2:
3 2
1 2 5
1 3 5
1
2 3
*/
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14208 KB Output is correct
3 Correct 11 ms 14188 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14460 KB Output is correct
9 Correct 126 ms 34300 KB Output is correct
10 Correct 155 ms 38636 KB Output is correct
11 Correct 152 ms 38252 KB Output is correct
12 Correct 164 ms 39660 KB Output is correct
13 Correct 131 ms 41196 KB Output is correct
14 Correct 142 ms 34412 KB Output is correct
15 Correct 345 ms 40692 KB Output is correct
16 Correct 328 ms 40088 KB Output is correct
17 Correct 355 ms 41680 KB Output is correct
18 Correct 432 ms 42960 KB Output is correct
19 Correct 109 ms 21868 KB Output is correct
20 Correct 359 ms 41964 KB Output is correct
21 Correct 347 ms 41252 KB Output is correct
22 Correct 370 ms 42860 KB Output is correct
23 Correct 425 ms 44368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14208 KB Output is correct
3 Correct 410 ms 44316 KB Output is correct
4 Correct 393 ms 45520 KB Output is correct
5 Correct 440 ms 44928 KB Output is correct
6 Correct 386 ms 45424 KB Output is correct
7 Correct 404 ms 45248 KB Output is correct
8 Correct 405 ms 44184 KB Output is correct
9 Correct 405 ms 45160 KB Output is correct
10 Correct 387 ms 43976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14208 KB Output is correct
3 Correct 11 ms 14188 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14460 KB Output is correct
9 Correct 10 ms 14188 KB Output is correct
10 Correct 11 ms 14444 KB Output is correct
11 Correct 11 ms 14444 KB Output is correct
12 Correct 11 ms 14444 KB Output is correct
13 Correct 11 ms 14444 KB Output is correct
14 Correct 11 ms 14444 KB Output is correct
15 Correct 11 ms 14444 KB Output is correct
16 Correct 10 ms 14444 KB Output is correct
17 Correct 11 ms 14444 KB Output is correct
18 Correct 11 ms 14444 KB Output is correct
19 Correct 10 ms 14444 KB Output is correct
20 Correct 11 ms 14444 KB Output is correct
21 Correct 11 ms 14444 KB Output is correct
22 Correct 11 ms 14572 KB Output is correct
23 Correct 11 ms 14444 KB Output is correct
24 Correct 11 ms 14700 KB Output is correct
25 Correct 11 ms 14700 KB Output is correct
26 Correct 11 ms 14700 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 11 ms 14700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14188 KB Output is correct
3 Correct 10 ms 14208 KB Output is correct
4 Correct 11 ms 14188 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 10 ms 14444 KB Output is correct
9 Correct 10 ms 14460 KB Output is correct
10 Correct 126 ms 34300 KB Output is correct
11 Correct 155 ms 38636 KB Output is correct
12 Correct 152 ms 38252 KB Output is correct
13 Correct 164 ms 39660 KB Output is correct
14 Correct 131 ms 41196 KB Output is correct
15 Correct 11 ms 14444 KB Output is correct
16 Correct 11 ms 14444 KB Output is correct
17 Correct 11 ms 14444 KB Output is correct
18 Correct 11 ms 14444 KB Output is correct
19 Correct 11 ms 14444 KB Output is correct
20 Correct 11 ms 14444 KB Output is correct
21 Correct 10 ms 14444 KB Output is correct
22 Correct 11 ms 14444 KB Output is correct
23 Correct 11 ms 14444 KB Output is correct
24 Correct 10 ms 14444 KB Output is correct
25 Correct 11 ms 14444 KB Output is correct
26 Correct 11 ms 14444 KB Output is correct
27 Correct 11 ms 14572 KB Output is correct
28 Correct 11 ms 14444 KB Output is correct
29 Correct 11 ms 14700 KB Output is correct
30 Correct 11 ms 14700 KB Output is correct
31 Correct 11 ms 14700 KB Output is correct
32 Correct 11 ms 14444 KB Output is correct
33 Correct 11 ms 14700 KB Output is correct
34 Correct 22 ms 17900 KB Output is correct
35 Correct 158 ms 41452 KB Output is correct
36 Correct 162 ms 41452 KB Output is correct
37 Correct 159 ms 41580 KB Output is correct
38 Correct 157 ms 41196 KB Output is correct
39 Correct 158 ms 40940 KB Output is correct
40 Correct 147 ms 38764 KB Output is correct
41 Correct 159 ms 41708 KB Output is correct
42 Correct 158 ms 41580 KB Output is correct
43 Correct 125 ms 43500 KB Output is correct
44 Correct 163 ms 42084 KB Output is correct
45 Correct 192 ms 51180 KB Output is correct
46 Correct 156 ms 41452 KB Output is correct
47 Correct 168 ms 41580 KB Output is correct
48 Correct 160 ms 44396 KB Output is correct
49 Correct 128 ms 47340 KB Output is correct
50 Correct 109 ms 40172 KB Output is correct
51 Correct 157 ms 46188 KB Output is correct
52 Correct 227 ms 54764 KB Output is correct
53 Correct 249 ms 57836 KB Output is correct
54 Correct 259 ms 62828 KB Output is correct
55 Correct 125 ms 43372 KB Output is correct
56 Correct 223 ms 57964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14208 KB Output is correct
3 Correct 11 ms 14188 KB Output is correct
4 Correct 10 ms 14336 KB Output is correct
5 Correct 10 ms 14444 KB Output is correct
6 Correct 11 ms 14444 KB Output is correct
7 Correct 10 ms 14444 KB Output is correct
8 Correct 10 ms 14460 KB Output is correct
9 Correct 126 ms 34300 KB Output is correct
10 Correct 155 ms 38636 KB Output is correct
11 Correct 152 ms 38252 KB Output is correct
12 Correct 164 ms 39660 KB Output is correct
13 Correct 131 ms 41196 KB Output is correct
14 Correct 142 ms 34412 KB Output is correct
15 Correct 345 ms 40692 KB Output is correct
16 Correct 328 ms 40088 KB Output is correct
17 Correct 355 ms 41680 KB Output is correct
18 Correct 432 ms 42960 KB Output is correct
19 Correct 410 ms 44316 KB Output is correct
20 Correct 393 ms 45520 KB Output is correct
21 Correct 440 ms 44928 KB Output is correct
22 Correct 386 ms 45424 KB Output is correct
23 Correct 404 ms 45248 KB Output is correct
24 Correct 405 ms 44184 KB Output is correct
25 Correct 405 ms 45160 KB Output is correct
26 Correct 387 ms 43976 KB Output is correct
27 Correct 11 ms 14444 KB Output is correct
28 Correct 11 ms 14444 KB Output is correct
29 Correct 11 ms 14444 KB Output is correct
30 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 11 ms 14444 KB Output is correct
33 Correct 10 ms 14444 KB Output is correct
34 Correct 11 ms 14444 KB Output is correct
35 Correct 11 ms 14444 KB Output is correct
36 Correct 22 ms 17900 KB Output is correct
37 Correct 158 ms 41452 KB Output is correct
38 Correct 162 ms 41452 KB Output is correct
39 Correct 159 ms 41580 KB Output is correct
40 Correct 157 ms 41196 KB Output is correct
41 Correct 158 ms 40940 KB Output is correct
42 Correct 147 ms 38764 KB Output is correct
43 Correct 159 ms 41708 KB Output is correct
44 Correct 158 ms 41580 KB Output is correct
45 Correct 125 ms 43500 KB Output is correct
46 Correct 163 ms 42084 KB Output is correct
47 Correct 32 ms 18284 KB Output is correct
48 Correct 341 ms 46436 KB Output is correct
49 Correct 359 ms 46472 KB Output is correct
50 Correct 340 ms 46288 KB Output is correct
51 Correct 342 ms 46172 KB Output is correct
52 Correct 332 ms 44696 KB Output is correct
53 Correct 283 ms 38240 KB Output is correct
54 Correct 351 ms 47184 KB Output is correct
55 Correct 343 ms 46544 KB Output is correct
56 Correct 423 ms 48008 KB Output is correct
57 Correct 372 ms 47312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 14188 KB Output is correct
2 Correct 10 ms 14188 KB Output is correct
3 Correct 10 ms 14208 KB Output is correct
4 Correct 11 ms 14188 KB Output is correct
5 Correct 10 ms 14336 KB Output is correct
6 Correct 10 ms 14444 KB Output is correct
7 Correct 11 ms 14444 KB Output is correct
8 Correct 10 ms 14444 KB Output is correct
9 Correct 10 ms 14460 KB Output is correct
10 Correct 126 ms 34300 KB Output is correct
11 Correct 155 ms 38636 KB Output is correct
12 Correct 152 ms 38252 KB Output is correct
13 Correct 164 ms 39660 KB Output is correct
14 Correct 131 ms 41196 KB Output is correct
15 Correct 142 ms 34412 KB Output is correct
16 Correct 345 ms 40692 KB Output is correct
17 Correct 328 ms 40088 KB Output is correct
18 Correct 355 ms 41680 KB Output is correct
19 Correct 432 ms 42960 KB Output is correct
20 Correct 410 ms 44316 KB Output is correct
21 Correct 393 ms 45520 KB Output is correct
22 Correct 440 ms 44928 KB Output is correct
23 Correct 386 ms 45424 KB Output is correct
24 Correct 404 ms 45248 KB Output is correct
25 Correct 405 ms 44184 KB Output is correct
26 Correct 405 ms 45160 KB Output is correct
27 Correct 387 ms 43976 KB Output is correct
28 Correct 11 ms 14444 KB Output is correct
29 Correct 11 ms 14444 KB Output is correct
30 Correct 11 ms 14444 KB Output is correct
31 Correct 11 ms 14444 KB Output is correct
32 Correct 11 ms 14444 KB Output is correct
33 Correct 11 ms 14444 KB Output is correct
34 Correct 10 ms 14444 KB Output is correct
35 Correct 11 ms 14444 KB Output is correct
36 Correct 11 ms 14444 KB Output is correct
37 Correct 22 ms 17900 KB Output is correct
38 Correct 158 ms 41452 KB Output is correct
39 Correct 162 ms 41452 KB Output is correct
40 Correct 159 ms 41580 KB Output is correct
41 Correct 157 ms 41196 KB Output is correct
42 Correct 158 ms 40940 KB Output is correct
43 Correct 147 ms 38764 KB Output is correct
44 Correct 159 ms 41708 KB Output is correct
45 Correct 158 ms 41580 KB Output is correct
46 Correct 125 ms 43500 KB Output is correct
47 Correct 163 ms 42084 KB Output is correct
48 Correct 32 ms 18284 KB Output is correct
49 Correct 341 ms 46436 KB Output is correct
50 Correct 359 ms 46472 KB Output is correct
51 Correct 340 ms 46288 KB Output is correct
52 Correct 342 ms 46172 KB Output is correct
53 Correct 332 ms 44696 KB Output is correct
54 Correct 283 ms 38240 KB Output is correct
55 Correct 351 ms 47184 KB Output is correct
56 Correct 343 ms 46544 KB Output is correct
57 Correct 423 ms 48008 KB Output is correct
58 Correct 372 ms 47312 KB Output is correct
59 Correct 109 ms 21868 KB Output is correct
60 Correct 359 ms 41964 KB Output is correct
61 Correct 347 ms 41252 KB Output is correct
62 Correct 370 ms 42860 KB Output is correct
63 Correct 425 ms 44368 KB Output is correct
64 Correct 10 ms 14444 KB Output is correct
65 Correct 11 ms 14444 KB Output is correct
66 Correct 11 ms 14444 KB Output is correct
67 Correct 11 ms 14572 KB Output is correct
68 Correct 11 ms 14444 KB Output is correct
69 Correct 11 ms 14700 KB Output is correct
70 Correct 11 ms 14700 KB Output is correct
71 Correct 11 ms 14700 KB Output is correct
72 Correct 11 ms 14444 KB Output is correct
73 Correct 11 ms 14700 KB Output is correct
74 Correct 192 ms 51180 KB Output is correct
75 Correct 156 ms 41452 KB Output is correct
76 Correct 168 ms 41580 KB Output is correct
77 Correct 160 ms 44396 KB Output is correct
78 Correct 128 ms 47340 KB Output is correct
79 Correct 109 ms 40172 KB Output is correct
80 Correct 157 ms 46188 KB Output is correct
81 Correct 227 ms 54764 KB Output is correct
82 Correct 249 ms 57836 KB Output is correct
83 Correct 259 ms 62828 KB Output is correct
84 Correct 125 ms 43372 KB Output is correct
85 Correct 223 ms 57964 KB Output is correct
86 Correct 103 ms 28264 KB Output is correct
87 Correct 345 ms 46288 KB Output is correct
88 Correct 341 ms 46288 KB Output is correct
89 Correct 420 ms 47752 KB Output is correct
90 Correct 236 ms 53316 KB Output is correct
91 Correct 250 ms 50960 KB Output is correct
92 Correct 389 ms 50120 KB Output is correct
93 Correct 405 ms 59048 KB Output is correct
94 Correct 460 ms 62412 KB Output is correct
95 Correct 454 ms 66668 KB Output is correct
96 Correct 443 ms 48240 KB Output is correct
97 Correct 450 ms 55212 KB Output is correct