제출 #120382

#제출 시각아이디문제언어결과실행 시간메모리
120382nvmdavaIslands (IOI08_islands)C++17
35 / 100
1074 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;
#define N 1000001

vector<pair<int, long long> > adj[N];
bool in[N];
vector<int> s;
vector<int> cyc;
vector<long long> dp, p;
bool inCyc[N];
long long res, t;

long long dfs(int v, int p){
	long long b1 = 0, b2 = 0;
	for(auto xx : adj[v]){
		int x = xx.first;
		long long c = xx.second;
		
		if(inCyc[x] && (p == x || p == 0) && inCyc[v]){
			t = c;
		}
		if(inCyc[x] || p == x) continue;
		auto q = dfs(x, v);
		
		long long up = q + c;
		if(b1 < up){
			b2 = b1;
			b1 = up;
		} else b2 = max(b2, up);
	}
	res = max(res, b1 + b2);
	return b1;
}

long long ans = 0;

void solve(){
	dp.clear();
	p.clear();
	cyc.clear();
	res = 0;
	long long full = 0;
	for(int x : s){
		if(inCyc[x]) {	
			dp.push_back(dfs(x, cyc.empty() ? 0 : cyc.back()));
			if(p.empty()) full = t;
			p.push_back(p.empty() ? 0 : p.back() + t);
			cyc.push_back(x);
		}
	}
	full += p.back();
	long long tmpp = dp[0] + p[0];
	long long tmpm = dp[0] - p[0];
	for(int i = 1; i < cyc.size(); i++){
		res = max(res, tmpm + dp[i] + p[i]);
		res = max(res, tmpp + full + dp[i] - p[i]);
		tmpm = max(tmpm, dp[i] - p[i]);
		tmpp = max(tmpp, dp[i] + p[i]);
	}
	ans += res;
}

int trav(int i, int p){
	s.push_back(i);
	in[i] = 1;
	int res = 0;
	bool ok = 0;
	for(auto xx : adj[i]){
		int x = xx.first;
		if(p == x && ok == 0){
			ok = 1;	
			continue;
		}
		if(in[x]){
			res = x;
			continue;
		}
		int t = trav(x, i);
		if(res < t)res = t;
	}
	if(res) inCyc[i] = 1;
	if(res == i) return 0;
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	
	for(int i = 1; i <= n; i++){
		int e;
		long long l;
		cin>>e>>l;
		adj[e].push_back({i, l});
		adj[i].push_back({e, l});
	}
	
	for(int i = 1; i <= n; i++){
		if(in[i]) continue;
		s.clear();		
		trav(i, 0);
		solve();
	}
	
	cout<<ans;
}

컴파일 시 표준 에러 (stderr) 메시지

islands.cpp: In function 'void solve()':
islands.cpp:54:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 1; i < cyc.size(); i++){
                 ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...