답안 #757037

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
757037 2023-06-12T12:43:10 Z SanguineChameleon Islands (IOI08_islands) C++17
100 / 100
1084 ms 120864 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];
bool head[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;
					head[sz] = (par[cur] == cur);
					sz++;
					cur = par[cur] ^ to[par[cur]] ^ cur;
				}
				cycle[sz] = v;
				head[sz] = (id == v);
				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 += (head[i] ? len[cycle[i]] : len[cycle[(i + 1) % sz]]);
	}
	long long dist = 0;
	long long mx1 = calc_max(cycle[0]);
	long long mx2 = mx1;
	for (int i = 1; i < sz; i++) {
		dist += (head[i - 1] ? len[cycle[i - 1]] : len[cycle[i]]);
		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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1236 KB Output is correct
2 Correct 17 ms 3100 KB Output is correct
3 Correct 12 ms 1360 KB Output is correct
4 Correct 8 ms 896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 4080 KB Output is correct
2 Correct 29 ms 6404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 11568 KB Output is correct
2 Correct 80 ms 16588 KB Output is correct
3 Correct 93 ms 22700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 110 ms 20860 KB Output is correct
2 Correct 152 ms 38232 KB Output is correct
3 Correct 197 ms 47192 KB Output is correct
4 Correct 206 ms 57704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 48216 KB Output is correct
2 Correct 537 ms 82468 KB Output is correct
3 Correct 231 ms 40496 KB Output is correct
4 Correct 275 ms 69436 KB Output is correct
5 Correct 276 ms 69400 KB Output is correct
6 Correct 1084 ms 47580 KB Output is correct
7 Correct 301 ms 89040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 351 ms 110880 KB Output is correct
2 Correct 353 ms 110928 KB Output is correct
3 Correct 475 ms 120864 KB Output is correct
4 Correct 276 ms 49180 KB Output is correct
5 Correct 271 ms 69432 KB Output is correct
6 Correct 295 ms 55732 KB Output is correct
7 Correct 1039 ms 48392 KB Output is correct