Submission #599680

# Submission time Handle Problem Language Result Execution time Memory
599680 2022-07-19T18:40:43 Z GusterGoose27 Hard route (IZhO17_road) C++11
100 / 100
845 ms 229692 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 5e5;
int n;
vector<int> edges[MAXN];
pii dist[MAXN];
pii p_dist[MAXN];
ll best_h = 0;
ll num_ways = 1;

void upd(ll v, ll c) {
	if (v == best_h) num_ways += c;
	else if (v > best_h) {
		best_h = v;
		num_ways = c;
	}
}

void dfs(int cur, int p = -1) {
	dist[cur] = pii(0, 1);
	for (int next: edges[cur]) {
		if (next == p) continue;
		dfs(next, cur);
		pii ndist = dist[next];
		if (ndist.first == dist[cur].first-1) dist[cur].second += ndist.second;
		if (ndist.first >= dist[cur].first) {
			dist[cur] = ndist;
			dist[cur].first++;
		}
	}
}

void dfs2(int cur, int p = -1) {
	map<int, int> curdists;
	curdists[p_dist[cur].first] = p_dist[cur].second;
	vector<pii> distlist;
	if (cur != 0) distlist.push_back(p_dist[cur]);
	for (int next: edges[cur]) {
		if (next != p) {
			curdists[dist[next].first+1] += dist[next].second;
			distlist.push_back(pii(dist[next].first+1, dist[next].second));
		}
	}
	for (int next: edges[cur]) {
		if (next == p) continue;
		curdists[dist[next].first+1] -= dist[next].second;
		if (curdists[dist[next].first+1] == 0) curdists.erase(dist[next].first+1);
		p_dist[next] = *(curdists.rbegin());
		p_dist[next].first++;
		curdists[dist[next].first+1] += dist[next].second;
		dfs2(next, cur);
	}
	sort(distlist.begin(), distlist.end());
	assert(distlist.size() == edges[cur].size());
	if (edges[cur].size() > 2) {
		int dsize = distlist.size();
		pii aval = distlist[dsize-1];
		pii bval = distlist[dsize-2];
		pii cval = distlist[dsize-3];
		if (aval.first == bval.first && aval.first == cval.first) {
			int pos = dsize-1;
			ll p = 0;
			ll q = 0;
			while (pos >= 0 && distlist[pos].first == aval.first) {
				p += distlist[pos].second;
				q += pow(distlist[pos].second, 2);
				pos--;
			}
			ll count = (p*p-q)/2;
			ll num = 2*aval.first;
			num *= aval.first;
			upd(num, count);
		}
		else if (aval.first == bval.first) {
			int pos = dsize-3;
			ll p = 0;
			while (pos >= 0 && distlist[pos].first == cval.first) {
				p += distlist[pos].second;
				pos--;
			}
			ll count = p*(bval.second+aval.second);
			ll num = aval.first+cval.first;
			num *= bval.first;
			upd(num, count);
		}
		else if (bval.first == cval.first) {
			int pos = dsize-2;
			ll p = 0;
			ll q = 0;
			while (pos >= 0 && distlist[pos].first == bval.first) {
				p += distlist[pos].second;
				q += pow(distlist[pos].second, 2);
				pos--;
			}
			ll count = (p*p-q)/2;
			ll num = 2*bval.first;
			num *= aval.first;
			upd(num, count);
		}
		else {
			int pos = dsize-3;
			ll p = 0;
			while (pos >= 0 && distlist[pos].first == cval.first) {
				p += distlist[pos].second;
				pos--;
			}
			ll count = p*bval.second;
			ll num = bval.first+cval.first;
			num *= aval.first;
			upd(num, count);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n;
	for (int i = 0; i < n-1; i++) {
		int x, y; cin >> x >> y;
		x--; y--;
		edges[x].push_back(y);
		edges[y].push_back(x);
	}
	dfs(0);
	p_dist[0] = pii(0, 1);
	dfs2(0);
	cout << best_h << " " << num_ways << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12072 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 12076 KB Output is correct
6 Correct 8 ms 11988 KB Output is correct
7 Correct 8 ms 12068 KB Output is correct
8 Correct 7 ms 12000 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12072 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 8 ms 11988 KB Output is correct
19 Correct 7 ms 12116 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11976 KB Output is correct
22 Correct 6 ms 12072 KB Output is correct
23 Correct 9 ms 12072 KB Output is correct
24 Correct 6 ms 12076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12072 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 12076 KB Output is correct
6 Correct 8 ms 11988 KB Output is correct
7 Correct 8 ms 12068 KB Output is correct
8 Correct 7 ms 12000 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12072 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 8 ms 11988 KB Output is correct
19 Correct 7 ms 12116 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11976 KB Output is correct
22 Correct 6 ms 12072 KB Output is correct
23 Correct 9 ms 12072 KB Output is correct
24 Correct 6 ms 12076 KB Output is correct
25 Correct 9 ms 12848 KB Output is correct
26 Correct 9 ms 13140 KB Output is correct
27 Correct 11 ms 13316 KB Output is correct
28 Correct 9 ms 13308 KB Output is correct
29 Correct 9 ms 13168 KB Output is correct
30 Correct 10 ms 13232 KB Output is correct
31 Correct 9 ms 13236 KB Output is correct
32 Correct 9 ms 13012 KB Output is correct
33 Correct 11 ms 13140 KB Output is correct
34 Correct 9 ms 13268 KB Output is correct
35 Correct 9 ms 13276 KB Output is correct
36 Correct 10 ms 13108 KB Output is correct
37 Correct 9 ms 13396 KB Output is correct
38 Correct 10 ms 14176 KB Output is correct
39 Correct 9 ms 12848 KB Output is correct
40 Correct 9 ms 12628 KB Output is correct
41 Correct 9 ms 12400 KB Output is correct
42 Correct 9 ms 12336 KB Output is correct
43 Correct 9 ms 12344 KB Output is correct
44 Correct 8 ms 12340 KB Output is correct
45 Correct 8 ms 12244 KB Output is correct
46 Correct 8 ms 12212 KB Output is correct
47 Correct 9 ms 12364 KB Output is correct
48 Correct 9 ms 12412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 7 ms 12072 KB Output is correct
3 Correct 6 ms 11988 KB Output is correct
4 Correct 6 ms 11988 KB Output is correct
5 Correct 6 ms 12076 KB Output is correct
6 Correct 8 ms 11988 KB Output is correct
7 Correct 8 ms 12068 KB Output is correct
8 Correct 7 ms 12000 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 7 ms 11988 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 6 ms 11988 KB Output is correct
13 Correct 6 ms 11988 KB Output is correct
14 Correct 7 ms 12072 KB Output is correct
15 Correct 6 ms 12060 KB Output is correct
16 Correct 7 ms 11988 KB Output is correct
17 Correct 7 ms 11988 KB Output is correct
18 Correct 8 ms 11988 KB Output is correct
19 Correct 7 ms 12116 KB Output is correct
20 Correct 6 ms 11988 KB Output is correct
21 Correct 6 ms 11976 KB Output is correct
22 Correct 6 ms 12072 KB Output is correct
23 Correct 9 ms 12072 KB Output is correct
24 Correct 6 ms 12076 KB Output is correct
25 Correct 9 ms 12848 KB Output is correct
26 Correct 9 ms 13140 KB Output is correct
27 Correct 11 ms 13316 KB Output is correct
28 Correct 9 ms 13308 KB Output is correct
29 Correct 9 ms 13168 KB Output is correct
30 Correct 10 ms 13232 KB Output is correct
31 Correct 9 ms 13236 KB Output is correct
32 Correct 9 ms 13012 KB Output is correct
33 Correct 11 ms 13140 KB Output is correct
34 Correct 9 ms 13268 KB Output is correct
35 Correct 9 ms 13276 KB Output is correct
36 Correct 10 ms 13108 KB Output is correct
37 Correct 9 ms 13396 KB Output is correct
38 Correct 10 ms 14176 KB Output is correct
39 Correct 9 ms 12848 KB Output is correct
40 Correct 9 ms 12628 KB Output is correct
41 Correct 9 ms 12400 KB Output is correct
42 Correct 9 ms 12336 KB Output is correct
43 Correct 9 ms 12344 KB Output is correct
44 Correct 8 ms 12340 KB Output is correct
45 Correct 8 ms 12244 KB Output is correct
46 Correct 8 ms 12212 KB Output is correct
47 Correct 9 ms 12364 KB Output is correct
48 Correct 9 ms 12412 KB Output is correct
49 Correct 599 ms 112428 KB Output is correct
50 Correct 620 ms 125640 KB Output is correct
51 Correct 630 ms 136772 KB Output is correct
52 Correct 593 ms 97128 KB Output is correct
53 Correct 550 ms 131696 KB Output is correct
54 Correct 627 ms 146044 KB Output is correct
55 Correct 561 ms 110708 KB Output is correct
56 Correct 553 ms 126040 KB Output is correct
57 Correct 590 ms 145300 KB Output is correct
58 Correct 592 ms 130512 KB Output is correct
59 Correct 636 ms 130224 KB Output is correct
60 Correct 574 ms 123156 KB Output is correct
61 Correct 821 ms 229692 KB Output is correct
62 Correct 845 ms 195588 KB Output is correct
63 Correct 801 ms 96496 KB Output is correct
64 Correct 822 ms 72960 KB Output is correct
65 Correct 773 ms 57832 KB Output is correct
66 Correct 752 ms 49888 KB Output is correct
67 Correct 727 ms 45540 KB Output is correct
68 Correct 726 ms 43716 KB Output is correct
69 Correct 696 ms 43216 KB Output is correct
70 Correct 689 ms 42716 KB Output is correct
71 Correct 659 ms 42672 KB Output is correct
72 Correct 696 ms 42800 KB Output is correct
73 Correct 708 ms 42956 KB Output is correct
74 Correct 649 ms 43052 KB Output is correct
75 Correct 649 ms 43460 KB Output is correct
76 Correct 621 ms 43876 KB Output is correct
77 Correct 531 ms 45540 KB Output is correct
78 Correct 329 ms 48732 KB Output is correct