Submission #757033

# Submission time Handle Problem Language Result Execution time Memory
757033 2023-06-12T12:38:57 Z SanguineChameleon Islands (IOI08_islands) C++17
100 / 100
1081 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;

void just_do_it();

int main() {
	#ifdef KAMIRULEZ
		freopen("kamirulez.inp", "r", stdin);
		freopen("kamirulez.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	just_do_it();
	return 0;
}

const int maxn = 1e6 + 20;
int to[maxn];
pair<int, int> edges[maxn * 2];
int adj_pt[maxn];
int par[maxn];
int len[maxn];
pair<long long, int> dp[maxn];
bool flag[maxn];
bool is_cycle[maxn];
int cycle[maxn];
int lens[maxn];
int sz;

void dfs1(int u) {
	flag[u] = true;
	for (int pt = adj_pt[u]; edges[pt].first == u; pt++) {
		int id = edges[pt].second;
		if (id == par[u]) {
			continue;
		}
		int v = id ^ to[id] ^ u;
		if (!flag[v]) {
			par[v] = id;
			dfs1(v);
		}
		else {
			int cur = u;
			while (cur != v) {
				if (par[cur] == -1) {
					break;
				}
				cur = par[cur] ^ to[par[cur]] ^ cur;
			}
			if (cur == v) {
				cur = u;
				while (cur != v) {
					cycle[sz] = cur;
					lens[sz] = len[par[cur]];
					sz++;
					cur = par[cur] ^ to[par[cur]] ^ cur;
				}
				cycle[sz] = v;
				lens[sz] = len[id];
				sz++;
			}
		}
	}
}

void dfs2(int u, int p) {
	dp[u].first = 0;
	dp[u].second = u;
	for (int pt = adj_pt[u]; edges[pt].first == u; pt++) {
		int id = edges[pt].second;
		int v = id ^ to[id] ^ u;
		if (v != p && !is_cycle[v]) {
			dfs2(v, u);
			if (dp[v].first + len[id] > dp[u].first) {
				dp[u].first = dp[v].first + len[id];
				dp[u].second = dp[v].second;
			}
		}
	}
}

long long res;

long long calc_max(int u) {
	is_cycle[u] = false;
	dfs2(u, -1);
	long long max_depth = dp[u].first;
	int v = dp[u].second;
	dfs2(v, -1);
	res = max(res, dp[v].first);
	is_cycle[u] = true;
	return max_depth;
}

long long solve() {
	res = 0;
	long long sum = 0;
	for (int i = 0; i < sz; i++) {
		is_cycle[cycle[i]] = true;
		sum += lens[i];
	}
	long long dist = 0;
	long long mx1 = calc_max(cycle[0]);
	long long mx2 = mx1;
	for (int i = 1; i < sz; i++) {
		dist += lens[i - 1];
		long long max_depth = calc_max(cycle[i]);
		res = max(res, dist + max_depth + mx1);
		res = max(res, sum - dist + max_depth + mx2);
		mx1 = max(mx1, max_depth - dist);
		mx2 = max(mx2, max_depth + dist);
	}
	return res;
}

void just_do_it() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> to[i] >> len[i];
		edges[i * 2 - 1].first = i;
		edges[i * 2 - 1].second = i;
		edges[i * 2].first = to[i];
		edges[i * 2].second = i;
	}
	sort(edges + 1, edges + n * 2 + 1);
	for (int i = 1; i <= n * 2; i++) {
		if (edges[i].first != edges[i - 1].first) {
			adj_pt[edges[i].first] = i;
		}
	}
	long long res = 0;
	for (int u = 1; u <= n; u++) {
		if (!flag[u]) {
			sz = 0;
			par[u] = -1;
			dfs1(u);
			res += solve();
		}
	}
	cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1216 KB Output is correct
2 Correct 15 ms 3160 KB Output is correct
3 Correct 10 ms 1364 KB Output is correct
4 Correct 5 ms 852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 4180 KB Output is correct
2 Correct 25 ms 6484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 11560 KB Output is correct
2 Correct 91 ms 16716 KB Output is correct
3 Correct 85 ms 23060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 20940 KB Output is correct
2 Correct 147 ms 38832 KB Output is correct
3 Correct 212 ms 47984 KB Output is correct
4 Correct 217 ms 58960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 307 ms 48284 KB Output is correct
2 Correct 542 ms 84044 KB Output is correct
3 Correct 224 ms 40508 KB Output is correct
4 Correct 294 ms 70352 KB Output is correct
5 Correct 283 ms 70280 KB Output is correct
6 Correct 1081 ms 47436 KB Output is correct
7 Correct 321 ms 90736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 397 ms 110900 KB Output is correct
2 Correct 359 ms 110932 KB Output is correct
3 Correct 530 ms 131072 KB Output is correct
4 Correct 295 ms 64208 KB Output is correct
5 Correct 282 ms 83896 KB Output is correct
6 Correct 336 ms 68724 KB Output is correct
7 Correct 1069 ms 63412 KB Output is correct