Submission #408876

# Submission time Handle Problem Language Result Execution time Memory
408876 2021-05-19T18:29:33 Z Jasiekstrz Islands (IOI08_islands) C++17
90 / 100
1045 ms 131076 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
vector<pair<int,int>> e[N+10];
int f[N+10];
int l[N+10];
bool vis_fill[N+10];
bool vis_s[N+10];
bool on_cycle[N+10];
vector<int> t;
void dfs_fill(int x)
{
	t.push_back(x);
	vis_fill[x]=true;
	for(auto v:e[x])
	{
		if(!vis_fill[v.fi])
			dfs_fill(v.fi);
	}
	return;
}
pair<long long,long long> dfs_tree(int x)
{
	on_cycle[x]=true;
	pair<long long,long long> ans={0,0};
	for(auto v:e[x])
	{
		if(on_cycle[v.fi])
			continue;
		auto tmp=dfs_tree(v.fi);
		ans.fi=max({ans.fi,tmp.fi,ans.se+tmp.se+v.se});
		ans.se=max(ans.se,tmp.se+v.se);
	}
	ans.fi=max(ans.fi,ans.se);
	return ans;
}
long long solve()
{
	long long ans=0;
	int c;
	for(int i=t[0];true;i=f[i])
	{
		if(vis_s[i])
		{
			c=i;
			break;
		}
		vis_s[i]=true;
	}
	long long l_all=l[c];
	on_cycle[c]=true;
	for(int i=f[c];i!=c;l_all+=l[i],i=f[i])
		on_cycle[i]=true;
	long long mx=0,mn=0;
	int x=c;
	do{
		x=f[x];
		auto ww=dfs_tree(x);
		ans=max(ans,ww.fi);
		ans=max({ans,mx+ww.se,mn+ww.se});
		mx=max(mx,ww.se)+l[x];
		mn=max(mn,ww.se+l_all)-l[x];
	}while(x!=c);
	return ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int a,b;
		cin>>a>>b;
		f[i]=a;
		l[i]=b;
		e[i].emplace_back(a,b);
		e[a].emplace_back(i,b);
	}
	long long ans=0;
	for(int i=1;i<=n;i++)
	{
		if(!vis_fill[i])
		{
			t.clear();
			dfs_fill(i);
			ans+=solve();
		}
	}
	cout<<ans<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 16 ms 23756 KB Output is correct
2 Correct 15 ms 23768 KB Output is correct
3 Correct 15 ms 23824 KB Output is correct
4 Correct 14 ms 23800 KB Output is correct
5 Correct 15 ms 23792 KB Output is correct
6 Correct 15 ms 23720 KB Output is correct
7 Correct 15 ms 23736 KB Output is correct
8 Correct 15 ms 23808 KB Output is correct
9 Correct 15 ms 23800 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23816 KB Output is correct
2 Correct 14 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23944 KB Output is correct
2 Correct 16 ms 24204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24864 KB Output is correct
2 Correct 32 ms 26952 KB Output is correct
3 Correct 26 ms 25300 KB Output is correct
4 Correct 20 ms 24560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 27932 KB Output is correct
2 Correct 51 ms 29956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 35676 KB Output is correct
2 Correct 106 ms 38180 KB Output is correct
3 Correct 122 ms 44488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 43796 KB Output is correct
2 Correct 210 ms 60020 KB Output is correct
3 Correct 221 ms 63084 KB Output is correct
4 Correct 267 ms 76152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 344 ms 81304 KB Output is correct
2 Correct 823 ms 100116 KB Output is correct
3 Correct 294 ms 63244 KB Output is correct
4 Correct 389 ms 88520 KB Output is correct
5 Correct 384 ms 89908 KB Output is correct
6 Correct 1045 ms 72720 KB Output is correct
7 Correct 434 ms 105004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 421 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -