답안 #547911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547911 2022-04-12T02:48:18 Z amunduzbaev Mountains and Valleys (CCO20_day1problem3) C++17
1 / 25
7000 ms 12980 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
//~ #define int long long
const int N = 5e5 + 5;
vector<int> edges[N];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	vector<ar<int, 3>> e;
	int cnt = 0;
	for(int i=0;i<m;i++){
		int a, b, c; cin>>a>>b>>c; a++, b++;
		if(c == 1){
			edges[a].push_back(b);
			edges[b].push_back(a);
			cnt++;
		} else {
			e.push_back({a, b, c});
		}
	}
	assert(cnt == n - 1);
	int res = 0;
	vector<int> d(n + 1);
	function<void(int, int)> dfs = [&](int u, int p){
		for(auto x : edges[u]){
			if(x == p) continue;
			d[x] = d[u] + 1;
			dfs(x, u);
		}
	};
	int a = 1; 
	d[a] = 0, dfs(a, a);
	for(int i=1;i<=n;i++){
		if(d[i] > d[a]) a = i;
	}
	
	d[a] = 0, dfs(a, a);
	for(int i=1;i<=n;i++){
		if(d[i] > d[a]) a = i;
	}
	
	res = 2 * n - 2 - d[a];
	for(auto x : e){
		int a = x[0], b = x[1], c = x[2];
		vector<int> par(n + 1), used(n + 1), d(n + 1);
		function<void(int)> dfs = [&](int u){
			for(auto x : edges[u]){
				if(x == par[u]) continue;
				par[x] = u, dfs(x);
			}
		}; dfs(a);
		
		vector<int> tt;
		while(b){ used[b] = 1;
			tt.push_back(b);
			b = par[b];
		}
		
		function<void(int)> dfs2 = [&](int u){
			used[u] = 1; d[u] = 0;
			for(auto x : edges[u]){
				if(used[x]) continue;
				dfs2(x);
				d[u] = max(d[u], d[x] + 1);
			}
		};
		
		int mx = 0, cyc = tt.size();
		for(auto x : tt){
			dfs2(x);
		}
		
		for(int i=1;i<cyc;i++){
			mx = max(mx, d[tt[i]] + d[tt[i-1]]);
		}
		
		res = min(res, 2 * n - 2 - mx - cyc + c);
		
		int tot = (cyc - 1) + c;
		int ed = 2 * (n - 1 + c);
		for(int i=0;i<cyc;i++){
			int sub = 0;
			for(int j=i+1;j<cyc;j++){
				sub++;
				res = min(res, ed - max(sub, tot - sub) - d[i] - d[j]);
			}
		}
	}
	
	cout<<res<<"\n";
}

/*

18 18
0 1 1
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 5
7 8 1
8 9 1
9 4 1
8 10 1
10 11 1
11 12 1
12 13 1
6 14 1
14 15 1
15 16 1
16 17 1

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 7078 ms 12980 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 11988 KB Output is correct
3 Correct 7 ms 12052 KB Output is correct
4 Correct 6 ms 11956 KB Output is correct
5 Correct 7 ms 11988 KB Output is correct
6 Correct 7 ms 12068 KB Output is correct
7 Correct 7 ms 12064 KB Output is correct
8 Correct 9 ms 12244 KB Output is correct
9 Correct 7 ms 12040 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 8 ms 12048 KB Output is correct
12 Correct 7 ms 12064 KB Output is correct
13 Correct 6 ms 12052 KB Output is correct
14 Correct 6 ms 12040 KB Output is correct
15 Correct 6 ms 11956 KB Output is correct
16 Correct 6 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Incorrect 8 ms 12008 KB Output isn't correct
19 Halted 0 ms 0 KB -