답안 #1102842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102842 2024-10-19T04:51:20 Z _rain_ Islands (IOI08_islands) C++14
80 / 100
509 ms 131072 KB
#include<bits/stdc++.h>
using namespace std;
 
#define name "main"
typedef long long ll;
typedef pair<int,int> ii;
#define fi first
#define se second
 
const int N = (int)1e6;
vector<ii> ke[N+2];
int cost[N+2],gi[N+2],vis[N+2],n,cha[N+2],arr[N+2],sz=0,times=0;
ll dep[N*2+2]={},pre[N*2+2]={},f[N*2+2]={};
bool com[N+2];
void add_edge(int u , int v , int w){
	ke[u].push_back({v,w});
	ke[v].push_back({u,w});
}
void search(int u , int p){
	vis[u] = ++times;
	for (auto& x:ke[u]){
		int v=x.fi,w=x.se;
		if (v==p) continue;
		if (!vis[v]) {
			cha[v]=u;
			gi[v]=w;
			search(v,u);
		}
		else{
			if (vis[v]<vis[u]) continue;
			++sz;
			arr[sz]=v;cost[sz]=w;
			for (; v!=u; v=cha[v]){
				++sz;
				arr[sz]=cha[v];
				cost[sz]=gi[v];
			}
		}
	}
	return ;
}
// the component has at most 1 cycle inside ~
ll mx,ans=0;
int pos,root;
void dfs(int u , int p , ll s){
	if (mx < s){
		mx = s;
		pos = u;
	}
	for (auto& x:ke[u]){
		int v=x.fi,w=x.se;
		if ((com[v]&&v!=root)||v==p) continue;
		dfs(v,u,s+w);
	}
	return;
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
 
	cin>>n;
	for (int i=1; i<=n; ++i){
		int en,l; cin>>en>>l;
		add_edge(i,en,l);
	}
	for (int id=1; id<=n; ++id){
		if (!vis[id]){
			sz = times = 0;
			search(id,id);
			ll addmore = -1;
			for (int i = 1; i <= sz; ++i) com[arr[i]] = true;
			for (int i = 1; i <= sz; ++i){
				mx=pos=root=-1;
				dfs(arr[i],0,0);
				dep[i]=mx;
				root=arr[i];
				dfs(pos,0,0);
				addmore = max(addmore,mx);
				pre[i]=pre[i-1]+cost[i];
			}
			for (int i = sz + 1; i <= sz*2; ++i){
				cost[i] = cost[i-sz];
				dep[i] = dep[i-sz];
				pre[i]=pre[i-1]+cost[i];
			}
			for (int i=1; i<=sz*2; ++i) f[i]=dep[i]-pre[i];
			deque<int> q;
			for (int i = 1; i <= sz*2; ++i){
				while (q.size() && q.front()<i-sz+1) q.pop_front();
				if (q.size()){
					addmore = max(addmore,pre[i]+dep[i]+f[q.front()]);
				}
				while (q.size() && f[q.back()]<=f[i]) q.pop_back();	
				q.push_back(i);
			}
			ans+=addmore;
		}
	}
	cout<<ans;
	exit(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 39760 KB Output is correct
2 Correct 6 ms 39760 KB Output is correct
3 Correct 6 ms 40184 KB Output is correct
4 Correct 7 ms 39760 KB Output is correct
5 Correct 6 ms 39760 KB Output is correct
6 Correct 6 ms 39760 KB Output is correct
7 Correct 6 ms 39760 KB Output is correct
8 Correct 6 ms 39760 KB Output is correct
9 Correct 6 ms 39760 KB Output is correct
10 Correct 7 ms 39876 KB Output is correct
11 Correct 8 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 40188 KB Output is correct
2 Correct 7 ms 40200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 40028 KB Output is correct
2 Correct 7 ms 40272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 40784 KB Output is correct
2 Correct 22 ms 42968 KB Output is correct
3 Correct 13 ms 40672 KB Output is correct
4 Correct 10 ms 40272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 44112 KB Output is correct
2 Correct 30 ms 45648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 49624 KB Output is correct
2 Correct 64 ms 59988 KB Output is correct
3 Correct 87 ms 74436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 56136 KB Output is correct
2 Correct 145 ms 92288 KB Output is correct
3 Correct 165 ms 103504 KB Output is correct
4 Correct 201 ms 125064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 83352 KB Output is correct
2 Runtime error 509 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 279 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -