Submission #440019

# Submission time Handle Problem Language Result Execution time Memory
440019 2021-07-01T12:37:59 Z dutch Islands (IOI08_islands) C++17
80 / 100
687 ms 131076 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sub(u) d(t[u], t[u] + s[u] - 1)
#define dis(u) d(t[u], t[u])
 
const int LIM = 1e6;

template<class T> struct SegmentTree{
	int n = LIM, i; T a[2*LIM] ;
	SegmentTree& operator[](int j){ i=j+n; return *this; }
	void operator=(T v){
		for(a[i]=v; i/=2; ) a[i] = max(a[2*i], a[2*i+1]); }
	T operator()(int l, int r){
		T x = 0;
		for(l+=n, r+=n+1; l<r; l/=2, r/=2){
			if(l & 1) x = max(x, a[l++]);
			if(r & 1) x = max(x, a[--r]);
		}
		return x;
	}
};
 
vector<array<int, 3>> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM], dfsTimer, t[LIM], s[LIM];
ll best, lastDist;
bitset<LIM> vis;
SegmentTree<ll> d;

void dfs(int u){
	s[u] = 1;
	ll curr = 0;
	t[u] = dfsTimer++;
	d[t[u]] = (u == r ? 0LL : lastDist);
	for(auto &[v, w, e] : g[u]) if(e != r && !s[v]){
		lastDist = dis(u) + (ll)w, p[v] = u;
		dfs(v);
		s[u] += s[v];
		best = max(best, curr + sub(v) - dis(u));
		curr = max(curr, sub(v) - dis(u));
	}
}
 
void findCycle(int u, int par){
	vis[u] = 1;
	for(auto &[v, w, e] : g[u]) if(e != par){
		if(vis[v]) return void(r = e);
		findCycle(v, e);
		if(r >= 0) return;
	}
	vis[u] = 0;
}
 
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, a[i][1], i});
		g[i].push_back({a[i][0], a[i][1], i});
	}
	fill(p, p+n, -1);
 
	ll ans = 0;
 
	for(int z=0; z<n; ++z){
		if(s[z]) continue;
		r = -1, best = 0;
		findCycle(z, -1);
		bool cycle = r >= 0;
		if(!cycle) r = z;
 
		dfsTimer = 0;
		dfs(r);
 
		if(!cycle){
			ans += best;
			continue;
		}
		assert(cycle);
 
		int u = a[r][0], last = -1;
		ll res = 0, ex;
		while(u != r){
			ll mx = 0;
			for(auto &[v, w, e] : g[u])
				if(e != r && v != p[u] && v != last) mx = max(mx, sub(v) - dis(u));
 
			ex = max(d(0, t[u]-1), d(t[u]+s[u], dfsTimer-1));
			res = max(res, dis(a[r][0]) - dis(u) + mx + ex);
			last = u;
			u = p[u];
		}
		res += a[r][1];

		ans += max(best, res);
		for(int i=0; i<dfsTimer; ++i) d[i] = 0;
	}
 
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 23884 KB Output is correct
2 Correct 16 ms 23924 KB Output is correct
3 Correct 15 ms 23880 KB Output is correct
4 Correct 15 ms 23808 KB Output is correct
5 Correct 16 ms 23800 KB Output is correct
6 Correct 15 ms 23896 KB Output is correct
7 Correct 14 ms 23884 KB Output is correct
8 Correct 15 ms 23884 KB Output is correct
9 Correct 14 ms 23884 KB Output is correct
10 Correct 15 ms 23804 KB Output is correct
11 Correct 16 ms 23832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24016 KB Output is correct
2 Correct 17 ms 23948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 24012 KB Output is correct
2 Correct 18 ms 24396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25028 KB Output is correct
2 Correct 57 ms 28232 KB Output is correct
3 Correct 33 ms 25796 KB Output is correct
4 Correct 23 ms 24700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 29324 KB Output is correct
2 Correct 91 ms 31556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 40876 KB Output is correct
2 Correct 186 ms 45740 KB Output is correct
3 Correct 227 ms 59076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 277 ms 49356 KB Output is correct
2 Correct 394 ms 79252 KB Output is correct
3 Correct 499 ms 85332 KB Output is correct
4 Correct 611 ms 113792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 555 ms 102064 KB Output is correct
2 Runtime error 687 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 465 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -