답안 #273938

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
273938 2020-08-19T07:55:48 Z 임성재(#5106) Mountains and Valleys (CCO20_day1problem3) C++17
11 / 25
4054 ms 52620 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;

int n, m;
vector<int> g[500010];
int d[500010];
int sp[500010][21];
vector<int> D[500010];
vector<pair<pii, int>> e;
int dis[511][511];

int lca(int a, int b) {
	if(d[a] > d[b]) swap(a, b);

	for(int i=20; i>=0; i--) {
		if(d[b] - d[a] >= (1<<i)) b = sp[b][i];
	}

	if(a == b) return a;

	for(int i=20; i>=0; i--) {
		if(sp[a][i] != sp[b][i]) {
			a = sp[a][i];
			b = sp[b][i];
		}
	}

	return sp[a][0];
}

int dist(int a, int b) {
	return d[a] + d[b] - 2 * d[lca(a, b)];
}

int cross(int a,int b, int x, int y) {
	return (dis[a][b] + dis[x][y] - min(dis[x][a] + dis[y][b], dis[x][b] + dis[y][a])) / 2;
}

void dfs(int x) {
	for(int i=1; i<=20; i++)
		sp[x][i] = sp[sp[x][i-1]][i-1];

	D[x].eb(x);
	D[x].eb(x);

	for(auto i : g[x]) {
		if(i == sp[x][0]) continue;

		d[i] = d[x] + 1;
		sp[i][0] = x;
		dfs(i);

		int mx = dist(D[x][0], D[x][1]);
		pii mxi = mp(D[x][0], D[x][1]);

		if(dist(D[i][0], D[i][1]) > mx) {
			mx = dist(D[i][0], D[i][1]);
			mxi = mp(D[i][0], D[i][1]);
		}

		for(int j=0; j<2; j++) {
			for(int k=0; k<2; k++) {
				if(dist(D[x][j], D[i][k]) > mx) {
					mx = dist(D[x][j], D[i][k]);
					mxi = mp(D[x][j], D[i][k]);
				}
			}
		}

		D[x][0] = mxi.fi;
		D[x][1] = mxi.se;
	}
}

int main() {
	fast;

	cin >> n >> m;

	for(int i=0; i<m; i++) {
		int x, y, w;
		cin >> x >> y >> w;

		if(w == 1) {
			g[x].eb(y);
			g[y].eb(x);
		}
		else {
			e.eb(mp(x, y), w);
		}
	}

	dfs(0);

	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
			dis[i][j] = dist(i, j);

	int ans = 2 * n - 2 - dis[D[0][0]][D[0][1]];

	for(auto i : e) {
		int x = i.fi.fi;
		int y = i.fi.se;
		int w = i.se;

		for(int u=0; u<n; u++) {
			for(int v = 0; v<n; v++) {
				int f = 2 * n - 2 + w;
				
				if(cross(x, y, u, v) <= 0) f -= dis[x][y] + dis[u][v];
				else if(cross(x, u, y, v) < 0) f -= dis[x][u] + dis[y][v] + 2;

				ans = min(f, ans);
			}
		}
	}

	cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4054 ms 52620 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
36 Correct 37 ms 24064 KB Output is correct
37 Correct 23 ms 24064 KB Output is correct
38 Correct 34 ms 24064 KB Output is correct
39 Correct 22 ms 24064 KB Output is correct
40 Correct 23 ms 24064 KB Output is correct
41 Correct 24 ms 24064 KB Output is correct
42 Correct 24 ms 24096 KB Output is correct
43 Correct 23 ms 24064 KB Output is correct
44 Correct 22 ms 24064 KB Output is correct
45 Correct 26 ms 24056 KB Output is correct
46 Correct 22 ms 24064 KB Output is correct
47 Correct 22 ms 24064 KB Output is correct
48 Correct 21 ms 24064 KB Output is correct
49 Correct 21 ms 24048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
36 Correct 37 ms 24064 KB Output is correct
37 Correct 23 ms 24064 KB Output is correct
38 Correct 34 ms 24064 KB Output is correct
39 Correct 22 ms 24064 KB Output is correct
40 Correct 23 ms 24064 KB Output is correct
41 Correct 24 ms 24064 KB Output is correct
42 Correct 24 ms 24096 KB Output is correct
43 Correct 23 ms 24064 KB Output is correct
44 Correct 22 ms 24064 KB Output is correct
45 Correct 26 ms 24056 KB Output is correct
46 Correct 22 ms 24064 KB Output is correct
47 Correct 22 ms 24064 KB Output is correct
48 Correct 21 ms 24064 KB Output is correct
49 Correct 21 ms 24048 KB Output is correct
50 Correct 627 ms 25012 KB Output is correct
51 Correct 755 ms 25004 KB Output is correct
52 Correct 676 ms 24984 KB Output is correct
53 Correct 616 ms 24952 KB Output is correct
54 Correct 562 ms 24952 KB Output is correct
55 Correct 417 ms 24952 KB Output is correct
56 Correct 646 ms 24996 KB Output is correct
57 Correct 752 ms 24996 KB Output is correct
58 Correct 682 ms 24988 KB Output is correct
59 Correct 657 ms 24988 KB Output is correct
60 Correct 746 ms 24984 KB Output is correct
61 Correct 701 ms 24924 KB Output is correct
62 Correct 142 ms 24980 KB Output is correct
63 Correct 137 ms 24952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
36 Runtime error 4054 ms 52620 KB Execution killed with signal 11
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
36 Runtime error 4054 ms 52620 KB Execution killed with signal 11
37 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23808 KB Output is correct
2 Correct 22 ms 23808 KB Output is correct
3 Correct 16 ms 23808 KB Output is correct
4 Correct 22 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 18 ms 24028 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 16 ms 23808 KB Output is correct
9 Correct 16 ms 23808 KB Output is correct
10 Correct 17 ms 23808 KB Output is correct
11 Correct 17 ms 23808 KB Output is correct
12 Correct 16 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 20 ms 23808 KB Output is correct
15 Correct 16 ms 23808 KB Output is correct
16 Correct 20 ms 23808 KB Output is correct
17 Correct 16 ms 23808 KB Output is correct
18 Correct 17 ms 23808 KB Output is correct
19 Correct 17 ms 23808 KB Output is correct
20 Correct 16 ms 23808 KB Output is correct
21 Correct 17 ms 23808 KB Output is correct
22 Correct 18 ms 23808 KB Output is correct
23 Correct 17 ms 23808 KB Output is correct
24 Correct 17 ms 23936 KB Output is correct
25 Correct 16 ms 23808 KB Output is correct
26 Correct 19 ms 23936 KB Output is correct
27 Correct 22 ms 23808 KB Output is correct
28 Correct 20 ms 23808 KB Output is correct
29 Correct 20 ms 23808 KB Output is correct
30 Correct 17 ms 23808 KB Output is correct
31 Correct 21 ms 23936 KB Output is correct
32 Correct 16 ms 23808 KB Output is correct
33 Correct 16 ms 23808 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 18 ms 23808 KB Output is correct
36 Runtime error 4054 ms 52620 KB Execution killed with signal 11
37 Halted 0 ms 0 KB -