Submission #507008

# Submission time Handle Problem Language Result Execution time Memory
507008 2022-01-12T08:23:34 Z akhan42 Hard route (IZhO17_road) C++17
0 / 100
9 ms 12068 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(v) v.begin(), v.end()

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }


const int MX = 500 * 1000 + 10;
int n;
vi gr[MX];
bool is_diam[MX];
ii far_node;
vi path;
int da[MX], db[MX];
vi diam;
int far_ch[MX];
int max_hard = -1;
int counter_hard = 0;


void dfs_far(int node, int par = 0, int depth = 0) {
	da[node] = depth;
	maxeq(far_node, mp(depth, node));
	for(int nb: gr[node]) {
		if(nb == par) continue;
		dfs_far(nb, node, depth + 1);
	}
}

int get_farthest(int node) {
	far_node = mp(0, 0);
	dfs_far(node);
	return far_node.S;
}

void dfs_path(int node, int tar, int par = 0) {
	db[node] = sz(path);
	path.pb(node);

	if(node == tar) {
		for(int v: path) {
			is_diam[v] = true;
			diam.pb(v);
		}
	}

	for(int nb: gr[node]) {
		if(nb == par) continue;
		dfs_path(nb, tar, node);
	}
	path.pop_back();
}


void record_path(int start, int tar) {
	dfs_path(start, tar);
}

void update(int hard) {
	if(hard > max_hard) {
		max_hard = hard;
		counter_hard = 1;
	} else if(hard == max_hard) {
		counter_hard += 1;
	}
}

void dfs(int node, int par = 0) {
	vi v;
	for(int nb: gr[node]) {
		if(nb == par || is_diam[nb]) continue;

		dfs(nb, node);
		int val = far_ch[nb] + 1;
		maxeq(far_ch[node], val);

		v.pb(val);
	}
	sort(all(v));
	reverse(all(v));

	if(sz(v) >= 2) {
		int i = 0;
		while(i < sz(v) && v[i] == v[0]) {
			i += 1;
		}
		int mx = v[0];
		if(i >= 2) {
			int hard = (2 * mx) * max(da[node], db[node]);
			int times = i * (i - 1) / 2;
			forn(w, times)
				update(hard);
		} else {
			int hard = (v[0] + v[1]) * max(da[node], db[node]);

			int j = 1;
			while(j < sz(v) && v[j] == v[1])
				j += 1;

			forn(w, j - 1)
				update(hard);
		}
	}
}

void count_for_diam() {
	int mx1 = -1;
	forn(i, sz(diam)) {
		if(i > 0 && far_ch[diam[i]]) {
			int val = (i + far_ch[diam[i]]) * max(mx1, sz(diam) - i - 1);
			update(val);
		}
		maxeq(mx1, far_ch[diam[i]]);
	}
}


void solve() {
	cin >> n;
	forn(i, n - 1) {
		int a, b;
		cin >> a >> b;
		gr[a].pb(b);
		gr[b].pb(a);
	}

	int a = get_farthest(1);
	int b = get_farthest(a);
	record_path(b, a);

//	cerr << "DIAM a, b: " << a << ' ' << b << '\n';
//	for(int v: diam) {
//		cerr << v << ' ';
//	}
//	forbn(i, 1, n + 1) {
//		cerr << "is diam " << i << ' ' << is_diam[i] << '\n';
//	}
//	forbn(i, 1, n + 1) {
//		cerr << "dist_a, dist_b " << i << ' ' << da[i] << ' ' << db[i] << '\n';
//	}

	int mx = -1;
	for(int v: diam) {
		dfs(v);
		maxeq(mx, far_ch[v]);
	}
	int val = (sz(diam) - 1) * mx;
	update(val);

	count_for_diam();
	reverse(all(diam));
	count_for_diam();

	cout << max_hard << ' ' << counter_hard << '\n';
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
//	cout.tie(nullptr);

//	freopen("deleg.in", "r", stdin);
//	freopen("deleg.out", "w", stdout);

	int t = 1;
//	cin >> t;
	while(t--) {
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 6 ms 12068 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12060 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 9 ms 11980 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 6 ms 12068 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12060 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 9 ms 11980 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11980 KB Output is correct
2 Correct 6 ms 12068 KB Output is correct
3 Correct 8 ms 11980 KB Output is correct
4 Correct 8 ms 12060 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 9 ms 11980 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -