답안 #759833

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
759833 2023-06-16T22:24:57 Z NK_ Hard route (IZhO17_road) C++17
100 / 100
1075 ms 173436 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define f first
#define s second
#define mp make_pair

#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())

using pi = pair<int, int>;
using ll = long long;

template<class T> using V = vector<T>;
template<class T> using mset = multiset<T>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;

	V<V<int>> adj(N);
	V<int> deg(N);

	int T = 0;
	for(int i = 0; i < N-1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
		deg[u]++, deg[v]++;
	}	

	int R = -1; for(int i = N-1; i >= 0; i--) {
		if (deg[i] > 1) R = i;
		else T++;
	}

	if (R == -1) {
		cout << 0 << " " << 1 << nl;
		return 0;
	}


	ll ans = 0, ways = T * (T - 1) / 2;
	V<pi> dp(N); V<map<int, int>> chd(N); 
	function<void(int, int)> gen = [&](int u, int p) {
		for(auto v : adj[u]) if (v != p) {
			gen(v, u); chd[u][dp[v].f] += dp[v].s;
		};
		
		if (sz(chd[u])) { dp[u] = *rbegin(chd[u]); dp[u].f++; }
		else dp[u] = mp(0, 1);	
	};

	gen(R, -1);

	auto upd = [&](int u, int k, int v) {
		chd[u][k] += v;
		if (chd[u][k] == 0) chd[u].erase(k);

		if (sz(chd[u])) { dp[u] = *rbegin(chd[u]); dp[u].f++; }
		else dp[u] = mp(0, 1);	
	};	

	function<void(int, int)> dfs = [&](int u, int p) {
		// cout << "NODE: " << u << " " << p << endl;
		V<pi> chd = {};
		for(auto v : adj[u]) chd.pb(dp[v]);

		sort(rbegin(chd), rend(chd));


		// if (p != -1 && size(chd) >= 2) {
		// 	// C is from above
		// 	int a = chd[0].f, b = chd[1].f;
		// 	ll R = 0;
		// 	if (a == b) {
		// 		ll sum = 0; for(auto x : chd) if (x.f == a) sum += x.s;
		// 		for(auto x : chd) R += (sum -= x.s) * x.s;
		// 	} else {
		// 		ll sum = 0; for(auto x : chd) if (x.f == b) sum += x.s;
		// 		R += sum * 1LL * chd[0].s;
		// 	}

		// 	int c = dp[p].f;

		// 	ll H = c * (a + b);
		// }

		// // All 3 are children
		// if (size(chd) >= 3) {
		// 	int a = chd[0].f, b = chd[1].f, c = chd[2].f;	

		// 	if (a == b && b == c) {
		// 		ll sum = 0, cnt = 0; for(auto x : chd) if (x.f == a) sum += x.s, cnt++;
		// 		for(auto x : chd) R += (sum -= x.s) * x.s * (cnt - 2);
		// 	}


		// }


		if (size(chd) >= 3) {
			int a, at; tie(a, at) = chd[0];
			int b, bt; tie(b, bt) = chd[1];
			int c, ct; tie(c, ct) = chd[2];

			ll R = 0;
			if (a == b && b == c) {
				ll sum = 0, cnt = 0; for(auto x : chd) if (x.f == a) sum += x.s, cnt++;
				for(auto x : chd) if (x.f == a) R += (sum -= x.s) * 1LL * x.s;
			}

			if (a != b && b == c) {
				ll sum = 0, cnt = 0; for(auto x : chd) if (x.f == b) sum += x.s, cnt++;
				// cout << sum << " - " << cnt << nl;
				for(auto x : chd) if (x.f == b) R += (sum -= x.s) * 1LL * x.s;
			}

			if (a == b && b != c) {
				ll sum = 0, cnt = 0; for(auto x : chd) if (x.f == c) sum += x.s, cnt++;
				R += sum * 1LL * (at + bt);
			}

			if (a != b && b != c) {
				ll sum = 0, cnt = 0; for(auto x : chd) if (x.f == c) sum += x.s, cnt++;
				R += bt * 1LL * sum;
			}

			++a, ++b, ++c;

			// cout << a << " - " << b << " - " << c << endl;
			// cout << at << " - " << bt << " - " << ct << endl;

			ll H = (c + b) * 1LL * a;
			// cout << "(H, R) -> (" << H << ", " << R << ") " << endl;

			if (ans < H) ans = H, ways = 0;
			if (ans == H) ways += R;
		}

		for(auto v : adj[u]) if (v != p) {
			upd(u, dp[v].f, -dp[v].s);
			upd(v, dp[u].f, dp[u].s);

			dfs(v, u);

			upd(v, dp[u].f, -dp[u].s);
			upd(u, dp[v].f, dp[v].s);
		}
	};

	dfs(R, -1);

	cout << ans << " " << ways << nl;

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 1364 KB Output is correct
26 Correct 4 ms 1432 KB Output is correct
27 Correct 4 ms 1296 KB Output is correct
28 Correct 4 ms 1484 KB Output is correct
29 Correct 4 ms 1492 KB Output is correct
30 Correct 4 ms 1552 KB Output is correct
31 Correct 3 ms 1492 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1492 KB Output is correct
34 Correct 3 ms 1620 KB Output is correct
35 Correct 4 ms 1492 KB Output is correct
36 Correct 3 ms 1492 KB Output is correct
37 Correct 4 ms 1620 KB Output is correct
38 Correct 5 ms 2004 KB Output is correct
39 Correct 3 ms 1364 KB Output is correct
40 Correct 3 ms 1228 KB Output is correct
41 Correct 3 ms 1236 KB Output is correct
42 Correct 3 ms 1188 KB Output is correct
43 Correct 4 ms 1100 KB Output is correct
44 Correct 3 ms 1108 KB Output is correct
45 Correct 3 ms 972 KB Output is correct
46 Correct 3 ms 980 KB Output is correct
47 Correct 3 ms 1108 KB Output is correct
48 Correct 2 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 316 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 3 ms 1364 KB Output is correct
26 Correct 4 ms 1432 KB Output is correct
27 Correct 4 ms 1296 KB Output is correct
28 Correct 4 ms 1484 KB Output is correct
29 Correct 4 ms 1492 KB Output is correct
30 Correct 4 ms 1552 KB Output is correct
31 Correct 3 ms 1492 KB Output is correct
32 Correct 3 ms 1364 KB Output is correct
33 Correct 3 ms 1492 KB Output is correct
34 Correct 3 ms 1620 KB Output is correct
35 Correct 4 ms 1492 KB Output is correct
36 Correct 3 ms 1492 KB Output is correct
37 Correct 4 ms 1620 KB Output is correct
38 Correct 5 ms 2004 KB Output is correct
39 Correct 3 ms 1364 KB Output is correct
40 Correct 3 ms 1228 KB Output is correct
41 Correct 3 ms 1236 KB Output is correct
42 Correct 3 ms 1188 KB Output is correct
43 Correct 4 ms 1100 KB Output is correct
44 Correct 3 ms 1108 KB Output is correct
45 Correct 3 ms 972 KB Output is correct
46 Correct 3 ms 980 KB Output is correct
47 Correct 3 ms 1108 KB Output is correct
48 Correct 2 ms 980 KB Output is correct
49 Correct 795 ms 117440 KB Output is correct
50 Correct 792 ms 123340 KB Output is correct
51 Correct 744 ms 128504 KB Output is correct
52 Correct 737 ms 110280 KB Output is correct
53 Correct 675 ms 134320 KB Output is correct
54 Correct 724 ms 130632 KB Output is correct
55 Correct 714 ms 120232 KB Output is correct
56 Correct 681 ms 111240 KB Output is correct
57 Correct 739 ms 134720 KB Output is correct
58 Correct 778 ms 127900 KB Output is correct
59 Correct 836 ms 127696 KB Output is correct
60 Correct 756 ms 124632 KB Output is correct
61 Correct 1075 ms 173436 KB Output is correct
62 Correct 977 ms 157764 KB Output is correct
63 Correct 995 ms 112460 KB Output is correct
64 Correct 983 ms 101564 KB Output is correct
65 Correct 955 ms 94600 KB Output is correct
66 Correct 913 ms 90988 KB Output is correct
67 Correct 853 ms 88984 KB Output is correct
68 Correct 860 ms 88200 KB Output is correct
69 Correct 910 ms 87796 KB Output is correct
70 Correct 863 ms 87544 KB Output is correct
71 Correct 871 ms 87184 KB Output is correct
72 Correct 861 ms 87644 KB Output is correct
73 Correct 926 ms 87408 KB Output is correct
74 Correct 885 ms 87136 KB Output is correct
75 Correct 785 ms 86256 KB Output is correct
76 Correct 804 ms 84428 KB Output is correct
77 Correct 655 ms 78848 KB Output is correct
78 Correct 415 ms 70484 KB Output is correct