Submission #1102841

# Submission time Handle Problem Language Result Execution time Memory
1102841 2024-10-19T04:50:51 Z _rain_ Islands (IOI08_islands) C++14
70 / 100
494 ms 131072 KB
#include<bits/stdc++.h>
using namespace std;
 
#define name "main"
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<int,ll> iil;
#define fi first
#define se second
 
const int N = 1000000;
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];
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;
		ke[i].push_back({en,l});
		ke[en].push_back({i,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);
			}
			deque<iil> q;
			ll sum = 0;
			for (int i = 1; i <= sz*2; ++i){
				int x = (i <= sz ? dep[i] : dep[i - sz]);
				sum += (i <= sz ? cost[i] : cost[i - sz]);
				while (q.size() && q.front().fi < i - sz + 1) q.pop_front();
				if (q.size()){
					addmore = max(addmore,sum+x+q.front().se);
				}
				ll c = x - sum;
				while (q.size() && q.back().se <= c) q.pop_back();
				q.push_back({i,c});
			}
			ans+=addmore;
		}
	}
	cout<<ans;
	exit(0);
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37712 KB Output is correct
2 Correct 7 ms 37712 KB Output is correct
3 Correct 7 ms 37712 KB Output is correct
4 Correct 8 ms 37712 KB Output is correct
5 Correct 7 ms 37712 KB Output is correct
6 Correct 7 ms 37712 KB Output is correct
7 Correct 6 ms 37568 KB Output is correct
8 Correct 6 ms 37712 KB Output is correct
9 Correct 6 ms 37712 KB Output is correct
10 Correct 6 ms 37712 KB Output is correct
11 Correct 7 ms 37796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37712 KB Output is correct
2 Correct 6 ms 37712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 37880 KB Output is correct
2 Correct 7 ms 37968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 38480 KB Output is correct
2 Correct 16 ms 40672 KB Output is correct
3 Incorrect 12 ms 38480 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 20 ms 43600 KB Output is correct
2 Correct 27 ms 45460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 49224 KB Output is correct
2 Correct 59 ms 55584 KB Output is correct
3 Correct 78 ms 67916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 55756 KB Output is correct
2 Correct 129 ms 84188 KB Output is correct
3 Correct 150 ms 93708 KB Output is correct
4 Correct 179 ms 112968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 177 ms 81352 KB Output is correct
2 Runtime error 494 ms 131072 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 290 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -