답안 #294917

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
294917 2020-09-09T10:42:23 Z Berted 자매 도시 (APIO20_swap) C++14
100 / 100
512 ms 45804 KB
#include "swap.h"

#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>
#define pii pair<int, int>
#define fst first
#define snd second
#define ppi pair<pii, int>
#define vi vector<int>
using namespace std;

const int INF = 1e9, LG = 19;

ppi edge[200001];
int par[100001], cur[100001], idx, deg[100001];

vi tree[300001];
int sps[300001][LG], val[300001], h[300001];
bool st[300001];

inline bool comp(const ppi &l, const ppi &r)
{
	return make_pair(l.snd, l.fst) < make_pair(r.snd, r.fst);
}

int fn(int x) {return par[x] = (par[x] == x) ? x : fn(par[x]);}
void jn(int a, int b, int v)
{	
	deg[a]++; deg[b]++;
	bool ck = (deg[a] > 2) || (deg[b] > 2);
	a = fn(a), b = fn(b);
	if (a != b)
	{
		ck |= st[cur[a]] || st[cur[b]];
		par[b] = a;
		//cout << "Edge from " << idx << " -> " << cur[a] << "\n";
		//cout << "Edge from " << idx << " -> " << cur[b] << "\n";
		tree[idx].push_back(cur[a]);
		tree[idx].push_back(cur[b]);
		cur[a] = idx++;
		val[cur[a]] = v; st[cur[a]] = ck;
		//cout << "Data : " << cur[a] << " " << val[cur[a]] << " " << st[cur[a]] << "\n";
	}
	else
	{
		if (!st[cur[a]])
		{
			//cout << "Edge from " << idx << " -> " << cur[a] << "\n";
			tree[idx].push_back(cur[a]);
			cur[a] = idx++;
			val[cur[a]] = v; st[cur[a]] = 1;
			//cout << "Data : " << cur[a] << " " << val[cur[a]] << " " << st[cur[a]] << "\n";
		}
	}
}

void DFS(int u, int p)
{
	h[u] = (p == -1) ? 0 : (h[p] + 1);
	sps[u][0] = p;
	//cout << u << " " << h[u] << " " << sps[u][0] << "\n";
	for (int i = 1; i < LG; i++)
	{
		if (sps[u][i - 1] != -1) {sps[u][i] = sps[sps[u][i - 1]][i - 1];}
		else {sps[u][i] = -1;}
	}
	for (auto v : tree[u]) DFS(v, u);
}

int findLCA(int u, int v)
{
	if (h[u] > h[v]) swap(u, v);
	for (int i = 0; h[v] - h[u]; i++) 
	{
		assert(i < LG);
		if ((h[v] - h[u]) & (1 << i)) v = sps[v][i];
	}
	if (u == v) return u;
	for (int i = LG - 1; i >= 0; i--)
	{
		if (sps[u][i] != sps[v][i]) 
		{
			u = sps[u][i];
			v = sps[v][i];
		}
	}
	return sps[u][0];
}

int findValid(int x)
{
	if (st[x]) return x;
	for (int i = LG - 1; i >= 0; i--)
	{
		if (sps[x][i] != -1 && !st[sps[x][i]]) {x = sps[x][i];}
	}
	return sps[x][0];
}

void init(int N, int M, vector<int> U, vector<int> V, vector<int> W) 
{
	for (int i = 0; i < N; i++)
	{
		cur[i] = i; deg[i] = 0; par[i] = i;
	}
	idx = N;
	for (int i = 0; i < M; i++)
	{
		edge[i] = {{U[i], V[i]}, W[i]};
	}
	sort(edge, edge + M, comp);
	for (int i = 0; i < M; i++)
	{
		jn(edge[i].fst.fst, edge[i].fst.snd, edge[i].snd);
	}
	DFS(idx - 1, -1);
}

int getMinimumFuelCapacity(int X, int Y) 
{
	if (!st[idx - 1]) return -1;
	else
	{
		int L = findLCA(X, Y);
		return val[findValid(L)];
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7400 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 7 ms 7552 KB Output is correct
5 Correct 6 ms 7680 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 7 ms 7680 KB Output is correct
8 Correct 7 ms 7680 KB Output is correct
9 Correct 129 ms 28008 KB Output is correct
10 Correct 149 ms 32420 KB Output is correct
11 Correct 153 ms 31992 KB Output is correct
12 Correct 177 ms 33588 KB Output is correct
13 Correct 163 ms 35064 KB Output is correct
14 Correct 134 ms 28280 KB Output is correct
15 Correct 222 ms 36784 KB Output is correct
16 Correct 228 ms 36008 KB Output is correct
17 Correct 235 ms 37600 KB Output is correct
18 Correct 233 ms 39136 KB Output is correct
19 Correct 116 ms 16684 KB Output is correct
20 Correct 314 ms 37820 KB Output is correct
21 Correct 311 ms 37284 KB Output is correct
22 Correct 322 ms 39076 KB Output is correct
23 Correct 512 ms 40688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7400 KB Output is correct
3 Correct 389 ms 40092 KB Output is correct
4 Correct 384 ms 41180 KB Output is correct
5 Correct 403 ms 40628 KB Output is correct
6 Correct 378 ms 40928 KB Output is correct
7 Correct 393 ms 40952 KB Output is correct
8 Correct 389 ms 39716 KB Output is correct
9 Correct 381 ms 40700 KB Output is correct
10 Correct 399 ms 39508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7400 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 7 ms 7552 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 6 ms 7680 KB Output is correct
8 Correct 7 ms 7680 KB Output is correct
9 Correct 7 ms 7680 KB Output is correct
10 Correct 7 ms 7680 KB Output is correct
11 Correct 7 ms 7680 KB Output is correct
12 Correct 7 ms 7680 KB Output is correct
13 Correct 7 ms 7552 KB Output is correct
14 Correct 6 ms 7552 KB Output is correct
15 Correct 7 ms 7680 KB Output is correct
16 Correct 7 ms 7680 KB Output is correct
17 Correct 6 ms 7680 KB Output is correct
18 Correct 6 ms 7680 KB Output is correct
19 Correct 6 ms 7552 KB Output is correct
20 Correct 7 ms 7680 KB Output is correct
21 Correct 6 ms 7680 KB Output is correct
22 Correct 7 ms 7552 KB Output is correct
23 Correct 6 ms 7552 KB Output is correct
24 Correct 7 ms 7680 KB Output is correct
25 Correct 8 ms 7680 KB Output is correct
26 Correct 7 ms 7680 KB Output is correct
27 Correct 6 ms 7680 KB Output is correct
28 Correct 7 ms 7680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7400 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 7 ms 7552 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 6 ms 7680 KB Output is correct
8 Correct 7 ms 7680 KB Output is correct
9 Correct 7 ms 7680 KB Output is correct
10 Correct 129 ms 28008 KB Output is correct
11 Correct 149 ms 32420 KB Output is correct
12 Correct 153 ms 31992 KB Output is correct
13 Correct 177 ms 33588 KB Output is correct
14 Correct 163 ms 35064 KB Output is correct
15 Correct 7 ms 7680 KB Output is correct
16 Correct 7 ms 7680 KB Output is correct
17 Correct 7 ms 7680 KB Output is correct
18 Correct 7 ms 7552 KB Output is correct
19 Correct 6 ms 7552 KB Output is correct
20 Correct 7 ms 7680 KB Output is correct
21 Correct 7 ms 7680 KB Output is correct
22 Correct 6 ms 7680 KB Output is correct
23 Correct 6 ms 7680 KB Output is correct
24 Correct 20 ms 10880 KB Output is correct
25 Correct 163 ms 33144 KB Output is correct
26 Correct 159 ms 33144 KB Output is correct
27 Correct 164 ms 33144 KB Output is correct
28 Correct 145 ms 32888 KB Output is correct
29 Correct 159 ms 32764 KB Output is correct
30 Correct 140 ms 30584 KB Output is correct
31 Correct 160 ms 33528 KB Output is correct
32 Correct 153 ms 33272 KB Output is correct
33 Correct 185 ms 35356 KB Output is correct
34 Correct 164 ms 33528 KB Output is correct
35 Correct 6 ms 7552 KB Output is correct
36 Correct 7 ms 7680 KB Output is correct
37 Correct 6 ms 7680 KB Output is correct
38 Correct 7 ms 7552 KB Output is correct
39 Correct 6 ms 7552 KB Output is correct
40 Correct 7 ms 7680 KB Output is correct
41 Correct 8 ms 7680 KB Output is correct
42 Correct 7 ms 7680 KB Output is correct
43 Correct 6 ms 7680 KB Output is correct
44 Correct 7 ms 7680 KB Output is correct
45 Correct 169 ms 32636 KB Output is correct
46 Correct 153 ms 33264 KB Output is correct
47 Correct 170 ms 33144 KB Output is correct
48 Correct 166 ms 34040 KB Output is correct
49 Correct 117 ms 16008 KB Output is correct
50 Correct 85 ms 15608 KB Output is correct
51 Correct 147 ms 28280 KB Output is correct
52 Correct 197 ms 36976 KB Output is correct
53 Correct 241 ms 42104 KB Output is correct
54 Correct 223 ms 38776 KB Output is correct
55 Correct 173 ms 35064 KB Output is correct
56 Correct 214 ms 38524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7424 KB Output is correct
2 Correct 6 ms 7400 KB Output is correct
3 Correct 5 ms 7424 KB Output is correct
4 Correct 7 ms 7552 KB Output is correct
5 Correct 6 ms 7680 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 7 ms 7680 KB Output is correct
8 Correct 7 ms 7680 KB Output is correct
9 Correct 129 ms 28008 KB Output is correct
10 Correct 149 ms 32420 KB Output is correct
11 Correct 153 ms 31992 KB Output is correct
12 Correct 177 ms 33588 KB Output is correct
13 Correct 163 ms 35064 KB Output is correct
14 Correct 134 ms 28280 KB Output is correct
15 Correct 222 ms 36784 KB Output is correct
16 Correct 228 ms 36008 KB Output is correct
17 Correct 235 ms 37600 KB Output is correct
18 Correct 233 ms 39136 KB Output is correct
19 Correct 389 ms 40092 KB Output is correct
20 Correct 384 ms 41180 KB Output is correct
21 Correct 403 ms 40628 KB Output is correct
22 Correct 378 ms 40928 KB Output is correct
23 Correct 393 ms 40952 KB Output is correct
24 Correct 389 ms 39716 KB Output is correct
25 Correct 381 ms 40700 KB Output is correct
26 Correct 399 ms 39508 KB Output is correct
27 Correct 7 ms 7680 KB Output is correct
28 Correct 7 ms 7680 KB Output is correct
29 Correct 7 ms 7680 KB Output is correct
30 Correct 7 ms 7552 KB Output is correct
31 Correct 6 ms 7552 KB Output is correct
32 Correct 7 ms 7680 KB Output is correct
33 Correct 7 ms 7680 KB Output is correct
34 Correct 6 ms 7680 KB Output is correct
35 Correct 6 ms 7680 KB Output is correct
36 Correct 20 ms 10880 KB Output is correct
37 Correct 163 ms 33144 KB Output is correct
38 Correct 159 ms 33144 KB Output is correct
39 Correct 164 ms 33144 KB Output is correct
40 Correct 145 ms 32888 KB Output is correct
41 Correct 159 ms 32764 KB Output is correct
42 Correct 140 ms 30584 KB Output is correct
43 Correct 160 ms 33528 KB Output is correct
44 Correct 153 ms 33272 KB Output is correct
45 Correct 185 ms 35356 KB Output is correct
46 Correct 164 ms 33528 KB Output is correct
47 Correct 30 ms 11152 KB Output is correct
48 Correct 298 ms 38236 KB Output is correct
49 Correct 316 ms 38236 KB Output is correct
50 Correct 305 ms 37984 KB Output is correct
51 Correct 362 ms 37992 KB Output is correct
52 Correct 311 ms 36648 KB Output is correct
53 Correct 267 ms 30572 KB Output is correct
54 Correct 306 ms 39008 KB Output is correct
55 Correct 308 ms 38112 KB Output is correct
56 Correct 426 ms 39644 KB Output is correct
57 Correct 354 ms 39008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7424 KB Output is correct
2 Correct 5 ms 7424 KB Output is correct
3 Correct 6 ms 7400 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 7 ms 7552 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 6 ms 7680 KB Output is correct
8 Correct 7 ms 7680 KB Output is correct
9 Correct 7 ms 7680 KB Output is correct
10 Correct 129 ms 28008 KB Output is correct
11 Correct 149 ms 32420 KB Output is correct
12 Correct 153 ms 31992 KB Output is correct
13 Correct 177 ms 33588 KB Output is correct
14 Correct 163 ms 35064 KB Output is correct
15 Correct 134 ms 28280 KB Output is correct
16 Correct 222 ms 36784 KB Output is correct
17 Correct 228 ms 36008 KB Output is correct
18 Correct 235 ms 37600 KB Output is correct
19 Correct 233 ms 39136 KB Output is correct
20 Correct 389 ms 40092 KB Output is correct
21 Correct 384 ms 41180 KB Output is correct
22 Correct 403 ms 40628 KB Output is correct
23 Correct 378 ms 40928 KB Output is correct
24 Correct 393 ms 40952 KB Output is correct
25 Correct 389 ms 39716 KB Output is correct
26 Correct 381 ms 40700 KB Output is correct
27 Correct 399 ms 39508 KB Output is correct
28 Correct 7 ms 7680 KB Output is correct
29 Correct 7 ms 7680 KB Output is correct
30 Correct 7 ms 7680 KB Output is correct
31 Correct 7 ms 7552 KB Output is correct
32 Correct 6 ms 7552 KB Output is correct
33 Correct 7 ms 7680 KB Output is correct
34 Correct 7 ms 7680 KB Output is correct
35 Correct 6 ms 7680 KB Output is correct
36 Correct 6 ms 7680 KB Output is correct
37 Correct 20 ms 10880 KB Output is correct
38 Correct 163 ms 33144 KB Output is correct
39 Correct 159 ms 33144 KB Output is correct
40 Correct 164 ms 33144 KB Output is correct
41 Correct 145 ms 32888 KB Output is correct
42 Correct 159 ms 32764 KB Output is correct
43 Correct 140 ms 30584 KB Output is correct
44 Correct 160 ms 33528 KB Output is correct
45 Correct 153 ms 33272 KB Output is correct
46 Correct 185 ms 35356 KB Output is correct
47 Correct 164 ms 33528 KB Output is correct
48 Correct 30 ms 11152 KB Output is correct
49 Correct 298 ms 38236 KB Output is correct
50 Correct 316 ms 38236 KB Output is correct
51 Correct 305 ms 37984 KB Output is correct
52 Correct 362 ms 37992 KB Output is correct
53 Correct 311 ms 36648 KB Output is correct
54 Correct 267 ms 30572 KB Output is correct
55 Correct 306 ms 39008 KB Output is correct
56 Correct 308 ms 38112 KB Output is correct
57 Correct 426 ms 39644 KB Output is correct
58 Correct 354 ms 39008 KB Output is correct
59 Correct 116 ms 16684 KB Output is correct
60 Correct 314 ms 37820 KB Output is correct
61 Correct 311 ms 37284 KB Output is correct
62 Correct 322 ms 39076 KB Output is correct
63 Correct 512 ms 40688 KB Output is correct
64 Correct 6 ms 7552 KB Output is correct
65 Correct 7 ms 7680 KB Output is correct
66 Correct 6 ms 7680 KB Output is correct
67 Correct 7 ms 7552 KB Output is correct
68 Correct 6 ms 7552 KB Output is correct
69 Correct 7 ms 7680 KB Output is correct
70 Correct 8 ms 7680 KB Output is correct
71 Correct 7 ms 7680 KB Output is correct
72 Correct 6 ms 7680 KB Output is correct
73 Correct 7 ms 7680 KB Output is correct
74 Correct 169 ms 32636 KB Output is correct
75 Correct 153 ms 33264 KB Output is correct
76 Correct 170 ms 33144 KB Output is correct
77 Correct 166 ms 34040 KB Output is correct
78 Correct 117 ms 16008 KB Output is correct
79 Correct 85 ms 15608 KB Output is correct
80 Correct 147 ms 28280 KB Output is correct
81 Correct 197 ms 36976 KB Output is correct
82 Correct 241 ms 42104 KB Output is correct
83 Correct 223 ms 38776 KB Output is correct
84 Correct 173 ms 35064 KB Output is correct
85 Correct 214 ms 38524 KB Output is correct
86 Correct 96 ms 17964 KB Output is correct
87 Correct 307 ms 38108 KB Output is correct
88 Correct 301 ms 38200 KB Output is correct
89 Correct 369 ms 37400 KB Output is correct
90 Correct 206 ms 20560 KB Output is correct
91 Correct 225 ms 22432 KB Output is correct
92 Correct 318 ms 33220 KB Output is correct
93 Correct 370 ms 41724 KB Output is correct
94 Correct 420 ms 45804 KB Output is correct
95 Correct 373 ms 42744 KB Output is correct
96 Correct 424 ms 39900 KB Output is correct
97 Correct 387 ms 41400 KB Output is correct