답안 #727091

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
727091 2023-04-20T03:40:30 Z ono_de206 Hard route (IZhO17_road) C++14
100 / 100
683 ms 131612 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define in insert
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second

#define int long long
 
typedef long long ll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T, typename U>
ostream & operator << (ostream &out, const pair<T, U> &c) {
	out << c.first << ' ' << c.second;
    return out;
}

template<typename T>
ostream & operator << (ostream &out, vector<T> &v) {
	const int sz = v.size();
	for (int i = 0; i < sz; i++) {
		if (i) out << ' ';
		out << v[i];
	}
    return out;
}

template<typename T>
istream & operator >> (istream &in, vector<T> &v) {
	for (T &x : v) in >> x;
    return in;
}


template<typename T>
void mxx(T &a, T b){if(b > a) a = b;}
template<typename T>
void mnn(T &a, T b){if(b < a) a = b;}

const int mxn = 5e5 + 10;
int n, ans1, ans2;
vector<int> g[mxn];
pair<int, int> d[mxn];

void init(int to, int fr) {
	auto it = find(all(g[to]), fr);
	if(it != g[to].end()) {
		g[to].erase(it);
	}
	d[to] = {0, 1};
	for(int x : g[to]) {
		init(x, to);
		if(d[x].ff + 1 > d[to].ff) {
			d[to] = d[x];
			d[to].ff++;
		} else if(d[x].ff + 1 == d[to].ff) {
			d[to].ss += d[x].ss;
		}
	}
}

void dfs(int to, int mx, int cnt) {
	vector<pair<int, int>> v;
	if(to != 1 && cnt) {
		v.pb({mx, cnt});
	}
	for(int x : g[to]) {
		v.pb({d[x].ff + 1, d[x].ss});
	}
	sort(v.rbegin(), v.rend());
	if(v.size() >= 3) {
		int nmx = (v[0].ff * (v[1].ff + v[2].ff)), ncnt = 0;
		int sum = 0;
		for(auto it : v) {
			if(it.ff == v[2].ff) {
				sum += it.ss;
			}
		}
		if(v[1].ff == v[2].ff) {
			ncnt = sum * sum;
			for(auto it : v) {
				if(it.ff == v[2].ff) {
					ncnt -= it.ss * it.ss;
				}
			}
			ncnt /= 2;
		} else if(v[1].ff == v[0].ff) {
			ncnt = (v[0].ss + v[1].ss) * sum;
		} else {
			ncnt = v[1].ss * sum;
		}
		if(nmx > ans1) {
			ans1 = nmx;
			ans2 = ncnt;
		} else if(nmx == ans1) {
			ans2 += ncnt;
		}
	}
	pair<int, int> mx1, mx2;
	mx1 = mx2 = make_pair(0, 0);
	if(cnt) mx1 = make_pair(mx + 1, cnt);
	for(int x : g[to]) {
		if(d[x].ff + 2 > mx1.ff) {
			mx2 = mx1;
			mx1 = d[x];
			mx1.ff += 2;
		} else if(d[x].ff + 2 == mx1.ff) {
			mx1.ss += d[x].ss;
		} else if(d[x].ff + 2 > mx2.ff) {
			mx2 = d[x];
			mx2.ff += 2;
		} else if(d[x].ff + 2 == mx2.ff) {
			mx2.ss += d[x].ss;
		}
	}
	for(int x : g[to]) {
		if(d[x].ff + 2 == mx1.ff) {
			if(d[x].ss == mx1.ss) {
				dfs(x, mx2.ff, mx2.ss);
			} else {
				dfs(x, mx1.ff, mx1.ss - d[x].ss);
			}
		} else {
			dfs(x, mx1.ff, mx1.ss);
		}
	}
}

void go() {
	cin >> n;
	for(int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		g[x].pb(y);
		g[y].pb(x);
	}
	ans1 = 0;
	ans2 = 1;
	if(n == 2) {
		cout << ans1 << ' ' << ans2 << '\n';
		return;
	}
	int root = 1;
	for(int i = 1; i <= n; i++) {
		if(g[i].size() > 1) {
			root = i;
		}
	}
	init(root, 0);
	dfs(root, 0, 0);
	cout << ans1 << ' ' << ans2 << '\n';
}
 
signed main() {
	// #ifndef ONO
	// freopen("file.in", "r", stdin);
	// freopen("file.out", "w", stdout);
	// #endif
	fast;
	int t = 1;
	// cin >> t;
	while(t--) {
		go();
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 6 ms 11984 KB Output is correct
3 Correct 8 ms 12068 KB Output is correct
4 Correct 8 ms 11952 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 9 ms 12084 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 12060 KB Output is correct
13 Correct 8 ms 11992 KB Output is correct
14 Correct 9 ms 12196 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12000 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 8 ms 12072 KB Output is correct
19 Correct 6 ms 11976 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 7 ms 11996 KB Output is correct
22 Correct 9 ms 12068 KB Output is correct
23 Correct 8 ms 12068 KB Output is correct
24 Correct 9 ms 12028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 6 ms 11984 KB Output is correct
3 Correct 8 ms 12068 KB Output is correct
4 Correct 8 ms 11952 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 9 ms 12084 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 12060 KB Output is correct
13 Correct 8 ms 11992 KB Output is correct
14 Correct 9 ms 12196 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12000 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 8 ms 12072 KB Output is correct
19 Correct 6 ms 11976 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 7 ms 11996 KB Output is correct
22 Correct 9 ms 12068 KB Output is correct
23 Correct 8 ms 12068 KB Output is correct
24 Correct 9 ms 12028 KB Output is correct
25 Correct 9 ms 12728 KB Output is correct
26 Correct 12 ms 12672 KB Output is correct
27 Correct 9 ms 12756 KB Output is correct
28 Correct 9 ms 12756 KB Output is correct
29 Correct 8 ms 12628 KB Output is correct
30 Correct 9 ms 12724 KB Output is correct
31 Correct 10 ms 12720 KB Output is correct
32 Correct 9 ms 12668 KB Output is correct
33 Correct 10 ms 12884 KB Output is correct
34 Correct 13 ms 12976 KB Output is correct
35 Correct 16 ms 12884 KB Output is correct
36 Correct 15 ms 12592 KB Output is correct
37 Correct 15 ms 12884 KB Output is correct
38 Correct 15 ms 13104 KB Output is correct
39 Correct 11 ms 12592 KB Output is correct
40 Correct 11 ms 12500 KB Output is correct
41 Correct 12 ms 12372 KB Output is correct
42 Correct 10 ms 12372 KB Output is correct
43 Correct 17 ms 12244 KB Output is correct
44 Correct 10 ms 12244 KB Output is correct
45 Correct 8 ms 12244 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 9 ms 12416 KB Output is correct
48 Correct 8 ms 12548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11988 KB Output is correct
2 Correct 6 ms 11984 KB Output is correct
3 Correct 8 ms 12068 KB Output is correct
4 Correct 8 ms 11952 KB Output is correct
5 Correct 7 ms 12068 KB Output is correct
6 Correct 6 ms 11988 KB Output is correct
7 Correct 6 ms 11988 KB Output is correct
8 Correct 8 ms 11988 KB Output is correct
9 Correct 7 ms 11988 KB Output is correct
10 Correct 9 ms 12084 KB Output is correct
11 Correct 7 ms 11988 KB Output is correct
12 Correct 7 ms 12060 KB Output is correct
13 Correct 8 ms 11992 KB Output is correct
14 Correct 9 ms 12196 KB Output is correct
15 Correct 7 ms 12068 KB Output is correct
16 Correct 7 ms 12000 KB Output is correct
17 Correct 6 ms 12072 KB Output is correct
18 Correct 8 ms 12072 KB Output is correct
19 Correct 6 ms 11976 KB Output is correct
20 Correct 7 ms 11988 KB Output is correct
21 Correct 7 ms 11996 KB Output is correct
22 Correct 9 ms 12068 KB Output is correct
23 Correct 8 ms 12068 KB Output is correct
24 Correct 9 ms 12028 KB Output is correct
25 Correct 9 ms 12728 KB Output is correct
26 Correct 12 ms 12672 KB Output is correct
27 Correct 9 ms 12756 KB Output is correct
28 Correct 9 ms 12756 KB Output is correct
29 Correct 8 ms 12628 KB Output is correct
30 Correct 9 ms 12724 KB Output is correct
31 Correct 10 ms 12720 KB Output is correct
32 Correct 9 ms 12668 KB Output is correct
33 Correct 10 ms 12884 KB Output is correct
34 Correct 13 ms 12976 KB Output is correct
35 Correct 16 ms 12884 KB Output is correct
36 Correct 15 ms 12592 KB Output is correct
37 Correct 15 ms 12884 KB Output is correct
38 Correct 15 ms 13104 KB Output is correct
39 Correct 11 ms 12592 KB Output is correct
40 Correct 11 ms 12500 KB Output is correct
41 Correct 12 ms 12372 KB Output is correct
42 Correct 10 ms 12372 KB Output is correct
43 Correct 17 ms 12244 KB Output is correct
44 Correct 10 ms 12244 KB Output is correct
45 Correct 8 ms 12244 KB Output is correct
46 Correct 8 ms 12244 KB Output is correct
47 Correct 9 ms 12416 KB Output is correct
48 Correct 8 ms 12548 KB Output is correct
49 Correct 525 ms 91072 KB Output is correct
50 Correct 483 ms 91096 KB Output is correct
51 Correct 510 ms 90180 KB Output is correct
52 Correct 486 ms 74780 KB Output is correct
53 Correct 409 ms 81808 KB Output is correct
54 Correct 425 ms 100724 KB Output is correct
55 Correct 464 ms 100780 KB Output is correct
56 Correct 411 ms 75344 KB Output is correct
57 Correct 439 ms 96612 KB Output is correct
58 Correct 465 ms 83856 KB Output is correct
59 Correct 433 ms 93364 KB Output is correct
60 Correct 520 ms 106192 KB Output is correct
61 Correct 683 ms 131612 KB Output is correct
62 Correct 640 ms 114956 KB Output is correct
63 Correct 636 ms 63484 KB Output is correct
64 Correct 647 ms 59892 KB Output is correct
65 Correct 662 ms 50264 KB Output is correct
66 Correct 623 ms 44868 KB Output is correct
67 Correct 581 ms 43696 KB Output is correct
68 Correct 598 ms 43504 KB Output is correct
69 Correct 617 ms 42840 KB Output is correct
70 Correct 590 ms 42676 KB Output is correct
71 Correct 609 ms 42592 KB Output is correct
72 Correct 588 ms 42804 KB Output is correct
73 Correct 574 ms 43248 KB Output is correct
74 Correct 567 ms 43432 KB Output is correct
75 Correct 579 ms 44160 KB Output is correct
76 Correct 555 ms 45500 KB Output is correct
77 Correct 484 ms 48672 KB Output is correct
78 Correct 338 ms 55080 KB Output is correct