답안 #720249

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
720249 2023-04-07T18:05:44 Z jcelin Islands (IOI08_islands) C++14
90 / 100
577 ms 131072 KB
#include <bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;

#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<ii>
#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]);
	}
}

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]) ret = 0, resi(i), ans += ret;
	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 13 ms 23800 KB Output is correct
2 Correct 14 ms 23764 KB Output is correct
3 Correct 13 ms 23764 KB Output is correct
4 Correct 13 ms 23764 KB Output is correct
5 Correct 15 ms 23724 KB Output is correct
6 Correct 13 ms 23764 KB Output is correct
7 Correct 13 ms 23764 KB Output is correct
8 Correct 18 ms 23828 KB Output is correct
9 Correct 15 ms 23764 KB Output is correct
10 Correct 14 ms 23764 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23892 KB Output is correct
2 Correct 14 ms 23760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23764 KB Output is correct
2 Correct 14 ms 24056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24360 KB Output is correct
2 Correct 23 ms 25944 KB Output is correct
3 Correct 19 ms 24436 KB Output is correct
4 Correct 14 ms 24092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 26804 KB Output is correct
2 Correct 45 ms 28408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 30740 KB Output is correct
2 Correct 65 ms 38584 KB Output is correct
3 Correct 86 ms 43216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 105 ms 37176 KB Output is correct
2 Correct 127 ms 51900 KB Output is correct
3 Correct 152 ms 68016 KB Output is correct
4 Correct 185 ms 72228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 194 ms 36632 KB Output is correct
2 Correct 432 ms 87996 KB Output is correct
3 Correct 191 ms 46872 KB Output is correct
4 Correct 286 ms 75320 KB Output is correct
5 Correct 251 ms 73044 KB Output is correct
6 Correct 577 ms 51468 KB Output is correct
7 Correct 286 ms 100512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 318 ms 131072 KB Output is correct
2 Correct 288 ms 110540 KB Output is correct
3 Runtime error 279 ms 131072 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -