답안 #52727

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
52727 2018-06-26T13:14:58 Z Nicksechko Hard route (IZhO17_road) C++14
100 / 100
1210 ms 276396 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() {
//	freopen("road.in", "r", stdin);
//	freopen("road.out", "w", stdout);
	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:152:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
road.cpp:155:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27768 KB Output is correct
2 Correct 24 ms 27888 KB Output is correct
3 Correct 21 ms 27888 KB Output is correct
4 Correct 24 ms 27888 KB Output is correct
5 Correct 22 ms 27888 KB Output is correct
6 Correct 26 ms 27888 KB Output is correct
7 Correct 31 ms 28076 KB Output is correct
8 Correct 31 ms 28076 KB Output is correct
9 Correct 30 ms 28076 KB Output is correct
10 Correct 30 ms 28076 KB Output is correct
11 Correct 25 ms 28096 KB Output is correct
12 Correct 26 ms 28140 KB Output is correct
13 Correct 25 ms 28156 KB Output is correct
14 Correct 26 ms 28164 KB Output is correct
15 Correct 26 ms 28164 KB Output is correct
16 Correct 26 ms 28172 KB Output is correct
17 Correct 31 ms 28172 KB Output is correct
18 Correct 27 ms 28272 KB Output is correct
19 Correct 25 ms 28272 KB Output is correct
20 Correct 24 ms 28272 KB Output is correct
21 Correct 30 ms 28272 KB Output is correct
22 Correct 30 ms 28272 KB Output is correct
23 Correct 29 ms 28272 KB Output is correct
24 Correct 29 ms 28272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27768 KB Output is correct
2 Correct 24 ms 27888 KB Output is correct
3 Correct 21 ms 27888 KB Output is correct
4 Correct 24 ms 27888 KB Output is correct
5 Correct 22 ms 27888 KB Output is correct
6 Correct 26 ms 27888 KB Output is correct
7 Correct 31 ms 28076 KB Output is correct
8 Correct 31 ms 28076 KB Output is correct
9 Correct 30 ms 28076 KB Output is correct
10 Correct 30 ms 28076 KB Output is correct
11 Correct 25 ms 28096 KB Output is correct
12 Correct 26 ms 28140 KB Output is correct
13 Correct 25 ms 28156 KB Output is correct
14 Correct 26 ms 28164 KB Output is correct
15 Correct 26 ms 28164 KB Output is correct
16 Correct 26 ms 28172 KB Output is correct
17 Correct 31 ms 28172 KB Output is correct
18 Correct 27 ms 28272 KB Output is correct
19 Correct 25 ms 28272 KB Output is correct
20 Correct 24 ms 28272 KB Output is correct
21 Correct 30 ms 28272 KB Output is correct
22 Correct 30 ms 28272 KB Output is correct
23 Correct 29 ms 28272 KB Output is correct
24 Correct 29 ms 28272 KB Output is correct
25 Correct 34 ms 28844 KB Output is correct
26 Correct 38 ms 28896 KB Output is correct
27 Correct 35 ms 28896 KB Output is correct
28 Correct 33 ms 29160 KB Output is correct
29 Correct 38 ms 29160 KB Output is correct
30 Correct 30 ms 29160 KB Output is correct
31 Correct 30 ms 29204 KB Output is correct
32 Correct 37 ms 29384 KB Output is correct
33 Correct 31 ms 29384 KB Output is correct
34 Correct 31 ms 29540 KB Output is correct
35 Correct 29 ms 29636 KB Output is correct
36 Correct 29 ms 29636 KB Output is correct
37 Correct 26 ms 29636 KB Output is correct
38 Correct 24 ms 29636 KB Output is correct
39 Correct 30 ms 29640 KB Output is correct
40 Correct 35 ms 29640 KB Output is correct
41 Correct 30 ms 29640 KB Output is correct
42 Correct 34 ms 29640 KB Output is correct
43 Correct 29 ms 29640 KB Output is correct
44 Correct 32 ms 29660 KB Output is correct
45 Correct 35 ms 29660 KB Output is correct
46 Correct 28 ms 29660 KB Output is correct
47 Correct 28 ms 29804 KB Output is correct
48 Correct 29 ms 29856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 27768 KB Output is correct
2 Correct 24 ms 27888 KB Output is correct
3 Correct 21 ms 27888 KB Output is correct
4 Correct 24 ms 27888 KB Output is correct
5 Correct 22 ms 27888 KB Output is correct
6 Correct 26 ms 27888 KB Output is correct
7 Correct 31 ms 28076 KB Output is correct
8 Correct 31 ms 28076 KB Output is correct
9 Correct 30 ms 28076 KB Output is correct
10 Correct 30 ms 28076 KB Output is correct
11 Correct 25 ms 28096 KB Output is correct
12 Correct 26 ms 28140 KB Output is correct
13 Correct 25 ms 28156 KB Output is correct
14 Correct 26 ms 28164 KB Output is correct
15 Correct 26 ms 28164 KB Output is correct
16 Correct 26 ms 28172 KB Output is correct
17 Correct 31 ms 28172 KB Output is correct
18 Correct 27 ms 28272 KB Output is correct
19 Correct 25 ms 28272 KB Output is correct
20 Correct 24 ms 28272 KB Output is correct
21 Correct 30 ms 28272 KB Output is correct
22 Correct 30 ms 28272 KB Output is correct
23 Correct 29 ms 28272 KB Output is correct
24 Correct 29 ms 28272 KB Output is correct
25 Correct 34 ms 28844 KB Output is correct
26 Correct 38 ms 28896 KB Output is correct
27 Correct 35 ms 28896 KB Output is correct
28 Correct 33 ms 29160 KB Output is correct
29 Correct 38 ms 29160 KB Output is correct
30 Correct 30 ms 29160 KB Output is correct
31 Correct 30 ms 29204 KB Output is correct
32 Correct 37 ms 29384 KB Output is correct
33 Correct 31 ms 29384 KB Output is correct
34 Correct 31 ms 29540 KB Output is correct
35 Correct 29 ms 29636 KB Output is correct
36 Correct 29 ms 29636 KB Output is correct
37 Correct 26 ms 29636 KB Output is correct
38 Correct 24 ms 29636 KB Output is correct
39 Correct 30 ms 29640 KB Output is correct
40 Correct 35 ms 29640 KB Output is correct
41 Correct 30 ms 29640 KB Output is correct
42 Correct 34 ms 29640 KB Output is correct
43 Correct 29 ms 29640 KB Output is correct
44 Correct 32 ms 29660 KB Output is correct
45 Correct 35 ms 29660 KB Output is correct
46 Correct 28 ms 29660 KB Output is correct
47 Correct 28 ms 29804 KB Output is correct
48 Correct 29 ms 29856 KB Output is correct
49 Correct 821 ms 100212 KB Output is correct
50 Correct 953 ms 113340 KB Output is correct
51 Correct 831 ms 125664 KB Output is correct
52 Correct 839 ms 125664 KB Output is correct
53 Correct 684 ms 139188 KB Output is correct
54 Correct 749 ms 143148 KB Output is correct
55 Correct 718 ms 143148 KB Output is correct
56 Correct 716 ms 143148 KB Output is correct
57 Correct 806 ms 172060 KB Output is correct
58 Correct 770 ms 172400 KB Output is correct
59 Correct 826 ms 179428 KB Output is correct
60 Correct 766 ms 183448 KB Output is correct
61 Correct 507 ms 183448 KB Output is correct
62 Correct 468 ms 183448 KB Output is correct
63 Correct 1158 ms 192408 KB Output is correct
64 Correct 1210 ms 192408 KB Output is correct
65 Correct 1087 ms 192408 KB Output is correct
66 Correct 1156 ms 192568 KB Output is correct
67 Correct 1126 ms 197620 KB Output is correct
68 Correct 1165 ms 204152 KB Output is correct
69 Correct 1086 ms 210956 KB Output is correct
70 Correct 1039 ms 217696 KB Output is correct
71 Correct 1043 ms 224612 KB Output is correct
72 Correct 1098 ms 231892 KB Output is correct
73 Correct 1127 ms 239208 KB Output is correct
74 Correct 1029 ms 246252 KB Output is correct
75 Correct 969 ms 253428 KB Output is correct
76 Correct 938 ms 260632 KB Output is correct
77 Correct 839 ms 268164 KB Output is correct
78 Correct 573 ms 276396 KB Output is correct