Submission #293961

# Submission time Handle Problem Language Result Execution time Memory
293961 2020-09-08T14:08:14 Z oolimry Swapping Cities (APIO20_swap) C++14
13 / 100
486 ms 27024 KB
#include "swap.h"
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
using namespace std;
typedef pair<int,int> ii;
const int inf = 1023456789;

int n, newNode;

namespace ufds{
	int p[200005];

	void init(){
		for(int i = 0;i < 2*n;i++) p[i] = i;
	}

	int findSet(int u){
		if(u == p[u]) return u;
		else return p[u] = findSet(p[u]);
	}		

	bool unionSet(int u, int v){
		u = findSet(u), v = findSet(v);
		if(u == v) return false;
		
		if(u > v) swap(u, v);
		p[u] = v;
		return true;
	}
}

struct edge{ int u, v, w; };
vector<edge> edges;

int maxEdge[200005];
int minWork[200005];
int L[200005];
int R[200005];
int deg[200005];
int p[200005][19];
int depth[200005];

void dfs0(int u){
	if(L[u] == R[u] && L[u] == 0) return;
		
	depth[L[u]] = depth[u] + 1;
	depth[R[u]] = depth[u] + 1;
	dfs0(L[u]); dfs0(R[u]);
}

void dfs(int u){
	minWork[u] = max(minWork[u], maxEdge[u]);
	if(L[u] == R[u] && L[u] == 0) return;
	
	minWork[L[u]] = min(minWork[u], minWork[L[u]]); ///pass down to children
	minWork[R[u]] = min(minWork[u], minWork[R[u]]);
	
	dfs(L[u]); dfs(R[u]);
	
	minWork[u] = min(minWork[u], minWork[L[u]]); ///pass up to parent
	minWork[u] = min(minWork[u], minWork[R[u]]);
}

int LCA(int u, int v){
	if(depth[u] < depth[v]) swap(u, v);
	
	for(int k = 18;k >= 0;k--){
		int newU = p[u][k];
		if(depth[newU] >= depth[v]) u = newU;
	}
	
	for(int k = 18;k >= 0;k--){
		int newU = p[u][k], newV = p[v][k];
		if(newU != newV){
			u = newU;
			v = newV;
		}
	}
	return p[u][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) {
	n = N; newNode = n;
	
	for(int i = 0;i < M;i++) edges.push_back({U[i], V[i], W[i]});
	
	ufds::init();
	fill(minWork, minWork+2*n, inf);
	
	sort(all(edges), [&](edge a, edge b){ return a.w < b.w; });
	
	vector<edge> cycle;
	for(edge e : edges){
		deg[e.u]++;
		deg[e.v]++;
		if(deg[e.u] >= 3) minWork[e.u] = min(e.w, minWork[e.u]);
		if(deg[e.v] >= 3) minWork[e.v] = min(e.w, minWork[e.v]);
		
		int u = ufds::findSet(e.u), v = ufds::findSet(e.v);
		if(u != v){
			L[newNode] = u;
			R[newNode] = v;
			ufds::unionSet(newNode, u);
			ufds::unionSet(newNode, v);
			maxEdge[newNode] = e.w;
			
			p[u][0] = newNode;
			p[v][0] = newNode;
			
			newNode++;
		}
		else cycle.push_back(e);
	}
	
	n = newNode;
	p[n-1][0] = -1;
	
	for(int k = 1;k <= 18;k++){
		for(int i = 0;i < n;i++){
			if(p[i][k-1] == -1) p[i][k] = -1;
			else p[i][k] = p[p[i][k-1]][k-1];
		}
	}
	
	dfs0(n-1);

	for(edge e : cycle){
		int u = e.u, v = e.v;
		int lca = LCA(u, v);
		minWork[lca] = min(e.w, minWork[lca]);
	}
	
	dfs(n-1);
	
}

int getMinimumFuelCapacity(int X, int Y){
	
	int lca = LCA(X, Y);
	//cout << X << " " << Y << " " << lca << "\n";
	
	int ans = max(minWork[lca], maxEdge[lca]);
	if(ans == inf) ans = -1;
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 136 ms 18004 KB Output is correct
10 Correct 115 ms 21868 KB Output is correct
11 Correct 116 ms 21576 KB Output is correct
12 Correct 121 ms 22828 KB Output is correct
13 Correct 131 ms 23532 KB Output is correct
14 Correct 112 ms 18064 KB Output is correct
15 Correct 397 ms 23904 KB Output is correct
16 Correct 305 ms 23300 KB Output is correct
17 Correct 340 ms 24520 KB Output is correct
18 Correct 476 ms 25400 KB Output is correct
19 Correct 128 ms 7292 KB Output is correct
20 Correct 300 ms 24940 KB Output is correct
21 Correct 317 ms 24484 KB Output is correct
22 Correct 326 ms 25944 KB Output is correct
23 Correct 486 ms 26708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 444 ms 25996 KB Output is correct
4 Correct 448 ms 27024 KB Output is correct
5 Correct 467 ms 26628 KB Output is correct
6 Correct 407 ms 26924 KB Output is correct
7 Correct 457 ms 26840 KB Output is correct
8 Correct 420 ms 25884 KB Output is correct
9 Correct 452 ms 26604 KB Output is correct
10 Correct 446 ms 25668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 1 ms 640 KB Output is correct
11 Correct 1 ms 640 KB Output is correct
12 Correct 1 ms 640 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Incorrect 1 ms 640 KB Output isn't correct
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 136 ms 18004 KB Output is correct
11 Correct 115 ms 21868 KB Output is correct
12 Correct 116 ms 21576 KB Output is correct
13 Correct 121 ms 22828 KB Output is correct
14 Correct 131 ms 23532 KB Output is correct
15 Correct 1 ms 640 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 2 ms 512 KB Output is correct
20 Incorrect 1 ms 640 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 640 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 1 ms 640 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 136 ms 18004 KB Output is correct
10 Correct 115 ms 21868 KB Output is correct
11 Correct 116 ms 21576 KB Output is correct
12 Correct 121 ms 22828 KB Output is correct
13 Correct 131 ms 23532 KB Output is correct
14 Correct 112 ms 18064 KB Output is correct
15 Correct 397 ms 23904 KB Output is correct
16 Correct 305 ms 23300 KB Output is correct
17 Correct 340 ms 24520 KB Output is correct
18 Correct 476 ms 25400 KB Output is correct
19 Correct 444 ms 25996 KB Output is correct
20 Correct 448 ms 27024 KB Output is correct
21 Correct 467 ms 26628 KB Output is correct
22 Correct 407 ms 26924 KB Output is correct
23 Correct 457 ms 26840 KB Output is correct
24 Correct 420 ms 25884 KB Output is correct
25 Correct 452 ms 26604 KB Output is correct
26 Correct 446 ms 25668 KB Output is correct
27 Correct 1 ms 640 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 512 KB Output is correct
31 Correct 2 ms 512 KB Output is correct
32 Incorrect 1 ms 640 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 640 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 1 ms 640 KB Output is correct
10 Correct 136 ms 18004 KB Output is correct
11 Correct 115 ms 21868 KB Output is correct
12 Correct 116 ms 21576 KB Output is correct
13 Correct 121 ms 22828 KB Output is correct
14 Correct 131 ms 23532 KB Output is correct
15 Correct 112 ms 18064 KB Output is correct
16 Correct 397 ms 23904 KB Output is correct
17 Correct 305 ms 23300 KB Output is correct
18 Correct 340 ms 24520 KB Output is correct
19 Correct 476 ms 25400 KB Output is correct
20 Correct 444 ms 25996 KB Output is correct
21 Correct 448 ms 27024 KB Output is correct
22 Correct 467 ms 26628 KB Output is correct
23 Correct 407 ms 26924 KB Output is correct
24 Correct 457 ms 26840 KB Output is correct
25 Correct 420 ms 25884 KB Output is correct
26 Correct 452 ms 26604 KB Output is correct
27 Correct 446 ms 25668 KB Output is correct
28 Correct 1 ms 640 KB Output is correct
29 Correct 1 ms 640 KB Output is correct
30 Correct 1 ms 640 KB Output is correct
31 Correct 1 ms 512 KB Output is correct
32 Correct 2 ms 512 KB Output is correct
33 Incorrect 1 ms 640 KB Output isn't correct
34 Halted 0 ms 0 KB -