답안 #1102749

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1102749 2024-10-18T18:51:47 Z _rain_ Islands (IOI08_islands) C++14
60 / 100
2000 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],en[N+2],l[N+2],vis[N+2],n,cha[N+2],arr[N+2],sz=0,times=0;
ll dep[N+2]={},pre[N+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;
}
void giai(){
	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;++i){
		for (int j=i+1;j<=i+sz-1;++j){
			addmore=max(addmore,dep[i]+dep[j]+pre[j]-pre[i]);
		}
	}
	ans+=addmore;
}

int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
  
	cin>>n;
	for (int i=1; i<=n; ++i){
		cin>>en[i]>>l[i];
		add_edge(i,en[i],l[i]);
	}
	for (int i=1; i<=n; ++i){
		if (!vis[i]){
			sz = times = 0;
			search(i,i);
			giai();
		}
	}
	cout<<ans;
	exit(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 41044 KB Output is correct
2 Correct 7 ms 41044 KB Output is correct
3 Correct 7 ms 41044 KB Output is correct
4 Correct 7 ms 41044 KB Output is correct
5 Correct 7 ms 41120 KB Output is correct
6 Correct 7 ms 41044 KB Output is correct
7 Correct 7 ms 41044 KB Output is correct
8 Correct 7 ms 41092 KB Output is correct
9 Correct 7 ms 41044 KB Output is correct
10 Correct 7 ms 41140 KB Output is correct
11 Correct 9 ms 41044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 41208 KB Output is correct
2 Correct 9 ms 41044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 41112 KB Output is correct
2 Correct 12 ms 41556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 42068 KB Output is correct
2 Correct 183 ms 44504 KB Output is correct
3 Correct 15 ms 42068 KB Output is correct
4 Correct 11 ms 41556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 313 ms 47692 KB Output is correct
2 Correct 437 ms 50056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 61444 KB Output is correct
2 Execution timed out 2077 ms 64556 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1103 ms 72148 KB Output is correct
2 Execution timed out 2072 ms 102484 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 108460 KB Output is correct
2 Runtime error 391 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 270 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -