답안 #546036

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
546036 2022-04-06T05:39:30 Z amunduzbaev 자매 도시 (APIO20_swap) C++17
6 / 100
470 ms 48508 KB
#include "swap.h"
#include "bits/stdc++.h"
using namespace std;

#ifndef EVAL
#include "grader.cpp"
#endif
#define ar array

const int N = 1e5 + 5;
int par[N][20], mx[N][20];
int tin[N], tout[N], t, is[N];
vector<ar<int, 2>> edges[N];

void dfs(int u, int p = -1){
	for(int j=1;j<20;j++){
		par[u][j] = par[par[u][j-1]][j-1];
		mx[u][j] = max(mx[u][j-1], mx[par[u][j-1]][j-1]);
	}
	
	tin[u] = ++t;
	for(auto x : edges[u]){
		if(x[0] == p) continue;
		par[x[0]][0] = u, mx[x[0]][0] = x[1];
		dfs(x[0], u);
	} tout[u] = t;
}

void init(int n, int m, vector<int> u, vector<int> v, vector<int> w) {
	vector<int> p(m); iota(p.begin(), p.end(), 0);
	sort(p.begin(), p.end(), [&](int i, int j){
		return (w[i] < w[j]);
	});
	
	vector<int> par(n), sz(n, 1), d(n);
	vector<vector<int>> sub(n);
	for(int i=0;i<n;i++){
		par[i] = i;
		sub[i].push_back(i);
	}
	
	auto upd = [&](int a, int c){
		for(auto x : sub[a]) is[x] = c;
		sub[a].clear();
	};
	function<int(int)> f = [&](int x) { return (par[x] == x ? x : par[x] = f(par[x])); };
	auto merge = [&](int a, int b, int c){
		d[a]++, d[b]++;
		bool t = (d[a] >= 3 || d[b] >= 3);
		a = f(a), b = f(b);
		if(a == b){
			upd(a, c);
			return false;
		}
		
		if(sz[a] < sz[b]) swap(a, b);
		par[b] = a, sz[a] += sz[b];
		sub[a].insert(sub[a].end(), sub[b].begin(), sub[b].end());
		
		if(is[a] && is[b]) return true;
		if(!is[a] && !is[b]){
			if(t) upd(a, c);
			return true;
		}
		
		upd(a, c);
		return true;
	};
	
	for(auto i : p){
		if(merge(u[i], v[i], w[i])){
			edges[u[i]].push_back({v[i], w[i]});
			edges[v[i]].push_back({u[i], w[i]});
		}
	}
	
	::par[0][0] = 1;
	dfs(0, -1);
}

bool P(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }
int lca(int a, int b){
	if(P(a, b)) return a;
	if(P(b, a)) return b;
	for(int j=19;~j;j--){
		if(!P(par[a][j], b)) a = par[a][j];
	} return par[a][0];
}

int getMinimumFuelCapacity(int a, int b) {
	int mx = 0;
	int p = lca(a, b);
	//~ cout<<a<<" "<<b<<" "<<p<<"\n";
	{
		int x = a;
		for(int j=19;~j;j--){
			if(!P(par[x][j], p)){
				mx = max(mx, ::mx[x][j]);
				x = par[x][j];
			}
		} if(x != p) mx = max(mx, ::mx[x][0]);
	}
	{
		int x = b;
		for(int j=19;~j;j--){
			if(!P(par[x][j], p)){
				mx = max(mx, ::mx[x][j]);
				x = par[x][j];
			}
		} if(x != p) mx = max(mx, ::mx[x][0]);
	}
	assert(mx);
	if(!is[a]) return -1;
	return max({mx, is[a], is[b]});
}

/*

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

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2664 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 3028 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 3 ms 2928 KB Output is correct
8 Correct 2 ms 3056 KB Output is correct
9 Correct 144 ms 33316 KB Output is correct
10 Correct 205 ms 41404 KB Output is correct
11 Correct 188 ms 40248 KB Output is correct
12 Correct 180 ms 42944 KB Output is correct
13 Correct 164 ms 41980 KB Output is correct
14 Correct 186 ms 33004 KB Output is correct
15 Correct 470 ms 45360 KB Output is correct
16 Correct 436 ms 42632 KB Output is correct
17 Correct 465 ms 48508 KB Output is correct
18 Correct 421 ms 44472 KB Output is correct
19 Correct 112 ms 13896 KB Output is correct
20 Correct 455 ms 44924 KB Output is correct
21 Correct 457 ms 42596 KB Output is correct
22 Correct 419 ms 46688 KB Output is correct
23 Correct 453 ms 44412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2664 KB Output is correct
3 Incorrect 262 ms 36500 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2664 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 3028 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 3 ms 2928 KB Output is correct
8 Correct 2 ms 3056 KB Output is correct
9 Correct 2 ms 2664 KB Output is correct
10 Correct 3 ms 2936 KB Output is correct
11 Correct 3 ms 3028 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 2900 KB Output is correct
14 Correct 2 ms 2936 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 2 ms 2936 KB Output is correct
17 Incorrect 2 ms 3028 KB Output isn't correct
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2664 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2664 KB Output is correct
4 Correct 3 ms 2668 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 3028 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 3 ms 2928 KB Output is correct
9 Correct 2 ms 3056 KB Output is correct
10 Correct 144 ms 33316 KB Output is correct
11 Correct 205 ms 41404 KB Output is correct
12 Correct 188 ms 40248 KB Output is correct
13 Correct 180 ms 42944 KB Output is correct
14 Correct 164 ms 41980 KB Output is correct
15 Correct 3 ms 2936 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 2900 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 2 ms 2936 KB Output is correct
20 Correct 2 ms 3028 KB Output is correct
21 Correct 2 ms 2936 KB Output is correct
22 Incorrect 2 ms 3028 KB Output isn't correct
23 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 3 ms 2664 KB Output is correct
3 Correct 3 ms 2668 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 3 ms 3028 KB Output is correct
6 Correct 3 ms 2900 KB Output is correct
7 Correct 3 ms 2928 KB Output is correct
8 Correct 2 ms 3056 KB Output is correct
9 Correct 144 ms 33316 KB Output is correct
10 Correct 205 ms 41404 KB Output is correct
11 Correct 188 ms 40248 KB Output is correct
12 Correct 180 ms 42944 KB Output is correct
13 Correct 164 ms 41980 KB Output is correct
14 Correct 186 ms 33004 KB Output is correct
15 Correct 470 ms 45360 KB Output is correct
16 Correct 436 ms 42632 KB Output is correct
17 Correct 465 ms 48508 KB Output is correct
18 Correct 421 ms 44472 KB Output is correct
19 Incorrect 262 ms 36500 KB Output isn't correct
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2664 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 3 ms 2664 KB Output is correct
4 Correct 3 ms 2668 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 3 ms 3028 KB Output is correct
7 Correct 3 ms 2900 KB Output is correct
8 Correct 3 ms 2928 KB Output is correct
9 Correct 2 ms 3056 KB Output is correct
10 Correct 144 ms 33316 KB Output is correct
11 Correct 205 ms 41404 KB Output is correct
12 Correct 188 ms 40248 KB Output is correct
13 Correct 180 ms 42944 KB Output is correct
14 Correct 164 ms 41980 KB Output is correct
15 Correct 186 ms 33004 KB Output is correct
16 Correct 470 ms 45360 KB Output is correct
17 Correct 436 ms 42632 KB Output is correct
18 Correct 465 ms 48508 KB Output is correct
19 Correct 421 ms 44472 KB Output is correct
20 Incorrect 262 ms 36500 KB Output isn't correct
21 Halted 0 ms 0 KB -