Submission #1012657

# Submission time Handle Problem Language Result Execution time Memory
1012657 2024-07-02T13:11:06 Z Muaath_5 Islands (IOI08_islands) C++17
70 / 100
436 ms 131072 KB
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;

const int N = 1e6+1;

int n;
vector<pair<int, ll>> adj[N];
map<pii, int> edge;
map<pii, int> we;

char vis[N];
vector<int> stck;
vector<int> cyc; 
void dfs(int node, int par=0) {
	vis[node] = 1;
	stck.push_back(node);
	for (auto &[ch, cost] : adj[node]) {
		if (!vis[ch])
			dfs(ch, node);
		else if (!cyc.size() && edge[{node, ch}] == 2) {
			cyc.push_back(node);
			cyc.push_back(ch);
		}
		else if (!cyc.size() && vis[ch] && ch != par) {
			bool st = 0;
			for (int cur : stck) {
				if (cur == ch) st = 1;
				if (st) cyc.push_back(cur);
			}
		}
		
	}
	stck.pop_back();
}

pii remedge;
int blk[2];
int mx[N];
ll mxdep[N];
void farthest_dp(int node, int par=0) {
	mx[node] = node;
	mxdep[node] = 0;
	for (auto [ch, cost] : adj[node]) {
		if (blk[0] == ch || blk[1] == ch || ch == par) continue;
		if (make_pair(node, ch) != remedge && make_pair(ch, node) != remedge) {
			farthest_dp(ch, node);
			if (mxdep[ch] + cost > mxdep[node]) {
				mxdep[node] = mxdep[ch] + cost;
				mx[node] = mx[ch];
			}
		}
	}
}

ll pref[N], a[N];
ll solve_for(int node)
{
	cyc.clear();
	dfs(node);
	const ll L = cyc.size();

	// cerr << "#" << node << ": ";	
	// cerr << L << ": ";
	// for(int i:cyc)cerr<<i<<' ';
	// cerr<<"| ";
	
	
	
	// remove an edge from the cycle and find diameter
	if (L > 2)
		remedge = {cyc[0], cyc[1]};
	else
		remedge = {-1, -1};
	farthest_dp(node);
	int fr = mx[node];
	farthest_dp(fr);
	ll diam = mxdep[fr];
	
	// a[i] = max depth of tree going from i not in cycle direction 
	// prefix sums
	for (int i = 0; i < L; i++) {
		blk[0] = cyc[(i+1)%L];
		blk[1] = cyc[(i-1+L)%L];
		farthest_dp(cyc[i]);
		a[i] = mxdep[cyc[i]];
	}
	
	
	// for (int i = 0; i < L; i++)
		// cerr << a[i] << ' ';
	// cerr << "| ";

	// cost of path(i,j) = a[i] + a[j] + (p[i] - p[j]);
	// maintain maximum a[j] - p[j], and the answer for suffix i:
	// 	
	ll prvmx = 0;
	ll sol = 0;
	pref[0] = 0;
	for (int i = 1; i < L; i++)
		pref[i] = pref[i-1] + we[{cyc[i], cyc[i-1]}];
	
	const ll LL = pref[L-1] + we[{cyc[L-1], cyc[0]}];
	if (L == 2) {
		sol = *max_element(a, a+L) + we[{cyc[0], cyc[1]}];
		goto RET;
	}
	
	prvmx = a[0];
	for (int i = 1; i < L; i++) {
		sol = max(sol, a[i] + pref[i] + prvmx);
		prvmx = max(prvmx, a[i] - pref[i]);
	}

	prvmx = a[0];
	for (int i = 1; i < L; i++) {
		sol = max(sol, LL + a[i] - pref[i] + prvmx);
		prvmx = max(prvmx, a[i] + pref[i]);
	}
	
	RET:
	return max(diam, sol);
}


signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int u;
		int w;
		cin >> u >> w;
		adj[i].push_back({u, w});
		adj[u].push_back({i, w});
		edge[{i,u}]++, edge[{u,i}]++;
		we[{i, u}] = we[{u, i}] = max(we[{i, u}], w);
	}
	ll sum = 0;
	for (int i = 1; i <= n; i++)
		if (!vis[i])
			sum += solve_for(i);
	cout << sum;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 25432 KB Output is correct
2 Correct 8 ms 25436 KB Output is correct
3 Correct 7 ms 25464 KB Output is correct
4 Correct 7 ms 25436 KB Output is correct
5 Correct 7 ms 25432 KB Output is correct
6 Correct 9 ms 25436 KB Output is correct
7 Correct 7 ms 25436 KB Output is correct
8 Correct 6 ms 25436 KB Output is correct
9 Correct 7 ms 25436 KB Output is correct
10 Correct 7 ms 25436 KB Output is correct
11 Correct 7 ms 25436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 25896 KB Output is correct
2 Correct 7 ms 25800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 25948 KB Output is correct
2 Correct 12 ms 26888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 30336 KB Output is correct
2 Correct 63 ms 38996 KB Output is correct
3 Correct 45 ms 32080 KB Output is correct
4 Correct 22 ms 28760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 44484 KB Output is correct
2 Correct 99 ms 59728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 270 ms 91480 KB Output is correct
2 Correct 315 ms 109268 KB Output is correct
3 Correct 343 ms 130896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 237 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 436 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 370 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -