Submission #341280

# Submission time Handle Problem Language Result Execution time Memory
341280 2020-12-29T10:49:32 Z Tosic Hard route (IZhO17_road) C++14
52 / 100
595 ms 64260 KB
#include <bits/stdc++.h>
#define maxn 5010
using namespace std;

int n, ans, cnt, maxO[maxn], iT[maxn], oT[maxn], gT;
vector<int> dp[maxn];
vector<vector<int>> gr;

void dfs(int x, int p){
	iT[x] = gT;
	++gT;
	dp[x].push_back(0);
	for(auto i:gr[x]){
		if(i ==p){
			continue;
		}
		dfs(i, x);
		dp[x].push_back(dp[i].back()+1);
	}
	sort(dp[x].begin(), dp[x].end());
	oT[x] = gT;
	++gT;
}

void dfs1(int cur, int p, int x, int d){
	if(iT[cur] > iT[x] and oT[cur] < oT[x]){
		return;
	}
	maxO[x] = max(maxO[x], d);
	
	
	for(auto i : gr[cur]){
		if(i==p){
			continue;
		}
		dfs1(i, cur, x, d+1);
	}
}

bool isA(int x, int y){
	return iT[x] < iT[y];
}

bool isL(int x){
	return gr[x].size()==1;
}

void cDfs(int x, int p, bool up, int d, int res){
	if(p != -1 and isL(x)){
		if(ans == d*res){
			++cnt;
		}
		if(ans < d*res){
			cnt = 1;
			ans = d*res;
		}
		return;
	}
	for(auto i : gr[x]){
		if(i != p){
			if(isA(x, i) and up){
				int idx = int(dp[x].size())-1;
				if(dp[x][idx] == max(dp[i].back()+1,dp[p].back()+1) ){
					--idx;
				}
				if(dp[x][idx] == min(dp[p].back()+1,dp[i].back()+1) ){
					--idx;
				}
				int tmpRes = max(res, dp[x][idx]);
				tmpRes = max(tmpRes, maxO[x]);
				cDfs(i, x, 0, d+1, tmpRes);
			} else {
				int idx = int(dp[x].size())-1, tmpRes;
				if(up){
					if(p >= 0 and dp[x][idx] == dp[p].back()+1){
						--idx;
					}
					tmpRes = max(res, dp[x][idx]);
				} else {
					if(dp[x][idx] == dp[i].back()+1){
						--idx;
					}
					tmpRes = max(res, dp[x][idx]);
				}
				cDfs(i, x, up, d+1, tmpRes);
			}
		}
	}
}

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> n;
	gr.resize(n, vector<int>());
	for(int i = 0; i < n-1; ++i){
		int u, v;
		cin >> u >> v;
		--u,--v;
		gr[u].push_back(v);
		gr[v].push_back(u);
	}
	if(n == 2){
		cout << "0 1";
		return 0;
	}
	for(int i = 0; i < n; ++i){
		if(!isL(i)){
			dfs(i, -1);
			break;
		}
	}
	for(int i = 0; i < n; ++i){
		dfs1(i, -1, i, 0);
	}
	for(int i = 0; i < n; ++i){
		if(isL(i)){
			cDfs(i, -1, 1, 0, 0);
		}
	}
	cout<<ans<<' '<<cnt/2<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 441 ms 1260 KB Output is correct
26 Correct 478 ms 1388 KB Output is correct
27 Correct 457 ms 1328 KB Output is correct
28 Correct 491 ms 1260 KB Output is correct
29 Correct 441 ms 1260 KB Output is correct
30 Correct 425 ms 1260 KB Output is correct
31 Correct 420 ms 1260 KB Output is correct
32 Correct 439 ms 1388 KB Output is correct
33 Correct 263 ms 1388 KB Output is correct
34 Correct 249 ms 1388 KB Output is correct
35 Correct 269 ms 1516 KB Output is correct
36 Correct 266 ms 1388 KB Output is correct
37 Correct 528 ms 1516 KB Output is correct
38 Correct 357 ms 1516 KB Output is correct
39 Correct 516 ms 1260 KB Output is correct
40 Correct 581 ms 1260 KB Output is correct
41 Correct 579 ms 1260 KB Output is correct
42 Correct 595 ms 1132 KB Output is correct
43 Correct 577 ms 1004 KB Output is correct
44 Correct 576 ms 1132 KB Output is correct
45 Correct 498 ms 1132 KB Output is correct
46 Correct 372 ms 1004 KB Output is correct
47 Correct 557 ms 1132 KB Output is correct
48 Correct 584 ms 1260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 1 ms 492 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 441 ms 1260 KB Output is correct
26 Correct 478 ms 1388 KB Output is correct
27 Correct 457 ms 1328 KB Output is correct
28 Correct 491 ms 1260 KB Output is correct
29 Correct 441 ms 1260 KB Output is correct
30 Correct 425 ms 1260 KB Output is correct
31 Correct 420 ms 1260 KB Output is correct
32 Correct 439 ms 1388 KB Output is correct
33 Correct 263 ms 1388 KB Output is correct
34 Correct 249 ms 1388 KB Output is correct
35 Correct 269 ms 1516 KB Output is correct
36 Correct 266 ms 1388 KB Output is correct
37 Correct 528 ms 1516 KB Output is correct
38 Correct 357 ms 1516 KB Output is correct
39 Correct 516 ms 1260 KB Output is correct
40 Correct 581 ms 1260 KB Output is correct
41 Correct 579 ms 1260 KB Output is correct
42 Correct 595 ms 1132 KB Output is correct
43 Correct 577 ms 1004 KB Output is correct
44 Correct 576 ms 1132 KB Output is correct
45 Correct 498 ms 1132 KB Output is correct
46 Correct 372 ms 1004 KB Output is correct
47 Correct 557 ms 1132 KB Output is correct
48 Correct 584 ms 1260 KB Output is correct
49 Runtime error 287 ms 64260 KB Execution killed with signal 6 (could be triggered by violating memory limits)
50 Halted 0 ms 0 KB -