답안 #676776

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676776 2023-01-01T05:44:14 Z QwertyPi Islands (IOI08_islands) C++14
6 / 100
431 ms 131072 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
 
const int N = 1e6 + 11;
 
struct a_edge{
	int v, w, nxt;
};

// 28
a_edge E[N * 2]; int e = 0; int adj[N];
void init(){
	fill(adj, adj + N, -1);
}
void add_edge(int u, int v, int w){
	E[e] = {v, w, adj[u]}; adj[u] = e++;
}

// 24
int p[N], w[N], tin[N], t = 0;
long long dp[N];
bool cyc[N];
 
// 12
struct edge{
	int u, v, w;
};
vector<edge> extra;
 
void dfs(int id, int v, int pa = -1){
	tin[v] = ++t;
	for(int eid = adj[v]; eid != -1; eid = E[eid].nxt){
		int u = E[eid].v, _w = E[eid].w;
		if(!tin[u]){
			p[u] = v;
			dfs(id, u, v);
			w[u] = _w;
		}else if(u != pa){
			extra[id] = {v, u, _w};
		}
	}
}
 
bool is_anc(int u, int v){
	return tin[u] <= tin[v];
}
 
long long dfs_dp(int id, int v, int pa = -1){
	long long mx1 = 0, mx2 = 0;
	long long ans = 0;
	for(int eid = adj[v]; eid != -1; eid = E[eid].nxt){
		int u = E[eid].v, _w = E[eid].w;
		if(!cyc[u] && u != pa){
			long long r = dfs_dp(id, u, v);
			ans = max(ans, r);
			long long v = dp[u] + _w;
			if(v > mx1) mx2 = mx1, mx1 = v;
			else if(v > mx2) mx2 = v;
		}
	}
	ans = max(ans, mx1 + mx2); dp[v] = mx1;
	return ans;
}
 
long long solve(int id, vector<long long>& DP, vector<long long>& W){
	int n = W.size();
	for(int i = 0; i < n; i++){
		W.push_back(W[i]);
	}
	W.push_back(0);
	for(int i = (int) W.size() - 2; i >= 0; i--){
		W[i] = W[i + 1] - W[i];
	}
	// 4
	deque<int> v;
	for(int i = 0; i < n; i++){
		while(v.size() && DP[i] + W[i] >= DP[v.back()] + W[v.back()]) v.pop_back();
		v.push_back(i);
	}
	long long ans = 0;
	for(int i = 0; i < n; i++){
		if(v.front() == i) v.pop_front();
		ans = max(ans, DP[i] - W[i] + DP[v.front() % n] + W[v.front()]);
		while(v.size() && DP[i] + W[i + n] >= DP[v.back() % n] + W[v.back()]) v.pop_back();
		v.push_back(i + n);
	}
	return ans;
}

vector<int> cyc_L, cyc_R;
vector<long long> W, DP;
int32_t main(){
	cin.tie(0); cout.tie(0)->sync_with_stdio(false);
	int n; cin >> n;
	init();
	for(int i = 1; i <= n; i++){
		int v, _w; cin >> v >> _w;
		add_edge(i, v, _w);
		add_edge(v, i, _w);
	}
	
	for(int i = 1; i <= n; i++){
		if(!tin[i]){
			extra.resize(extra.size() + 1);
			dfs(extra.size() - 1, i);
		}
	}
	
	// 20
	int CC = extra.size();
	extra.shrink_to_fit();
	long long ans = 0;
	for(int i = 0; i < CC; i++){
		cyc_L.clear(); cyc_R.clear();
		int x = extra[i].u, y = extra[i].v;
		while(!is_anc(x, y)){
			cyc[x] = true;
			cyc_L.push_back(x);
			x = p[x];
		}
		while(!is_anc(y, x)){
			cyc[y] = true;
			cyc_R.push_back(y);
			y = p[y];
		}
		int l = x;
		cyc[l] = true;
		long long rans = 0;
		for(auto v : cyc_L){
			rans = max(rans, dfs_dp(i, v));
		}
		for(auto v : cyc_R){
			rans = max(rans, dfs_dp(i, v));
		}
		rans = max(rans, dfs_dp(i, l));
		for(auto i : cyc_L){
			DP.push_back(dp[i]);
			W.push_back(w[i]);
		}
		DP.push_back(dp[l]);
		for(int i = (int) cyc_R.size() - 1; i >= 0; i--){
			DP.push_back(dp[cyc_R[i]]);
			W.push_back(w[cyc_R[i]]); 
		}
		cyc_L.resize(0); cyc_R.resize(0);
		W.push_back(extra[i].w);
		rans = max(rans, solve(i, DP, W));
		ans += rans;
	}
	cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4180 KB Output isn't correct
2 Incorrect 2 ms 4180 KB Output isn't correct
3 Correct 3 ms 4308 KB Output is correct
4 Incorrect 2 ms 4180 KB Output isn't correct
5 Incorrect 2 ms 4244 KB Output isn't correct
6 Incorrect 2 ms 4180 KB Output isn't correct
7 Runtime error 9 ms 9440 KB Execution killed with signal 11
8 Incorrect 2 ms 4180 KB Output isn't correct
9 Runtime error 10 ms 9424 KB Execution killed with signal 11
10 Correct 2 ms 4180 KB Output is correct
11 Runtime error 8 ms 9428 KB Execution killed with signal 11
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 4308 KB Output is correct
2 Runtime error 8 ms 9436 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 9888 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 13 ms 11460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 9000 KB Output is correct
2 Runtime error 37 ms 22312 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 15628 KB Output is correct
2 Incorrect 74 ms 26500 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 91 ms 41788 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 212 ms 47328 KB Output is correct
2 Correct 431 ms 104340 KB Output is correct
3 Runtime error 172 ms 70948 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 286 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -