Submission #720255

# Submission time Handle Problem Language Result Execution time Memory
720255 2023-04-07T18:13:49 Z jcelin Islands (IOI08_islands) C++14
90 / 100
616 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;
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 = w[x] + dfs(x);
		ret = max(ret, ch + 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, 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;
}
 
 
 
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 13 ms 23832 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 13 ms 23824 KB Output is correct
5 Correct 13 ms 23820 KB Output is correct
6 Correct 13 ms 23808 KB Output is correct
7 Correct 13 ms 23768 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 13 ms 23796 KB Output is correct
10 Correct 14 ms 23848 KB Output is correct
11 Correct 17 ms 23800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23836 KB Output is correct
2 Correct 14 ms 23892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23828 KB Output is correct
2 Correct 14 ms 24132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 24404 KB Output is correct
2 Correct 22 ms 26060 KB Output is correct
3 Correct 20 ms 24456 KB Output is correct
4 Correct 20 ms 24024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 27092 KB Output is correct
2 Correct 43 ms 28616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 31124 KB Output is correct
2 Correct 85 ms 39776 KB Output is correct
3 Correct 113 ms 45648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 102 ms 37404 KB Output is correct
2 Correct 131 ms 55052 KB Output is correct
3 Correct 173 ms 72008 KB Output is correct
4 Correct 197 ms 78424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 200 ms 36540 KB Output is correct
2 Correct 456 ms 95956 KB Output is correct
3 Correct 195 ms 47084 KB Output is correct
4 Correct 294 ms 79936 KB Output is correct
5 Correct 266 ms 77648 KB Output is correct
6 Correct 616 ms 51636 KB Output is correct
7 Correct 299 ms 109756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 283 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -