Submission #720253

# Submission time Handle Problem Language Result Execution time Memory
720253 2023-04-07T18:11:18 Z jcelin Islands (IOI08_islands) C++14
90 / 100
624 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 = 1e18;
const int MAXN = 1e6 + 7;
bool bio[MAXN];
int to[MAXN], w[MAXN]; 
vi g[MAXN];
ll ans, ret;
vll dp, s;
 
int ciklus(int u){
	if(bio[u]) return u;
	bio[u] = 1;
	int v = ciklus(to[u]);
	if(v){
		dp.PB(u), s.PB(w[u]);
		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 = (ll)w[x] + dfs(x);
		ret = max(ret, (ll)ch + (ll)mx);
		mx = max(mx, ch);
	}
	return mx;
}
 
void resi(int u){
	dp.clear(), s.clear();
	ciklus(u);
	int sz = s.size();
	reverse(all(dp));
	reverse(all(s));
	for(ll &x : dp) x = (ll)dfs(x);
	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, (ll)dp[i] + (ll)s[i] + (ll)m1);
		ret = max(ret, (ll)s[sz - 1] + (ll)m2 + (ll)dp[i] - (ll)s[i]);
		m1 = max(m1, (ll)dp[i] - (ll)s[i]);
		m2 = max(m2, (ll)dp[i] + (ll)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;
}
 
 
 
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23796 KB Output is correct
2 Correct 16 ms 23780 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 14 ms 23820 KB Output is correct
5 Correct 15 ms 23732 KB Output is correct
6 Correct 15 ms 23824 KB Output is correct
7 Correct 14 ms 23800 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 13 ms 23716 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 16 ms 23728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23892 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23764 KB Output is correct
2 Correct 15 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24508 KB Output is correct
2 Correct 34 ms 26092 KB Output is correct
3 Correct 21 ms 24420 KB Output is correct
4 Correct 19 ms 24020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 27104 KB Output is correct
2 Correct 46 ms 28628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 31072 KB Output is correct
2 Correct 73 ms 39808 KB Output is correct
3 Correct 89 ms 45576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 37400 KB Output is correct
2 Correct 144 ms 55012 KB Output is correct
3 Correct 148 ms 72096 KB Output is correct
4 Correct 203 ms 78440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 194 ms 36652 KB Output is correct
2 Correct 475 ms 95784 KB Output is correct
3 Correct 186 ms 47108 KB Output is correct
4 Correct 308 ms 79952 KB Output is correct
5 Correct 263 ms 77756 KB Output is correct
6 Correct 624 ms 51568 KB Output is correct
7 Correct 313 ms 109900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 292 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -