Submission #440058

# Submission time Handle Problem Language Result Execution time Memory
440058 2021-07-01T14:28:27 Z dutch Islands (IOI08_islands) C++17
100 / 100
1056 ms 102164 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define w (a[e][1])
#define V() (e == u ? a[e][0] : e)
#define v V()
#define E u
 
const int LIM = 1e6;
 
vector<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM];
ll d[LIM], s[LIM], best;
bitset<LIM> vis;
 
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
 
	for(int i=0; i<n; ++i){
		cin >> a[i][0] >> a[i][1]; --a[i][0];
		g[a[i][0]].push_back(i);
	}
	fill(p, p+n, -1);
 
	ll ans = 0;
	stack<int> st; 
 
	for(int z=0; z<n; ++z){
		if(vis[z]) continue;
		r = -1, best = 0;
		st.push(z);
		while(!st.empty()){
			int u = st.top();
			if(!vis[u]){
				vis[u] = 1;
				for(int e : g[u]) if(e != p[u]){
					if(vis[v]){
						r = e + (vis[u] = 0);
						break;
					}else{
						p[v] = e;
						st.push(v);
					}
					if(r >= 0) break;
				}
				if(u != p[u]){
					if(vis[a[u][0]]) r = u + (vis[u] = 0);
					p[a[u][0]] = u;
					st.push(a[u][0]);
				}
			}else{
				vis[u] = 0;
				st.pop();
			}
			if(r >= 0) while(!st.empty()) vis[st.top()] = 0, st.pop();
		}
 
		st.push(r);
 
		while(!st.empty()){
			int u = st.top();
			if(!vis[u]){
				vis[u] = 1, s[u] = d[u];
				for(int e : g[u]) if(e != r && !vis[v]){
					p[v] = u;
					d[v] = d[u] + (ll)w;
					st.push(v);
				}
				if(u != r && !vis[a[u][0]]){
					p[a[u][0]] = u;
					d[a[u][0]] = d[u] + (ll)a[u][1];
					st.push(a[u][0]);
				}
			}else{
				ll curr = 0;
				for(int e : g[u]) if(e != r){
					s[u] = max(s[u], s[v]);
					best = max(best, curr + s[v] - d[u]);
					curr = max(curr, s[v] - d[u]);
				}
				if(u != r){
					s[u] = max(s[u], s[a[u][0]]);
					best = max(best, curr + s[a[u][0]] - d[u]);
					curr = max(curr, s[a[u][0]] - d[u]);
				}
				st.pop();
			}
		}
 
		int u = a[r][0], last = -1;
		while(1){
			vis[u] = 0;
			s[u] = d[u];
			for(int e : g[u])
				if(e != r && v != p[u] && v != last) s[u] = max(s[u], s[v]);
			if(u != r && a[u][0] != p[u] && a[u][0] != last) s[u] = max(s[u], s[a[u][0]]);
 
			if(u == r) break;
			last = u;
			u = p[u];
		}
		u = r;
		while(1){
			vis[u] = 1;
			for(int e : g[u])
				if(e != r && v != p[u] && !vis[v]) last = v;
			if(u != r && a[u][0] != p[u] && !vis[a[u][0]]) last = a[u][0];
 
			s[last] = max(s[last], s[u]);
 
			if(u == a[r][0]) break;
			u = last;
			last = -1;
		}
 
		u = a[r][0], last = -1;
		ll res = 0;
 
		while(u != r){
			ll mx = 0;
			for(int e : g[u])
				if(e != r && v != p[u] && v != last) mx = max(mx, s[v] - d[u]);
			if(a[u][0] != p[u] && a[u][0] != last) mx = max(mx, s[a[u][0]] - d[u]);
 
			res = max(res, d[a[r][0]] - d[u] + mx + s[p[u]]);
			last = u;
			u = p[u];
		}
		res += a[r][1];
		ans += max(best, res);
	}
 
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23716 KB Output is correct
2 Correct 18 ms 23824 KB Output is correct
3 Correct 17 ms 23856 KB Output is correct
4 Correct 16 ms 23728 KB Output is correct
5 Correct 16 ms 23756 KB Output is correct
6 Correct 16 ms 23768 KB Output is correct
7 Correct 16 ms 23756 KB Output is correct
8 Correct 16 ms 23756 KB Output is correct
9 Correct 17 ms 23740 KB Output is correct
10 Correct 17 ms 23756 KB Output is correct
11 Correct 17 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23756 KB Output is correct
2 Correct 17 ms 23876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23884 KB Output is correct
2 Correct 19 ms 23996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24556 KB Output is correct
2 Correct 40 ms 25372 KB Output is correct
3 Correct 24 ms 24800 KB Output is correct
4 Correct 22 ms 24324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 26092 KB Output is correct
2 Correct 53 ms 28760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 33068 KB Output is correct
2 Correct 109 ms 37220 KB Output is correct
3 Correct 126 ms 36412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 151 ms 43448 KB Output is correct
2 Correct 219 ms 47124 KB Output is correct
3 Correct 227 ms 59732 KB Output is correct
4 Correct 259 ms 56132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 59312 KB Output is correct
2 Correct 1056 ms 76264 KB Output is correct
3 Correct 313 ms 60728 KB Output is correct
4 Correct 362 ms 71160 KB Output is correct
5 Correct 463 ms 68920 KB Output is correct
6 Correct 904 ms 69404 KB Output is correct
7 Correct 421 ms 76192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 86688 KB Output is correct
2 Correct 421 ms 100108 KB Output is correct
3 Correct 442 ms 102164 KB Output is correct
4 Correct 419 ms 93616 KB Output is correct
5 Correct 448 ms 83260 KB Output is correct
6 Correct 358 ms 86300 KB Output is correct
7 Correct 945 ms 85132 KB Output is correct