답안 #506976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
506976 2022-01-12T08:11:55 Z akhan42 Hard route (IZhO17_road) C++17
0 / 100
7 ms 12080 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) {
	int mx1 = -1, mx2 = -1;
	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);
		if(val > mx1) {
			mx2 = mx1;
			mx1 = val;
		} else if(val > mx2) {
			mx2 = val;
		}
	}
	if(mx2 > 0) {
		int hard = (mx1 + mx2) * max(da[node], db[node]);
		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();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12056 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12080 KB Output is correct
4 Correct 6 ms 12072 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 6 ms 12076 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12056 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12080 KB Output is correct
4 Correct 6 ms 12072 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 6 ms 12076 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12056 KB Output is correct
2 Correct 7 ms 12056 KB Output is correct
3 Correct 7 ms 12080 KB Output is correct
4 Correct 6 ms 12072 KB Output is correct
5 Correct 6 ms 11980 KB Output is correct
6 Correct 6 ms 12076 KB Output is correct
7 Incorrect 6 ms 11980 KB Output isn't correct
8 Halted 0 ms 0 KB -