Submission #273868

# Submission time Handle Problem Language Result Execution time Memory
273868 2020-08-19T07:41:03 Z 임성재(#5106) Mountains and Valleys (CCO20_day1problem3) C++17
9 / 25
7000 ms 25088 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 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 (dist(a, b) + dist(x, y) - min(dist(x, a) + dist(y, b), dist(x, b) + dist(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);

	int ans = 2 * n - 2 - dist(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 -= dist(x, y) + dist(u, v);
				else if(cross(x, u, y, v) < 0) f -= dist(x, u) + dist(y, v) + 2;

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

	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 7076 ms 25088 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Correct 1090 ms 23908 KB Output is correct
37 Correct 1147 ms 23808 KB Output is correct
38 Correct 1160 ms 23900 KB Output is correct
39 Correct 1014 ms 23904 KB Output is correct
40 Correct 958 ms 23808 KB Output is correct
41 Correct 661 ms 23900 KB Output is correct
42 Correct 950 ms 23908 KB Output is correct
43 Correct 1153 ms 23904 KB Output is correct
44 Correct 1219 ms 23928 KB Output is correct
45 Correct 1046 ms 23904 KB Output is correct
46 Correct 1176 ms 23808 KB Output is correct
47 Correct 1064 ms 23808 KB Output is correct
48 Correct 186 ms 23808 KB Output is correct
49 Correct 193 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Correct 1090 ms 23908 KB Output is correct
37 Correct 1147 ms 23808 KB Output is correct
38 Correct 1160 ms 23900 KB Output is correct
39 Correct 1014 ms 23904 KB Output is correct
40 Correct 958 ms 23808 KB Output is correct
41 Correct 661 ms 23900 KB Output is correct
42 Correct 950 ms 23908 KB Output is correct
43 Correct 1153 ms 23904 KB Output is correct
44 Correct 1219 ms 23928 KB Output is correct
45 Correct 1046 ms 23904 KB Output is correct
46 Correct 1176 ms 23808 KB Output is correct
47 Correct 1064 ms 23808 KB Output is correct
48 Correct 186 ms 23808 KB Output is correct
49 Correct 193 ms 23808 KB Output is correct
50 Execution timed out 7022 ms 23936 KB Time limit exceeded
51 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Execution timed out 7076 ms 25088 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Execution timed out 7076 ms 25088 KB Time limit exceeded
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23808 KB Output is correct
2 Correct 16 ms 23808 KB Output is correct
3 Correct 18 ms 23808 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 16 ms 23808 KB Output is correct
6 Correct 16 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 17 ms 23808 KB Output is correct
10 Correct 17 ms 23936 KB Output is correct
11 Correct 16 ms 23808 KB Output is correct
12 Correct 20 ms 23808 KB Output is correct
13 Correct 17 ms 23808 KB Output is correct
14 Correct 18 ms 23808 KB Output is correct
15 Correct 18 ms 23808 KB Output is correct
16 Correct 19 ms 23808 KB Output is correct
17 Correct 17 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 17 ms 23808 KB Output is correct
21 Correct 20 ms 23808 KB Output is correct
22 Correct 27 ms 23896 KB Output is correct
23 Correct 23 ms 23808 KB Output is correct
24 Correct 24 ms 23808 KB Output is correct
25 Correct 25 ms 23808 KB Output is correct
26 Correct 23 ms 23808 KB Output is correct
27 Correct 21 ms 23808 KB Output is correct
28 Correct 25 ms 23808 KB Output is correct
29 Correct 27 ms 23808 KB Output is correct
30 Correct 25 ms 23808 KB Output is correct
31 Correct 23 ms 23808 KB Output is correct
32 Correct 22 ms 23808 KB Output is correct
33 Correct 17 ms 23840 KB Output is correct
34 Correct 17 ms 23808 KB Output is correct
35 Correct 17 ms 23808 KB Output is correct
36 Execution timed out 7076 ms 25088 KB Time limit exceeded
37 Halted 0 ms 0 KB -