Submission #731083

# Submission time Handle Problem Language Result Execution time Memory
731083 2023-04-27T00:44:05 Z beaboss Hard route (IZhO17_road) C++14
0 / 100
3 ms 4948 KB
#include "bits/stdc++.h"

using namespace std;

#define s second
#define f first
#define pb push_back

typedef long long ll;

typedef pair<ll, ll> pii;

const ll INF = 1e17 + 10ll;
const ll N = 2*1e5 + 10;
ll n;
vector<ll> adj[N];
ll maxoutdist[N];
ll out[N];
ll maxi = 0;
ll nummaxi = 0;
ll h[N];

void update(ll ans, ll numways) {
	if (ans > maxi) {
		maxi = ans;
		nummaxi = numways;
	} else if (ans == maxi) nummaxi += numways;
}

void dfs1(ll cur, ll p) {
	vector<ll> heights;
	map<ll, ll> cntheights;
	for (auto val: adj[cur]) {
		if (val != p) {
			heights.pb(h[val] + 1);
			cntheights[h[val] + 1]++;
		}
	}


	heights.pb(out[cur]);
	cntheights[out[cur]]++;

	sort(heights.rbegin(), heights.rend());

	// cout << cur << heights[0] << heights[1] << heights[2] << endl;
	if (heights.size() >= 3) {
		ll numways = 1;
		numways *= cntheights[heights[0]];
		cntheights[heights[0]]--;
		if (heights[1]==heights[2]) numways *= (cntheights[heights[1]] * (cntheights[heights[1]] - 1ll)/2ll);
		else numways *= (cntheights[heights[1]] * (cntheights[heights[2]]));

		update(heights[0] * (heights[1] + heights[2]), numways);

	}

	for (auto val: adj[cur]) {
		if (val != p) dfs1(val, cur);
	}
}

void dfs0(ll cur, ll p) {
	// cout << cur << endl;
	vector<ll> heights ={0, 0, 0};
	for (auto val: adj[cur]) if (val != p) heights.pb(h[val] + 2);

	sort(heights.rbegin(), heights.rend());

	for (auto val: adj[cur]) {
		if (val != p) {
			ll curopt = heights[0];
			if (h[val] + 2 == curopt) curopt = heights[1];
			if (curopt < out[cur] + 1) {
				curopt = out[cur]+1;
			}
			out[val] = max(out[val], curopt);
			dfs0(val, cur);
		}
	}

	// cout << cur << out[cur] << endl;

}


void dfs(ll cur, ll p) { // get all heights
	vector<ll> heights;
	map<ll, ll> cntheights;
	for (auto val: adj[cur]) {
		if (val != p) {
			dfs(val, cur);
			h[cur] = max(h[val] + 1, h[cur]);
		}
	}

}


int main() {
	

	cin >> n;

	for (ll i = 0; i < n -1; i++ ) {
		ll a, b;
		cin >> a >> b;
		adj[a].pb(b);
		adj[b].pb(a);

	}
	dfs(1, -1);
	dfs0(1, -1);
	dfs1(1, -1);
	if (maxi == 0) nummaxi = 1;
	cout << maxi << ' ' << nummaxi << endl;



	
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Incorrect 3 ms 4948 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Incorrect 3 ms 4948 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 2 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Incorrect 3 ms 4948 KB Output isn't correct
5 Halted 0 ms 0 KB -