Submission #35061

# Submission time Handle Problem Language Result Execution time Memory
35061 2017-11-17T20:51:46 Z model_code Hard route (IZhO17_road) C++11
100 / 100
1239 ms 118756 KB
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <queue>

#define mp make_pair
#define pb push_back


typedef long long ll;
typedef long double ld;

using namespace std;
const int MAXN = 1000100;
int was[MAXN];
pair<ll, ll> dw[MAXN];
pair<ll, ll> up[MAXN];
vector<int> eds[MAXN];
int n;

void mrg(pair<ll, ll> &a, pair<ll, ll> b) {
	if (a.first < b.first)
		a = b;
	else if (a.first == b.first)
		a.second += b.second;
}

void dfs0(int v) {
	was[v] = 1;
	dw[v] = make_pair(0, 1);
	for (int u: eds[v]) {
		if (was[u])
			continue;
		dfs0(u);
		pair<ll, ll> tmp = dw[u];
		tmp.first += 1;
		mrg(dw[v], tmp);
	}
}

void dfs1(int v, pair<ll, ll> now) {
	up[v] = now;
	pair<ll, ll> mn[2];
	mn[0] = now;
	mn[1] = make_pair(0, 0);
	was[v] = 1;
	for (int u: eds[v]) {
		if (was[u])
			continue;
		pair<ll, ll> tmp = dw[u];
		tmp.first += 1;
		if (mn[0].first == tmp.first) {
			mn[0].second += tmp.second;
		}
		else {
			mrg(mn[1], tmp);
			if (mn[1].first > mn[0].first)
				swap(mn[0], mn[1]);
		}
	}
	for (int u: eds[v]) {
		if (was[u])
			continue;
		if (dw[u].first + 1 == mn[0].first && dw[u].second == mn[0].second)
			dfs1(u, make_pair(mn[1].first + 1, mn[1].second));
		else if (dw[u].first + 1 == mn[0].first)
			dfs1(u, make_pair(mn[0].first + 1, mn[0].second - dw[u].second));
		else
			dfs1(u, make_pair(mn[0].first + 1, mn[0].second));
	}
}

pair<ll, ll> ans;

void dfs2(int v) {
	tuple<ll, ll, ll, ll> mn[3];
	mn[0] = make_tuple(up[v].first, up[v].second, 0, 0);
	mn[1] = make_tuple(0, 0, 0, 0);
	mn[2] = make_tuple(0, 0, 0, 0);
	was[v] = 1;
	for (int u: eds[v]) {
		if (was[u])
			continue;
		dfs2(u);
		int fl = 0;
		for (int i = 0; i < 3; ++i) {
			if (get<0>(mn[i]) == dw[u].first + 1) {
				get<3>(mn[i]) += get<2>(mn[i]) * dw[u].second;
				get<2>(mn[i]) += get<1>(mn[i]) * dw[u].second;
				get<1>(mn[i]) += dw[u].second;
				fl = 1;
				break;
			}
		}
		if (!fl) {
			if (get<0>(mn[2]) < dw[u].first + 1) {
				mn[2] = make_tuple(dw[u].first + 1, dw[u].second, 0, 0);
				if (mn[2] > mn[1])
					swap(mn[2], mn[1]);
				if (mn[1] > mn[0])
					swap(mn[1], mn[0]);
			}
		}
	}
	if (get<3>(mn[0])) {
		ll a = get<0>(mn[0]) * (get<0>(mn[0]) + get<0>(mn[0]));
		ll c = get<2>(mn[0]);
		if (a > ans.first && c)
			ans = make_pair(a, c);
		else if (a == ans.first)
			ans.second += c;
	}
	else if (get<2>(mn[0])) {
		ll a = get<0>(mn[0]) * (get<0>(mn[0]) + get<0>(mn[1]));
		ll c = get<1>(mn[0]) * get<1>(mn[1]);
		if (a > ans.first && c)
			ans = make_pair(a, c);
		else if (a == ans.first)
			ans.second += c;
	}
	else if (get<2>(mn[1])) {
		ll a = get<0>(mn[0]) * (get<0>(mn[1]) + get<0>(mn[1]));
		ll c = get<2>(mn[1]);
		if (a > ans.first && c)
			ans = make_pair(a, c);
		else if (a == ans.first)
			ans.second += c;
	}
	else {
		ll a = get<0>(mn[0]) * (get<0>(mn[1]) + get<0>(mn[2]));
		ll c = get<1>(mn[1]) * get<1>(mn[2]);
		if (a > ans.first && c)
			ans = make_pair(a, c);
		else if (a == ans.first)
			ans.second += c;
	}
}


int main() {
	scanf("%d", &n);
	for (int i = 0; i < n - 1; ++i) {
		int a, b;
		scanf("%d%d", &a, &b);
		--a, --b;
		eds[a].push_back(b);
		eds[b].push_back(a);
	}
	int fl = 0;
	for (int i = 0; i < n; ++i)
		if (eds[i].size() > 2)
			fl = 1;
	if (!fl) {
		cout << 0 << " " << 1 << "\n";
		return 0;
	}
	int st = 0;
	for (int i = 0; i < n; ++i)
		if (eds[i].size() != 1) {
			st = i;
			break;
		}
	dfs0(st);
	memset(was, 0, sizeof(was));
	dfs1(st, make_pair(0, 0));
	memset(was, 0, sizeof(was));
	dfs2(st);
	cout << ans.first << " " << ans.second << "\n";
	return 0;
}


Compilation message

road.cpp: In function 'int main()':
road.cpp:150:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
road.cpp:153:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
                        ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 60616 KB Output is correct
2 Correct 9 ms 60616 KB Output is correct
3 Correct 6 ms 60616 KB Output is correct
4 Correct 6 ms 60616 KB Output is correct
5 Correct 3 ms 60616 KB Output is correct
6 Correct 3 ms 60616 KB Output is correct
7 Correct 13 ms 60616 KB Output is correct
8 Correct 3 ms 60616 KB Output is correct
9 Correct 3 ms 60616 KB Output is correct
10 Correct 9 ms 60616 KB Output is correct
11 Correct 6 ms 60616 KB Output is correct
12 Correct 3 ms 60616 KB Output is correct
13 Correct 6 ms 60616 KB Output is correct
14 Correct 13 ms 60616 KB Output is correct
15 Correct 3 ms 60616 KB Output is correct
16 Correct 3 ms 60616 KB Output is correct
17 Correct 3 ms 60616 KB Output is correct
18 Correct 9 ms 60616 KB Output is correct
19 Correct 9 ms 60616 KB Output is correct
20 Correct 6 ms 60616 KB Output is correct
21 Correct 3 ms 60616 KB Output is correct
22 Correct 3 ms 60616 KB Output is correct
23 Correct 6 ms 60616 KB Output is correct
24 Correct 6 ms 60616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 60616 KB Output is correct
2 Correct 9 ms 60616 KB Output is correct
3 Correct 6 ms 60616 KB Output is correct
4 Correct 6 ms 60616 KB Output is correct
5 Correct 3 ms 60616 KB Output is correct
6 Correct 3 ms 60616 KB Output is correct
7 Correct 13 ms 60616 KB Output is correct
8 Correct 3 ms 60616 KB Output is correct
9 Correct 3 ms 60616 KB Output is correct
10 Correct 9 ms 60616 KB Output is correct
11 Correct 6 ms 60616 KB Output is correct
12 Correct 3 ms 60616 KB Output is correct
13 Correct 6 ms 60616 KB Output is correct
14 Correct 13 ms 60616 KB Output is correct
15 Correct 3 ms 60616 KB Output is correct
16 Correct 3 ms 60616 KB Output is correct
17 Correct 3 ms 60616 KB Output is correct
18 Correct 9 ms 60616 KB Output is correct
19 Correct 9 ms 60616 KB Output is correct
20 Correct 6 ms 60616 KB Output is correct
21 Correct 3 ms 60616 KB Output is correct
22 Correct 3 ms 60616 KB Output is correct
23 Correct 6 ms 60616 KB Output is correct
24 Correct 6 ms 60616 KB Output is correct
25 Correct 13 ms 60840 KB Output is correct
26 Correct 9 ms 60964 KB Output is correct
27 Correct 9 ms 60820 KB Output is correct
28 Correct 13 ms 61020 KB Output is correct
29 Correct 13 ms 60912 KB Output is correct
30 Correct 16 ms 60964 KB Output is correct
31 Correct 13 ms 60964 KB Output is correct
32 Correct 6 ms 60876 KB Output is correct
33 Correct 13 ms 60956 KB Output is correct
34 Correct 9 ms 61036 KB Output is correct
35 Correct 9 ms 61016 KB Output is correct
36 Correct 9 ms 60976 KB Output is correct
37 Correct 9 ms 60748 KB Output is correct
38 Correct 6 ms 60748 KB Output is correct
39 Correct 13 ms 60880 KB Output is correct
40 Correct 6 ms 60748 KB Output is correct
41 Correct 6 ms 60748 KB Output is correct
42 Correct 6 ms 60748 KB Output is correct
43 Correct 13 ms 60748 KB Output is correct
44 Correct 9 ms 60748 KB Output is correct
45 Correct 9 ms 60748 KB Output is correct
46 Correct 9 ms 60748 KB Output is correct
47 Correct 9 ms 60748 KB Output is correct
48 Correct 13 ms 60880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 60616 KB Output is correct
2 Correct 9 ms 60616 KB Output is correct
3 Correct 6 ms 60616 KB Output is correct
4 Correct 6 ms 60616 KB Output is correct
5 Correct 3 ms 60616 KB Output is correct
6 Correct 3 ms 60616 KB Output is correct
7 Correct 13 ms 60616 KB Output is correct
8 Correct 3 ms 60616 KB Output is correct
9 Correct 3 ms 60616 KB Output is correct
10 Correct 9 ms 60616 KB Output is correct
11 Correct 6 ms 60616 KB Output is correct
12 Correct 3 ms 60616 KB Output is correct
13 Correct 6 ms 60616 KB Output is correct
14 Correct 13 ms 60616 KB Output is correct
15 Correct 3 ms 60616 KB Output is correct
16 Correct 3 ms 60616 KB Output is correct
17 Correct 3 ms 60616 KB Output is correct
18 Correct 9 ms 60616 KB Output is correct
19 Correct 9 ms 60616 KB Output is correct
20 Correct 6 ms 60616 KB Output is correct
21 Correct 3 ms 60616 KB Output is correct
22 Correct 3 ms 60616 KB Output is correct
23 Correct 6 ms 60616 KB Output is correct
24 Correct 6 ms 60616 KB Output is correct
25 Correct 13 ms 60840 KB Output is correct
26 Correct 9 ms 60964 KB Output is correct
27 Correct 9 ms 60820 KB Output is correct
28 Correct 13 ms 61020 KB Output is correct
29 Correct 13 ms 60912 KB Output is correct
30 Correct 16 ms 60964 KB Output is correct
31 Correct 13 ms 60964 KB Output is correct
32 Correct 6 ms 60876 KB Output is correct
33 Correct 13 ms 60956 KB Output is correct
34 Correct 9 ms 61036 KB Output is correct
35 Correct 9 ms 61016 KB Output is correct
36 Correct 9 ms 60976 KB Output is correct
37 Correct 9 ms 60748 KB Output is correct
38 Correct 6 ms 60748 KB Output is correct
39 Correct 13 ms 60880 KB Output is correct
40 Correct 6 ms 60748 KB Output is correct
41 Correct 6 ms 60748 KB Output is correct
42 Correct 6 ms 60748 KB Output is correct
43 Correct 13 ms 60748 KB Output is correct
44 Correct 9 ms 60748 KB Output is correct
45 Correct 9 ms 60748 KB Output is correct
46 Correct 9 ms 60748 KB Output is correct
47 Correct 9 ms 60748 KB Output is correct
48 Correct 13 ms 60880 KB Output is correct
49 Correct 893 ms 105412 KB Output is correct
50 Correct 906 ms 110860 KB Output is correct
51 Correct 889 ms 115528 KB Output is correct
52 Correct 906 ms 98976 KB Output is correct
53 Correct 696 ms 114892 KB Output is correct
54 Correct 723 ms 111868 KB Output is correct
55 Correct 666 ms 103204 KB Output is correct
56 Correct 683 ms 95664 KB Output is correct
57 Correct 809 ms 118756 KB Output is correct
58 Correct 833 ms 112580 KB Output is correct
59 Correct 803 ms 112484 KB Output is correct
60 Correct 823 ms 109596 KB Output is correct
61 Correct 459 ms 76192 KB Output is correct
62 Correct 459 ms 76192 KB Output is correct
63 Correct 1203 ms 98456 KB Output is correct
64 Correct 1239 ms 88672 KB Output is correct
65 Correct 1239 ms 82352 KB Output is correct
66 Correct 1223 ms 79076 KB Output is correct
67 Correct 1179 ms 77268 KB Output is correct
68 Correct 1159 ms 76556 KB Output is correct
69 Correct 1209 ms 76360 KB Output is correct
70 Correct 1189 ms 76192 KB Output is correct
71 Correct 1163 ms 76192 KB Output is correct
72 Correct 1196 ms 76324 KB Output is correct
73 Correct 1193 ms 76352 KB Output is correct
74 Correct 1159 ms 76328 KB Output is correct
75 Correct 1139 ms 76452 KB Output is correct
76 Correct 1079 ms 76712 KB Output is correct
77 Correct 946 ms 77228 KB Output is correct
78 Correct 559 ms 78276 KB Output is correct