답안 #507119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
507119 2022-01-12T09:02:18 Z akhan42 Hard route (IZhO17_road) C++17
0 / 100
10 ms 14048 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];
vi far_ctr(MX, 1);
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, int times) {
	if(hard > max_hard) {
		max_hard = hard;
		counter_hard = times;
	} else if(hard == max_hard) {
		counter_hard += times;
	}
}

void update_ch(int node, int nb) {
	int val = far_ch[nb] + 1;
	if(val > far_ch[node]) {
		far_ch[node] = val;
		far_ctr[node] = far_ctr[nb];
	} else if(val == far_ch[node]) {
		far_ctr[node] += far_ctr[nb];
	}
}

void dfs(int node, int par = 0) {
	int mx2 = -1;
	int ct2 = 0;
	for(int nb: gr[node]) {
		if(nb == par || is_diam[nb]) continue;

		dfs(nb, node);
		int val = far_ch[nb] + 1;
		if(val > far_ch[node]) {
			mx2 = far_ch[node];
			ct2 = far_ctr[node];
		} else if(val == far_ch[node]) {
			// do nothing
		} else if(val > mx2) {
			mx2 = val;
			ct2 = 1;
		} else if(val == mx2) {
			ct2 += 1;
		}
		update_ch(node, nb);
	}

	if(mx2 > 0 || far_ctr[node] > 1) {
		if(far_ctr[node] > 1) {
			int hard = 2 * far_ch[node] * max(da[node], db[node]);
			update(hard, far_ctr[node] * (far_ctr[node] - 1) / 2);
		} else {
			int hard = (far_ch[node] + mx2) * max(da[node], db[node]);
			update(hard, ct2);
		}
	}
}

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, far_ctr[diam[i]]);
		}
		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;
//	cerr << counter_hard << '\n';
	for(int v: diam) {
		dfs(v);
		maxeq(mx, far_ch[v]);
	}
//	cerr << counter_hard << '\n';
	int val = (sz(diam) - 1) * mx;
	update(val, 1);
//	cerr << counter_hard << '\n';

	count_for_diam();
//	cerr << counter_hard << '\n';
	reverse(all(diam));
	count_for_diam();
//	cerr << counter_hard << '\n';

	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();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14044 KB Output is correct
2 Correct 8 ms 14040 KB Output is correct
3 Correct 10 ms 13932 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 10 ms 13944 KB Output is correct
6 Correct 9 ms 14048 KB Output is correct
7 Incorrect 8 ms 14044 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14044 KB Output is correct
2 Correct 8 ms 14040 KB Output is correct
3 Correct 10 ms 13932 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 10 ms 13944 KB Output is correct
6 Correct 9 ms 14048 KB Output is correct
7 Incorrect 8 ms 14044 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14044 KB Output is correct
2 Correct 8 ms 14040 KB Output is correct
3 Correct 10 ms 13932 KB Output is correct
4 Correct 8 ms 14028 KB Output is correct
5 Correct 10 ms 13944 KB Output is correct
6 Correct 9 ms 14048 KB Output is correct
7 Incorrect 8 ms 14044 KB Output isn't correct
8 Halted 0 ms 0 KB -