답안 #293946

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
293946 2020-09-08T13:59:17 Z oolimry 자매 도시 (APIO20_swap) C++14
13 / 100
446 ms 27092 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){
	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;
}

//g++ -std=c++14 -o swap grader.cpp swap.cpp -O2 -lm; ./swap < 01.in
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 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 88 ms 18052 KB Output is correct
10 Correct 116 ms 21868 KB Output is correct
11 Correct 112 ms 21612 KB Output is correct
12 Correct 121 ms 22764 KB Output is correct
13 Correct 121 ms 23532 KB Output is correct
14 Correct 113 ms 18152 KB Output is correct
15 Correct 299 ms 23924 KB Output is correct
16 Correct 299 ms 23292 KB Output is correct
17 Correct 305 ms 24656 KB Output is correct
18 Correct 446 ms 25300 KB Output is correct
19 Correct 117 ms 7292 KB Output is correct
20 Correct 286 ms 25064 KB Output is correct
21 Correct 305 ms 24488 KB Output is correct
22 Correct 312 ms 25940 KB Output is correct
23 Correct 446 ms 26832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 407 ms 25968 KB Output is correct
4 Correct 390 ms 27092 KB Output is correct
5 Correct 413 ms 26628 KB Output is correct
6 Correct 398 ms 26964 KB Output is correct
7 Correct 430 ms 26856 KB Output is correct
8 Correct 426 ms 25884 KB Output is correct
9 Correct 428 ms 26604 KB Output is correct
10 Correct 406 ms 25768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 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 1 ms 512 KB Output is correct
15 Incorrect 1 ms 640 KB Output isn't correct
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 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 88 ms 18052 KB Output is correct
11 Correct 116 ms 21868 KB Output is correct
12 Correct 112 ms 21612 KB Output is correct
13 Correct 121 ms 22764 KB Output is correct
14 Correct 121 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 1 ms 512 KB Output is correct
20 Incorrect 1 ms 640 KB Output isn't correct
21 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 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 88 ms 18052 KB Output is correct
10 Correct 116 ms 21868 KB Output is correct
11 Correct 112 ms 21612 KB Output is correct
12 Correct 121 ms 22764 KB Output is correct
13 Correct 121 ms 23532 KB Output is correct
14 Correct 113 ms 18152 KB Output is correct
15 Correct 299 ms 23924 KB Output is correct
16 Correct 299 ms 23292 KB Output is correct
17 Correct 305 ms 24656 KB Output is correct
18 Correct 446 ms 25300 KB Output is correct
19 Correct 407 ms 25968 KB Output is correct
20 Correct 390 ms 27092 KB Output is correct
21 Correct 413 ms 26628 KB Output is correct
22 Correct 398 ms 26964 KB Output is correct
23 Correct 430 ms 26856 KB Output is correct
24 Correct 426 ms 25884 KB Output is correct
25 Correct 428 ms 26604 KB Output is correct
26 Correct 406 ms 25768 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 1 ms 512 KB Output is correct
32 Incorrect 1 ms 640 KB Output isn't correct
33 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 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 88 ms 18052 KB Output is correct
11 Correct 116 ms 21868 KB Output is correct
12 Correct 112 ms 21612 KB Output is correct
13 Correct 121 ms 22764 KB Output is correct
14 Correct 121 ms 23532 KB Output is correct
15 Correct 113 ms 18152 KB Output is correct
16 Correct 299 ms 23924 KB Output is correct
17 Correct 299 ms 23292 KB Output is correct
18 Correct 305 ms 24656 KB Output is correct
19 Correct 446 ms 25300 KB Output is correct
20 Correct 407 ms 25968 KB Output is correct
21 Correct 390 ms 27092 KB Output is correct
22 Correct 413 ms 26628 KB Output is correct
23 Correct 398 ms 26964 KB Output is correct
24 Correct 430 ms 26856 KB Output is correct
25 Correct 426 ms 25884 KB Output is correct
26 Correct 428 ms 26604 KB Output is correct
27 Correct 406 ms 25768 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 1 ms 512 KB Output is correct
33 Incorrect 1 ms 640 KB Output isn't correct
34 Halted 0 ms 0 KB -