답안 #676679

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676679 2022-12-31T17:25:06 Z QwertyPi Islands (IOI08_islands) C++14
80 / 100
629 ms 131072 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

const int N = 1e6 + 11;
vector<pair<int, int>> G[N];
int p[N], w[N];
long long dp[N];
bool vis[N], cyc[N];

struct edge{
	int u, v, w;
};

vector<edge> extra;
int tin[N], t = 0;
void dfs(int id, int v, int pa = -1){
	vis[v] = true; tin[v] = ++t;
	for(auto i : G[v]){
		if(!vis[i.fi]){
			p[i.fi] = v;
			dfs(id, i.fi, v);
			w[i.fi] = i.se;
		}else if(i.fi != pa){
			extra[id] = {v, i.fi, i.se};
		}
	}
}

bool is_anc(int u, int v){
	return tin[u] <= tin[v];
}

long long ans[N];
void dfs_dp(int id, int v, int pa = -1){
	vector<long long> dis;
	for(auto i : G[v]){
		if(!cyc[i.fi] && i.fi != pa){
			dfs_dp(id, i.fi, v);
			dis.push_back(dp[i.fi] + i.se);
		}
	}
	sort(dis.begin(), dis.end());
	if(dis.size() >= 2) ans[id] = max(ans[id], dis.back() + dis[dis.size() - 2]);
	if(dis.size()) dp[v] = dis.back(), ans[id] = max(ans[id], dis.back());
}

void solve(int id, vector<long long>& DP, vector<int>& W){
	int n = W.size();
	vector<long long> S(n * 2 + 1);
	S[0] = 0;
	for(int i = 0; i < n; i++){
		S[i + 1] = S[i] + W[i];
	}
	for(int i = 0; i < n; i++){
		S[n + i + 1] = S[n + i] + W[i];
	}
	priority_queue<long long> pq, _pq;
	for(int i = 0; i < n; i++){
		pq.push(S[i] + DP[i]);
	}
	for(int i = 0; i < n; i++){
		_pq.push(S[i] + DP[i]);
		while(pq.size() && _pq.size() && pq.top() == _pq.top()) pq.pop(), _pq.pop();
		ans[id] = max(ans[id], DP[i] - S[i] + pq.top());
		pq.push(S[i + n] + DP[i]);
	}
}

int32_t main(){
	cin.tie(0); cout.tie(0)->sync_with_stdio(false);
	int n; cin >> n;
	for(int i = 1; i <= n; i++){
		int v, _w; cin >> v >> _w;
		G[i].push_back({v, _w});
		G[v].push_back({i, _w});
	}
	
	vector<int> roots;
	for(int i = 1; i <= n; i++){
		if(!vis[i]){
			extra.resize(extra.size() + 1);
			dfs(roots.size(), i);
			roots.push_back(i);
		}
	}
	
	int CC = roots.size();
	for(int i = 0; i < CC; i++){
		vector<pair<int, int>> cyc_L, cyc_R;
		int x = extra[i].u, y = extra[i].v;
		vector<int> cyc_v;
		while(!is_anc(x, y)){
			cyc_v.push_back(x);
			cyc[x] = true;
			cyc_L.push_back({x, w[x]});
			x = p[x];
		}
		while(!is_anc(y, x)){
			cyc_v.push_back(y);
			cyc[y] = true;
			cyc_R.push_back({y, w[y]});
			y = p[y];
		}
		int l = x;
		cyc_v.push_back(l);
		cyc[l] = true;
		for(auto v : cyc_v){
			dfs_dp(i, v);
		}
		vector<int> W; vector<long long> DP;
		for(auto i : cyc_L){
			DP.push_back(dp[i.fi]);
			W.push_back(i.se);
		}
		DP.push_back(dp[l]);
		for(int i = (int) cyc_R.size() - 1; i >= 0; i--){
			DP.push_back(dp[cyc_R[i].fi]);
			W.push_back(cyc_R[i].se); 
		}
		W.push_back(extra[i].w);
		solve(i, DP, W);
	}
	cout << accumulate(ans, ans + CC, 0LL) << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 12 ms 23844 KB Output is correct
4 Correct 11 ms 23808 KB Output is correct
5 Correct 11 ms 23784 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 13 ms 23764 KB Output is correct
9 Correct 11 ms 23764 KB Output is correct
10 Correct 12 ms 23748 KB Output is correct
11 Correct 13 ms 23764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23892 KB Output is correct
2 Correct 15 ms 24384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 25028 KB Output is correct
2 Correct 29 ms 28308 KB Output is correct
3 Correct 21 ms 25044 KB Output is correct
4 Correct 17 ms 24468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 29924 KB Output is correct
2 Correct 46 ms 32084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 38636 KB Output is correct
2 Correct 101 ms 47832 KB Output is correct
3 Correct 121 ms 62848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 144 ms 47836 KB Output is correct
2 Correct 188 ms 83940 KB Output is correct
3 Correct 240 ms 90612 KB Output is correct
4 Correct 287 ms 130280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 87960 KB Output is correct
2 Runtime error 629 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 355 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -