Submission #482999

# Submission time Handle Problem Language Result Execution time Memory
482999 2021-10-27T10:53:25 Z PoPularPlusPlus Swapping Cities (APIO20_swap) C++17
100 / 100
326 ms 57500 KB
#include "swap.h"

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

const int N = 100002 , M = 200003 , l = 25;
vector<int> adj[M + N];
int degree[N];
int root , ans[N + M];
int timer;
int tin[N + M], tout[N + M];
int up[N + M][l + 5];

void dfs(int v, int p){
    tin[v] = ++timer;
    up[v][0] = p;
    for (int i = 1; i <= l; ++i)
        up[v][i] = up[up[v][i-1]][i-1];

    for (int u : adj[v]) {
        if (u != p){
			if(ans[u] == -1 && ans[v] != -1)ans[u] = ans[v];
            dfs(u, v);
		}
    }

    tout[v] = ++timer;
}

bool is_ancestor(int u, int v){
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v){
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = l; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}

struct UFDS {

vector<int> p,siz;

void init(int n){
	p.resize(n + 2);
	siz.resize(n + 2);
	for(int i = 0; i <= n; i++){
		p[i] = i;
		siz[i] = 1;
	}
}

int get(int x){
	return p[x]=(x==p[x] ? x : get(p[x]));
}

void unio(int u , int v , int w , int a , int b){
	// cycle 
	if(a == b){
		if(ans[a] == -1){
			adj[root].pb(a);
			p[a] = root;
			degree[u]++;
			degree[v]++;
			ans[root] = w;
		}
		else return;
	}
	else {
		adj[root].pb(a);
		adj[root].pb(b);
		p[a] = root;
		p[b] = root;
		degree[u]++;
		degree[v]++;
		if(ans[a] != -1 || ans[b] != -1 || degree[u] == 3 || degree[v] == 3)ans[root] = w;
	}
	root++;
}
};

UFDS d;

void init(int n , int m , vector<int> a , vector<int> b , vector<int> c){
	memset(degree,0,sizeof degree);
	memset(ans,-1,sizeof ans);
	d.init(n + m + 2);
	timer = 0;
	root = n;
	vector<pair<int , pair<int,int>>> lol;
	for(int i = 0; i < m; i++){
		lol.pb(mp(c[i] , mp(a[i] , b[i])));
		//lol[b[i]].pb(mp(a[i] , c[i]));
	}
	sv(lol);
	for(int i = 0; i < m; i++){
		d.unio(lol[i].vs.vf , lol[i].vs.vs , lol[i].vf , d.get(lol[i].vs.vf) , d.get(lol[i].vs.vs));
	}
	dfs(root-1,root-1);
}

int getMinimumFuelCapacity(int x, int y){
	int lc = lca(x , y);
	return ans[lc];
}


# Verdict Execution time Memory Grader output
1 Correct 6 ms 8908 KB Output is correct
2 Correct 4 ms 8908 KB Output is correct
3 Correct 4 ms 8908 KB Output is correct
4 Correct 4 ms 9036 KB Output is correct
5 Correct 5 ms 9164 KB Output is correct
6 Correct 5 ms 9164 KB Output is correct
7 Correct 5 ms 9252 KB Output is correct
8 Correct 5 ms 9164 KB Output is correct
9 Correct 103 ms 35000 KB Output is correct
10 Correct 132 ms 42936 KB Output is correct
11 Correct 124 ms 42304 KB Output is correct
12 Correct 128 ms 44220 KB Output is correct
13 Correct 134 ms 47412 KB Output is correct
14 Correct 112 ms 36908 KB Output is correct
15 Correct 220 ms 47136 KB Output is correct
16 Correct 214 ms 45932 KB Output is correct
17 Correct 221 ms 48176 KB Output is correct
18 Correct 306 ms 51308 KB Output is correct
19 Correct 83 ms 19400 KB Output is correct
20 Correct 214 ms 47172 KB Output is correct
21 Correct 224 ms 46252 KB Output is correct
22 Correct 228 ms 48516 KB Output is correct
23 Correct 326 ms 51796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8908 KB Output is correct
2 Correct 4 ms 8908 KB Output is correct
3 Correct 261 ms 48132 KB Output is correct
4 Correct 299 ms 53896 KB Output is correct
5 Correct 270 ms 52920 KB Output is correct
6 Correct 274 ms 53776 KB Output is correct
7 Correct 277 ms 53412 KB Output is correct
8 Correct 261 ms 51780 KB Output is correct
9 Correct 270 ms 53100 KB Output is correct
10 Correct 273 ms 51440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8908 KB Output is correct
2 Correct 4 ms 8908 KB Output is correct
3 Correct 4 ms 8908 KB Output is correct
4 Correct 4 ms 9036 KB Output is correct
5 Correct 5 ms 9164 KB Output is correct
6 Correct 5 ms 9164 KB Output is correct
7 Correct 5 ms 9252 KB Output is correct
8 Correct 5 ms 9164 KB Output is correct
9 Correct 4 ms 8908 KB Output is correct
10 Correct 6 ms 9140 KB Output is correct
11 Correct 5 ms 9164 KB Output is correct
12 Correct 5 ms 9164 KB Output is correct
13 Correct 7 ms 9104 KB Output is correct
14 Correct 6 ms 9164 KB Output is correct
15 Correct 6 ms 9164 KB Output is correct
16 Correct 5 ms 9256 KB Output is correct
17 Correct 5 ms 9164 KB Output is correct
18 Correct 5 ms 9164 KB Output is correct
19 Correct 5 ms 9036 KB Output is correct
20 Correct 5 ms 9164 KB Output is correct
21 Correct 6 ms 9136 KB Output is correct
22 Correct 5 ms 9036 KB Output is correct
23 Correct 5 ms 9164 KB Output is correct
24 Correct 6 ms 9292 KB Output is correct
25 Correct 5 ms 9292 KB Output is correct
26 Correct 5 ms 9292 KB Output is correct
27 Correct 7 ms 9292 KB Output is correct
28 Correct 6 ms 9292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8908 KB Output is correct
2 Correct 6 ms 8908 KB Output is correct
3 Correct 4 ms 8908 KB Output is correct
4 Correct 4 ms 8908 KB Output is correct
5 Correct 4 ms 9036 KB Output is correct
6 Correct 5 ms 9164 KB Output is correct
7 Correct 5 ms 9164 KB Output is correct
8 Correct 5 ms 9252 KB Output is correct
9 Correct 5 ms 9164 KB Output is correct
10 Correct 103 ms 35000 KB Output is correct
11 Correct 132 ms 42936 KB Output is correct
12 Correct 124 ms 42304 KB Output is correct
13 Correct 128 ms 44220 KB Output is correct
14 Correct 134 ms 47412 KB Output is correct
15 Correct 6 ms 9140 KB Output is correct
16 Correct 5 ms 9164 KB Output is correct
17 Correct 5 ms 9164 KB Output is correct
18 Correct 7 ms 9104 KB Output is correct
19 Correct 6 ms 9164 KB Output is correct
20 Correct 6 ms 9164 KB Output is correct
21 Correct 5 ms 9256 KB Output is correct
22 Correct 5 ms 9164 KB Output is correct
23 Correct 5 ms 9164 KB Output is correct
24 Correct 5 ms 9036 KB Output is correct
25 Correct 5 ms 9164 KB Output is correct
26 Correct 6 ms 9136 KB Output is correct
27 Correct 5 ms 9036 KB Output is correct
28 Correct 5 ms 9164 KB Output is correct
29 Correct 6 ms 9292 KB Output is correct
30 Correct 5 ms 9292 KB Output is correct
31 Correct 5 ms 9292 KB Output is correct
32 Correct 7 ms 9292 KB Output is correct
33 Correct 6 ms 9292 KB Output is correct
34 Correct 17 ms 13700 KB Output is correct
35 Correct 128 ms 43976 KB Output is correct
36 Correct 130 ms 43896 KB Output is correct
37 Correct 129 ms 43952 KB Output is correct
38 Correct 126 ms 43440 KB Output is correct
39 Correct 128 ms 43320 KB Output is correct
40 Correct 117 ms 40384 KB Output is correct
41 Correct 130 ms 44256 KB Output is correct
42 Correct 127 ms 43960 KB Output is correct
43 Correct 119 ms 48036 KB Output is correct
44 Correct 159 ms 44304 KB Output is correct
45 Correct 131 ms 42580 KB Output is correct
46 Correct 129 ms 43944 KB Output is correct
47 Correct 140 ms 43832 KB Output is correct
48 Correct 138 ms 45620 KB Output is correct
49 Correct 79 ms 19768 KB Output is correct
50 Correct 52 ms 18720 KB Output is correct
51 Correct 110 ms 36868 KB Output is correct
52 Correct 159 ms 48252 KB Output is correct
53 Correct 193 ms 55248 KB Output is correct
54 Correct 173 ms 50352 KB Output is correct
55 Correct 119 ms 47672 KB Output is correct
56 Correct 162 ms 50804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 8908 KB Output is correct
2 Correct 4 ms 8908 KB Output is correct
3 Correct 4 ms 8908 KB Output is correct
4 Correct 4 ms 9036 KB Output is correct
5 Correct 5 ms 9164 KB Output is correct
6 Correct 5 ms 9164 KB Output is correct
7 Correct 5 ms 9252 KB Output is correct
8 Correct 5 ms 9164 KB Output is correct
9 Correct 103 ms 35000 KB Output is correct
10 Correct 132 ms 42936 KB Output is correct
11 Correct 124 ms 42304 KB Output is correct
12 Correct 128 ms 44220 KB Output is correct
13 Correct 134 ms 47412 KB Output is correct
14 Correct 112 ms 36908 KB Output is correct
15 Correct 220 ms 47136 KB Output is correct
16 Correct 214 ms 45932 KB Output is correct
17 Correct 221 ms 48176 KB Output is correct
18 Correct 306 ms 51308 KB Output is correct
19 Correct 261 ms 48132 KB Output is correct
20 Correct 299 ms 53896 KB Output is correct
21 Correct 270 ms 52920 KB Output is correct
22 Correct 274 ms 53776 KB Output is correct
23 Correct 277 ms 53412 KB Output is correct
24 Correct 261 ms 51780 KB Output is correct
25 Correct 270 ms 53100 KB Output is correct
26 Correct 273 ms 51440 KB Output is correct
27 Correct 6 ms 9140 KB Output is correct
28 Correct 5 ms 9164 KB Output is correct
29 Correct 5 ms 9164 KB Output is correct
30 Correct 7 ms 9104 KB Output is correct
31 Correct 6 ms 9164 KB Output is correct
32 Correct 6 ms 9164 KB Output is correct
33 Correct 5 ms 9256 KB Output is correct
34 Correct 5 ms 9164 KB Output is correct
35 Correct 5 ms 9164 KB Output is correct
36 Correct 17 ms 13700 KB Output is correct
37 Correct 128 ms 43976 KB Output is correct
38 Correct 130 ms 43896 KB Output is correct
39 Correct 129 ms 43952 KB Output is correct
40 Correct 126 ms 43440 KB Output is correct
41 Correct 128 ms 43320 KB Output is correct
42 Correct 117 ms 40384 KB Output is correct
43 Correct 130 ms 44256 KB Output is correct
44 Correct 127 ms 43960 KB Output is correct
45 Correct 119 ms 48036 KB Output is correct
46 Correct 159 ms 44304 KB Output is correct
47 Correct 28 ms 13668 KB Output is correct
48 Correct 222 ms 47884 KB Output is correct
49 Correct 222 ms 47824 KB Output is correct
50 Correct 233 ms 47808 KB Output is correct
51 Correct 223 ms 47544 KB Output is correct
52 Correct 245 ms 45644 KB Output is correct
53 Correct 183 ms 37348 KB Output is correct
54 Correct 221 ms 48508 KB Output is correct
55 Correct 238 ms 47892 KB Output is correct
56 Correct 278 ms 51124 KB Output is correct
57 Correct 243 ms 48616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8908 KB Output is correct
2 Correct 6 ms 8908 KB Output is correct
3 Correct 4 ms 8908 KB Output is correct
4 Correct 4 ms 8908 KB Output is correct
5 Correct 4 ms 9036 KB Output is correct
6 Correct 5 ms 9164 KB Output is correct
7 Correct 5 ms 9164 KB Output is correct
8 Correct 5 ms 9252 KB Output is correct
9 Correct 5 ms 9164 KB Output is correct
10 Correct 103 ms 35000 KB Output is correct
11 Correct 132 ms 42936 KB Output is correct
12 Correct 124 ms 42304 KB Output is correct
13 Correct 128 ms 44220 KB Output is correct
14 Correct 134 ms 47412 KB Output is correct
15 Correct 112 ms 36908 KB Output is correct
16 Correct 220 ms 47136 KB Output is correct
17 Correct 214 ms 45932 KB Output is correct
18 Correct 221 ms 48176 KB Output is correct
19 Correct 306 ms 51308 KB Output is correct
20 Correct 261 ms 48132 KB Output is correct
21 Correct 299 ms 53896 KB Output is correct
22 Correct 270 ms 52920 KB Output is correct
23 Correct 274 ms 53776 KB Output is correct
24 Correct 277 ms 53412 KB Output is correct
25 Correct 261 ms 51780 KB Output is correct
26 Correct 270 ms 53100 KB Output is correct
27 Correct 273 ms 51440 KB Output is correct
28 Correct 6 ms 9140 KB Output is correct
29 Correct 5 ms 9164 KB Output is correct
30 Correct 5 ms 9164 KB Output is correct
31 Correct 7 ms 9104 KB Output is correct
32 Correct 6 ms 9164 KB Output is correct
33 Correct 6 ms 9164 KB Output is correct
34 Correct 5 ms 9256 KB Output is correct
35 Correct 5 ms 9164 KB Output is correct
36 Correct 5 ms 9164 KB Output is correct
37 Correct 17 ms 13700 KB Output is correct
38 Correct 128 ms 43976 KB Output is correct
39 Correct 130 ms 43896 KB Output is correct
40 Correct 129 ms 43952 KB Output is correct
41 Correct 126 ms 43440 KB Output is correct
42 Correct 128 ms 43320 KB Output is correct
43 Correct 117 ms 40384 KB Output is correct
44 Correct 130 ms 44256 KB Output is correct
45 Correct 127 ms 43960 KB Output is correct
46 Correct 119 ms 48036 KB Output is correct
47 Correct 159 ms 44304 KB Output is correct
48 Correct 28 ms 13668 KB Output is correct
49 Correct 222 ms 47884 KB Output is correct
50 Correct 222 ms 47824 KB Output is correct
51 Correct 233 ms 47808 KB Output is correct
52 Correct 223 ms 47544 KB Output is correct
53 Correct 245 ms 45644 KB Output is correct
54 Correct 183 ms 37348 KB Output is correct
55 Correct 221 ms 48508 KB Output is correct
56 Correct 238 ms 47892 KB Output is correct
57 Correct 278 ms 51124 KB Output is correct
58 Correct 243 ms 48616 KB Output is correct
59 Correct 83 ms 19400 KB Output is correct
60 Correct 214 ms 47172 KB Output is correct
61 Correct 224 ms 46252 KB Output is correct
62 Correct 228 ms 48516 KB Output is correct
63 Correct 326 ms 51796 KB Output is correct
64 Correct 5 ms 9036 KB Output is correct
65 Correct 5 ms 9164 KB Output is correct
66 Correct 6 ms 9136 KB Output is correct
67 Correct 5 ms 9036 KB Output is correct
68 Correct 5 ms 9164 KB Output is correct
69 Correct 6 ms 9292 KB Output is correct
70 Correct 5 ms 9292 KB Output is correct
71 Correct 5 ms 9292 KB Output is correct
72 Correct 7 ms 9292 KB Output is correct
73 Correct 6 ms 9292 KB Output is correct
74 Correct 131 ms 42580 KB Output is correct
75 Correct 129 ms 43944 KB Output is correct
76 Correct 140 ms 43832 KB Output is correct
77 Correct 138 ms 45620 KB Output is correct
78 Correct 79 ms 19768 KB Output is correct
79 Correct 52 ms 18720 KB Output is correct
80 Correct 110 ms 36868 KB Output is correct
81 Correct 159 ms 48252 KB Output is correct
82 Correct 193 ms 55248 KB Output is correct
83 Correct 173 ms 50352 KB Output is correct
84 Correct 119 ms 47672 KB Output is correct
85 Correct 162 ms 50804 KB Output is correct
86 Correct 79 ms 22660 KB Output is correct
87 Correct 233 ms 47800 KB Output is correct
88 Correct 236 ms 47800 KB Output is correct
89 Correct 265 ms 47288 KB Output is correct
90 Correct 154 ms 23608 KB Output is correct
91 Correct 158 ms 25268 KB Output is correct
92 Correct 248 ms 40420 KB Output is correct
93 Correct 258 ms 52016 KB Output is correct
94 Correct 309 ms 57500 KB Output is correct
95 Correct 267 ms 54320 KB Output is correct
96 Correct 288 ms 51252 KB Output is correct
97 Correct 295 ms 52020 KB Output is correct