답안 #440029

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
440029 2021-07-01T13:02:04 Z dutch Islands (IOI08_islands) C++17
80 / 100
1605 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])
#define w (a[e][1])
#define v() (e == u ? a[e][0] : e)
 
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<int> g[LIM];
array<int, 2> a[LIM];
int r, p[LIM], dfsTimer, t[LIM], s[LIM];
ll best;
bitset<LIM> vis;
SegmentTree<ll> d;

void dfs(int u){
	s[u] = 1, t[u] = dfsTimer++;
	for(int e : g[u]) if(e != r && !s[v()]){
		p[v()] = u;
		d[dfsTimer] = v() == r ? 0LL : dis(u) + (ll)w;
		dfs(v());
		s[u] += s[v()];
		best = max(best, d(t[u], t[v()]-1) + sub(v()) - 2*dis(u));
	}
}
 
void findCycle(int u, int par){
	vis[u] = 1;
	for(int 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);
		g[i].push_back(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(int 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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 14 ms 23860 KB Output is correct
3 Correct 14 ms 23916 KB Output is correct
4 Correct 14 ms 23884 KB Output is correct
5 Correct 15 ms 23788 KB Output is correct
6 Correct 16 ms 23884 KB Output is correct
7 Correct 18 ms 23884 KB Output is correct
8 Correct 15 ms 23884 KB Output is correct
9 Correct 14 ms 23840 KB Output is correct
10 Correct 16 ms 23856 KB Output is correct
11 Correct 15 ms 23828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23880 KB Output is correct
2 Correct 17 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23900 KB Output is correct
2 Correct 18 ms 24264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 24908 KB Output is correct
2 Correct 44 ms 27396 KB Output is correct
3 Correct 31 ms 25256 KB Output is correct
4 Correct 27 ms 24572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 28740 KB Output is correct
2 Correct 82 ms 30624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 39056 KB Output is correct
2 Correct 172 ms 43332 KB Output is correct
3 Correct 210 ms 52176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 250 ms 45824 KB Output is correct
2 Correct 346 ms 72840 KB Output is correct
3 Correct 441 ms 79644 KB Output is correct
4 Correct 470 ms 96808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 639 ms 94300 KB Output is correct
2 Correct 1428 ms 131072 KB Output is correct
3 Correct 468 ms 77104 KB Output is correct
4 Correct 651 ms 119028 KB Output is correct
5 Correct 675 ms 121616 KB Output is correct
6 Correct 1605 ms 98900 KB Output is correct
7 Runtime error 498 ms 131076 KB Execution killed with signal 9
# 결과 실행 시간 메모리 Grader output
1 Runtime error 406 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -