Submission #718007

# Submission time Handle Problem Language Result Execution time Memory
718007 2023-04-03T06:40:41 Z qwerasdfzxcl Swapping Cities (APIO20_swap) C++17
100 / 100
477 ms 46104 KB
#include "swap.h"
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
constexpr int INF = 1e9 + 100;

int n;
vector<array<int, 3>> a;

struct Graph{
	int idx[100100], deg[300300], val[300300], ans[100100], sz, N;
	vector<int> adj[300300];

	void init(int n){
		N = n;
		sz = n;
		for (int i=1;i<=n;i++){
			idx[i] = i;
			deg[i] = 0;
			val[i] = INF;
			ans[i] = INF;
		}
	}

	int addV(){
		assert(sz+1 < 300300);
		deg[sz+1] = 0;
		val[sz+1] = INF;
		return ++sz;
	}

	void addE(int x, int y){
		y = idx[y];
		assert(x<=sz && y<=sz);
		adj[x].push_back(y);
		deg[y]++;
	}

	void change(int x, int y){
		idx[x] = y;
	}

	void update(int v, int w){
		v = idx[v];
		val[v] = min(val[v], w);
	}

	void dfs(int s, int v = INF){
		v = min(v, val[s]);
		if (s<=N) ans[s] = v;

		for (auto &nxt:adj[s]) dfs(nxt, v);
	}
	void init(){
		for (int i=1;i<=sz;i++) if (!deg[i]){
			dfs(i);
		}
	}
}G;

struct DSU2{
	int path[100100], mx[100100], deg[100100], isTree[100100];
	void init(int n){
		G.init(n);
		for (int i=0;i<=n;i++){
			path[i] = i, mx[i] = 0, deg[i] = 0, isTree[i] = 1;
		} 
	}
	int find(int s){
		if (s==path[s]) return s;
		return path[s] = find(path[s]);
	}
	bool merge(int s, int v, int w){
		deg[s]++, deg[v]++;
		int val = max(deg[s], deg[v]);
		s = find(s), v = find(v);

		if (s==v){
			isTree[s] = 0;
			mx[s] = max(mx[s], val);

			G.update(s, w);
			return 0;
		}

		path[s] = v;
		isTree[v] &= isTree[s];
		mx[v] = max(mx[v], mx[s]);
		mx[v] = max(mx[v], val);

		int tmp = G.addV();
		G.addE(tmp, s);
		G.addE(tmp, v);
		G.change(v, tmp);

		if (mx[v]>2 || !isTree[v]) G.update(v, w);

		return 1;
	}
}dsu;

struct TreeManager{
	vector<pair<int, int>> adj[100100];
	int sz, dep[100100], sp[100100][20], spE[100100][20], visited[100100];
 
	bool flag1 = 0, flag2 = 0;
 
	void init1(int n){
		flag1 = 1, flag2 = 0;
 
		sz = n;
		for (int i=0;i<=sz;i++){
			adj[i].clear();
			fill(sp[i], sp[i]+20, 0);
			fill(spE[i], spE[i]+20, INF);
			visited[i] = 0, dep[i] = 0;
		}
	}
 
	void add_edge(int s, int e, int w){
		assert(flag1 && !flag2);
 
		adj[s].emplace_back(e, w);
		adj[e].emplace_back(s, w);
	}
 
	void dfs(int s, int pa = 0, int paw = INF){
		assert(flag1 && flag2);
		sp[s][0] = pa;
		spE[s][0] = paw;
		dep[s] = dep[pa] + 1;
		visited[s] = 1;
 
		for (auto &[v, w]:adj[s]) if (!visited[v]) dfs(v, s, w);
	}
 
	void init2(){
		assert(flag1 && !flag2);
		flag2 = 1;
		for (int i=1;i<=sz;i++) if (!visited[i]){
			dfs(i);
		}
 
		for (int j=1;j<20;j++){
			for (int i=1;i<=sz;i++){
				sp[i][j] = sp[sp[i][j-1]][j-1];
				spE[i][j] = max(spE[i][j-1], spE[sp[i][j-1]][j-1]);
			}
		}
	}
 
	int queryE(int s, int e){
		int ret = -INF;
 
		if (dep[s]!=dep[e]){
			if (dep[s] > dep[e]) swap(s, e);
			for (int j=19;j>=0;j--) if (dep[s] < dep[sp[e][j]]){
				ret = max(ret, spE[e][j]);
				e = sp[e][j];
			}
 
			ret = max(ret, spE[e][0]);
			e = sp[e][0];
		}
 
		if (s==e) return ret;
		for (int j=19;j>=0;j--) if (sp[s][j]!=sp[e][j]){
			ret = max(ret, spE[s][j]);
			ret = max(ret, spE[e][j]);
 
			s = sp[s][j];
			e = sp[e][j];
		}
 
		return max(ret, max(spE[s][0], spE[e][0]));
	}
}T1;

void init(int N, int M, std::vector<int> U, std::vector<int> V, std::vector<int> W) {
	n = N;
	T1.init1(n);
	dsu.init(n);
	
	for (int i=0;i<M;i++) a.push_back({W[i], U[i]+1, V[i]+1});
	sort(a.begin(), a.end());

	for (const auto &[w, u, v]:a){
		if (dsu.merge(u, v, w)) T1.add_edge(u, v, w);
	}

	T1.init2();
	G.init();
}

int getMinimumFuelCapacity(int X, int Y) {
	++X, ++Y;
	int ans = T1.queryE(X, Y);
	ans = max(ans, G.ans[X]);
	
	return ans==INF?-1:ans;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10100 KB Output is correct
9 Correct 133 ms 34836 KB Output is correct
10 Correct 185 ms 40896 KB Output is correct
11 Correct 170 ms 40224 KB Output is correct
12 Correct 192 ms 42028 KB Output is correct
13 Correct 152 ms 42816 KB Output is correct
14 Correct 156 ms 34552 KB Output is correct
15 Correct 379 ms 42816 KB Output is correct
16 Correct 393 ms 41340 KB Output is correct
17 Correct 384 ms 44448 KB Output is correct
18 Correct 367 ms 43932 KB Output is correct
19 Correct 101 ms 18400 KB Output is correct
20 Correct 391 ms 44012 KB Output is correct
21 Correct 383 ms 42476 KB Output is correct
22 Correct 412 ms 44996 KB Output is correct
23 Correct 361 ms 45352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 183 ms 44072 KB Output is correct
4 Correct 186 ms 45620 KB Output is correct
5 Correct 190 ms 44856 KB Output is correct
6 Correct 179 ms 45556 KB Output is correct
7 Correct 187 ms 45264 KB Output is correct
8 Correct 187 ms 43852 KB Output is correct
9 Correct 192 ms 44992 KB Output is correct
10 Correct 183 ms 43624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10100 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 7 ms 10032 KB Output is correct
11 Correct 6 ms 10084 KB Output is correct
12 Correct 6 ms 10068 KB Output is correct
13 Correct 6 ms 9940 KB Output is correct
14 Correct 6 ms 9940 KB Output is correct
15 Correct 6 ms 10052 KB Output is correct
16 Correct 6 ms 10068 KB Output is correct
17 Correct 6 ms 10068 KB Output is correct
18 Correct 6 ms 10068 KB Output is correct
19 Correct 7 ms 9940 KB Output is correct
20 Correct 7 ms 10068 KB Output is correct
21 Correct 6 ms 10016 KB Output is correct
22 Correct 6 ms 9812 KB Output is correct
23 Correct 6 ms 9940 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 7 ms 10024 KB Output is correct
26 Correct 8 ms 10068 KB Output is correct
27 Correct 7 ms 10012 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 5 ms 10068 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10100 KB Output is correct
10 Correct 133 ms 34836 KB Output is correct
11 Correct 185 ms 40896 KB Output is correct
12 Correct 170 ms 40224 KB Output is correct
13 Correct 192 ms 42028 KB Output is correct
14 Correct 152 ms 42816 KB Output is correct
15 Correct 7 ms 10032 KB Output is correct
16 Correct 6 ms 10084 KB Output is correct
17 Correct 6 ms 10068 KB Output is correct
18 Correct 6 ms 9940 KB Output is correct
19 Correct 6 ms 9940 KB Output is correct
20 Correct 6 ms 10052 KB Output is correct
21 Correct 6 ms 10068 KB Output is correct
22 Correct 6 ms 10068 KB Output is correct
23 Correct 6 ms 10068 KB Output is correct
24 Correct 7 ms 9940 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 6 ms 10016 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 6 ms 9940 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 7 ms 10024 KB Output is correct
31 Correct 8 ms 10068 KB Output is correct
32 Correct 7 ms 10012 KB Output is correct
33 Correct 6 ms 10068 KB Output is correct
34 Correct 17 ms 13848 KB Output is correct
35 Correct 168 ms 41784 KB Output is correct
36 Correct 172 ms 40944 KB Output is correct
37 Correct 169 ms 40192 KB Output is correct
38 Correct 166 ms 39552 KB Output is correct
39 Correct 157 ms 39448 KB Output is correct
40 Correct 149 ms 37032 KB Output is correct
41 Correct 190 ms 41260 KB Output is correct
42 Correct 180 ms 41732 KB Output is correct
43 Correct 139 ms 42048 KB Output is correct
44 Correct 154 ms 40328 KB Output is correct
45 Correct 143 ms 36772 KB Output is correct
46 Correct 174 ms 40492 KB Output is correct
47 Correct 171 ms 39704 KB Output is correct
48 Correct 167 ms 40384 KB Output is correct
49 Correct 66 ms 16808 KB Output is correct
50 Correct 58 ms 15980 KB Output is correct
51 Correct 124 ms 31936 KB Output is correct
52 Correct 202 ms 42584 KB Output is correct
53 Correct 199 ms 43288 KB Output is correct
54 Correct 250 ms 44624 KB Output is correct
55 Correct 159 ms 42548 KB Output is correct
56 Correct 197 ms 43568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 10068 KB Output is correct
6 Correct 6 ms 9940 KB Output is correct
7 Correct 6 ms 10068 KB Output is correct
8 Correct 6 ms 10100 KB Output is correct
9 Correct 133 ms 34836 KB Output is correct
10 Correct 185 ms 40896 KB Output is correct
11 Correct 170 ms 40224 KB Output is correct
12 Correct 192 ms 42028 KB Output is correct
13 Correct 152 ms 42816 KB Output is correct
14 Correct 156 ms 34552 KB Output is correct
15 Correct 379 ms 42816 KB Output is correct
16 Correct 393 ms 41340 KB Output is correct
17 Correct 384 ms 44448 KB Output is correct
18 Correct 367 ms 43932 KB Output is correct
19 Correct 183 ms 44072 KB Output is correct
20 Correct 186 ms 45620 KB Output is correct
21 Correct 190 ms 44856 KB Output is correct
22 Correct 179 ms 45556 KB Output is correct
23 Correct 187 ms 45264 KB Output is correct
24 Correct 187 ms 43852 KB Output is correct
25 Correct 192 ms 44992 KB Output is correct
26 Correct 183 ms 43624 KB Output is correct
27 Correct 7 ms 10032 KB Output is correct
28 Correct 6 ms 10084 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 6 ms 9940 KB Output is correct
31 Correct 6 ms 9940 KB Output is correct
32 Correct 6 ms 10052 KB Output is correct
33 Correct 6 ms 10068 KB Output is correct
34 Correct 6 ms 10068 KB Output is correct
35 Correct 6 ms 10068 KB Output is correct
36 Correct 17 ms 13848 KB Output is correct
37 Correct 168 ms 41784 KB Output is correct
38 Correct 172 ms 40944 KB Output is correct
39 Correct 169 ms 40192 KB Output is correct
40 Correct 166 ms 39552 KB Output is correct
41 Correct 157 ms 39448 KB Output is correct
42 Correct 149 ms 37032 KB Output is correct
43 Correct 190 ms 41260 KB Output is correct
44 Correct 180 ms 41732 KB Output is correct
45 Correct 139 ms 42048 KB Output is correct
46 Correct 154 ms 40328 KB Output is correct
47 Correct 25 ms 13892 KB Output is correct
48 Correct 427 ms 43664 KB Output is correct
49 Correct 398 ms 43192 KB Output is correct
50 Correct 374 ms 42652 KB Output is correct
51 Correct 382 ms 42456 KB Output is correct
52 Correct 336 ms 40608 KB Output is correct
53 Correct 230 ms 33588 KB Output is correct
54 Correct 435 ms 44148 KB Output is correct
55 Correct 410 ms 44028 KB Output is correct
56 Correct 417 ms 45004 KB Output is correct
57 Correct 337 ms 43524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9812 KB Output is correct
6 Correct 5 ms 10068 KB Output is correct
7 Correct 6 ms 9940 KB Output is correct
8 Correct 6 ms 10068 KB Output is correct
9 Correct 6 ms 10100 KB Output is correct
10 Correct 133 ms 34836 KB Output is correct
11 Correct 185 ms 40896 KB Output is correct
12 Correct 170 ms 40224 KB Output is correct
13 Correct 192 ms 42028 KB Output is correct
14 Correct 152 ms 42816 KB Output is correct
15 Correct 156 ms 34552 KB Output is correct
16 Correct 379 ms 42816 KB Output is correct
17 Correct 393 ms 41340 KB Output is correct
18 Correct 384 ms 44448 KB Output is correct
19 Correct 367 ms 43932 KB Output is correct
20 Correct 183 ms 44072 KB Output is correct
21 Correct 186 ms 45620 KB Output is correct
22 Correct 190 ms 44856 KB Output is correct
23 Correct 179 ms 45556 KB Output is correct
24 Correct 187 ms 45264 KB Output is correct
25 Correct 187 ms 43852 KB Output is correct
26 Correct 192 ms 44992 KB Output is correct
27 Correct 183 ms 43624 KB Output is correct
28 Correct 7 ms 10032 KB Output is correct
29 Correct 6 ms 10084 KB Output is correct
30 Correct 6 ms 10068 KB Output is correct
31 Correct 6 ms 9940 KB Output is correct
32 Correct 6 ms 9940 KB Output is correct
33 Correct 6 ms 10052 KB Output is correct
34 Correct 6 ms 10068 KB Output is correct
35 Correct 6 ms 10068 KB Output is correct
36 Correct 6 ms 10068 KB Output is correct
37 Correct 17 ms 13848 KB Output is correct
38 Correct 168 ms 41784 KB Output is correct
39 Correct 172 ms 40944 KB Output is correct
40 Correct 169 ms 40192 KB Output is correct
41 Correct 166 ms 39552 KB Output is correct
42 Correct 157 ms 39448 KB Output is correct
43 Correct 149 ms 37032 KB Output is correct
44 Correct 190 ms 41260 KB Output is correct
45 Correct 180 ms 41732 KB Output is correct
46 Correct 139 ms 42048 KB Output is correct
47 Correct 154 ms 40328 KB Output is correct
48 Correct 25 ms 13892 KB Output is correct
49 Correct 427 ms 43664 KB Output is correct
50 Correct 398 ms 43192 KB Output is correct
51 Correct 374 ms 42652 KB Output is correct
52 Correct 382 ms 42456 KB Output is correct
53 Correct 336 ms 40608 KB Output is correct
54 Correct 230 ms 33588 KB Output is correct
55 Correct 435 ms 44148 KB Output is correct
56 Correct 410 ms 44028 KB Output is correct
57 Correct 417 ms 45004 KB Output is correct
58 Correct 337 ms 43524 KB Output is correct
59 Correct 101 ms 18400 KB Output is correct
60 Correct 391 ms 44012 KB Output is correct
61 Correct 383 ms 42476 KB Output is correct
62 Correct 412 ms 44996 KB Output is correct
63 Correct 361 ms 45352 KB Output is correct
64 Correct 7 ms 9940 KB Output is correct
65 Correct 7 ms 10068 KB Output is correct
66 Correct 6 ms 10016 KB Output is correct
67 Correct 6 ms 9812 KB Output is correct
68 Correct 6 ms 9940 KB Output is correct
69 Correct 6 ms 10068 KB Output is correct
70 Correct 7 ms 10024 KB Output is correct
71 Correct 8 ms 10068 KB Output is correct
72 Correct 7 ms 10012 KB Output is correct
73 Correct 6 ms 10068 KB Output is correct
74 Correct 143 ms 36772 KB Output is correct
75 Correct 174 ms 40492 KB Output is correct
76 Correct 171 ms 39704 KB Output is correct
77 Correct 167 ms 40384 KB Output is correct
78 Correct 66 ms 16808 KB Output is correct
79 Correct 58 ms 15980 KB Output is correct
80 Correct 124 ms 31936 KB Output is correct
81 Correct 202 ms 42584 KB Output is correct
82 Correct 199 ms 43288 KB Output is correct
83 Correct 250 ms 44624 KB Output is correct
84 Correct 159 ms 42548 KB Output is correct
85 Correct 197 ms 43568 KB Output is correct
86 Correct 80 ms 20660 KB Output is correct
87 Correct 426 ms 43140 KB Output is correct
88 Correct 419 ms 43076 KB Output is correct
89 Correct 260 ms 41184 KB Output is correct
90 Correct 148 ms 18712 KB Output is correct
91 Correct 163 ms 20588 KB Output is correct
92 Correct 228 ms 34780 KB Output is correct
93 Correct 405 ms 45276 KB Output is correct
94 Correct 306 ms 45416 KB Output is correct
95 Correct 477 ms 46104 KB Output is correct
96 Correct 378 ms 44188 KB Output is correct
97 Correct 294 ms 44904 KB Output is correct