답안 #720256

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720256 2023-04-07T18:16:54 Z jcelin Islands (IOI08_islands) C++14
100 / 100
610 ms 131072 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
#define vi vector<int>
#define vll vector<ll>
#define PB push_back
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
const ll INF = (1LL << 60);
const int MAXN = 1e6 + 10;

bool bio[MAXN];
int to[MAXN], w[MAXN]; 
vi g[MAXN];
ll ans, ret;
ll dp[MAXN], s[MAXN];
int sz;
 
int ciklus(int u){
	if(bio[u]) return u;
	bio[u] = 1;
	int v = ciklus(to[u]);
	if(v){
		dp[sz] = u, s[sz] = w[u];
		sz++;
		if(u == v) return 0;
		return v;
	}
	bio[u] = 0;
	return 0;
}
 
ll dfs(int u){
	ll mx = 0;
	bio[u] = 1;
	for(int &x : g[u]){
		if(bio[x]) continue;
		ll ch = w[x] + dfs(x);
		ret = max(ret, ch + mx);
		mx = max(mx, ch);
	}
	return mx;
}
 
void resi(int u){
	sz = 0;
	ciklus(u);
	reverse(dp, dp + sz);
	reverse(s, s + sz);
	REP(i, sz) dp[i] = (ll)dfs(dp[i]);
	for(int i = sz - 1; i; i--) swap(s[i], s[i - 1]);
	FOR(i, 1, sz) s[i] += s[i - 1];
	ll m1 = -INF, m2 = -INF;
	REP(i, sz){
		ret = max(ret, dp[i] + s[i] + m1);
		ret = max(ret, s[sz - 1] + m2 + dp[i] - s[i]);
		m1 = max(m1, dp[i] - s[i]);
		m2 = max(m2, dp[i] + s[i]);
	}
	ans += ret;
	ret = 0;
}
 
void solve(){
	int n;
	cin >> n;
	FOR(i, 1, n + 1) cin >> to[i] >> w[i], g[to[i]].PB(i); 
	FOR(i, 1, n + 1) if(!bio[i]) resi(i);
	cout << ans << "\n";
}
 
 
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t=1;
	//cin >> t;
	while(t--)solve();
	return 0;
}
 
 
 
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23764 KB Output is correct
2 Correct 14 ms 23828 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 13 ms 23836 KB Output is correct
5 Correct 14 ms 23824 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 13 ms 23840 KB Output is correct
8 Correct 14 ms 23764 KB Output is correct
9 Correct 15 ms 23752 KB Output is correct
10 Correct 15 ms 23892 KB Output is correct
11 Correct 14 ms 23732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23852 KB Output is correct
2 Correct 13 ms 23816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23812 KB Output is correct
2 Correct 13 ms 24020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 24328 KB Output is correct
2 Correct 25 ms 25300 KB Output is correct
3 Correct 20 ms 24372 KB Output is correct
4 Correct 17 ms 24020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 25972 KB Output is correct
2 Correct 38 ms 27588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 29960 KB Output is correct
2 Correct 65 ms 36060 KB Output is correct
3 Correct 82 ms 37716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 36556 KB Output is correct
2 Correct 131 ms 45400 KB Output is correct
3 Correct 144 ms 59656 KB Output is correct
4 Correct 174 ms 59932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 206 ms 36648 KB Output is correct
2 Correct 422 ms 72124 KB Output is correct
3 Correct 195 ms 46280 KB Output is correct
4 Correct 259 ms 64584 KB Output is correct
5 Correct 248 ms 62356 KB Output is correct
6 Correct 577 ms 51560 KB Output is correct
7 Correct 275 ms 78668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 337 ms 131072 KB Output is correct
2 Correct 303 ms 95224 KB Output is correct
3 Correct 322 ms 126264 KB Output is correct
4 Correct 296 ms 78996 KB Output is correct
5 Correct 270 ms 76668 KB Output is correct
6 Correct 263 ms 71260 KB Output is correct
7 Correct 610 ms 66980 KB Output is correct