Submission #408931

# Submission time Handle Problem Language Result Execution time Memory
408931 2021-05-19T19:57:11 Z Jasiekstrz Islands (IOI08_islands) C++17
100 / 100
1059 ms 131072 KB
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e6;
vector<int> e[N+10];
int f[N+10];
int l[N+10];
bitset<N+10> vis_fill;
bitset<N+10> vis_s;
bitset<N+10> on_cycle;
vector<int> t;
void dfs_fill(int x)
{
	t.push_back(x);
	vis_fill[x]=true;
	for(int &v:e[x])
	{
		if(!vis_fill[v])
			dfs_fill(v);
	}
	return;
}
long long aa;
long long dfs_tree(int x)
{
	on_cycle[x]=true;
	long long ans=0;
	for(int &v:e[x])
	{
		if(on_cycle[v])
			continue;
		long long tmp=dfs_tree(v);
		aa=max(aa,ans+tmp+(v==f[x] ? l[x]:l[v]));
		ans=max(ans,tmp+(v==f[x] ? l[x]:l[v]));
	}
	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++)
	{
		cin>>f[i]>>l[i];
		e[i].push_back(f[i]);
		e[f[i]].push_back(i);
	}
	long long ans=0;
	for(int j=1;j<=n;j++)
	{
		if(!vis_fill[j])
		{
			t.clear();
			dfs_fill(j);
			long long ans_s=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];
				aa=0;
				long long ww=dfs_tree(x);
				ans_s=max(ans_s,aa);
				ans_s=max({ans_s,mx+ww,mn+ww});
				mx=max(mx,ww)+l[x];
				mn=max(mn,ww+l_all)-l[x];
			}while(x!=c);
			ans+=ans_s;
		}
	}
	cout<<ans<<"\n";
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 14 ms 23768 KB Output is correct
3 Correct 14 ms 23828 KB Output is correct
4 Correct 13 ms 23816 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 14 ms 23756 KB Output is correct
7 Correct 14 ms 23804 KB Output is correct
8 Correct 14 ms 23748 KB Output is correct
9 Correct 16 ms 23756 KB Output is correct
10 Correct 13 ms 23808 KB Output is correct
11 Correct 14 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 14 ms 23884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23832 KB Output is correct
2 Correct 15 ms 24140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 24672 KB Output is correct
2 Correct 31 ms 26616 KB Output is correct
3 Correct 25 ms 24900 KB Output is correct
4 Correct 20 ms 24396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 27656 KB Output is correct
2 Correct 49 ms 29380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 34524 KB Output is correct
2 Correct 99 ms 38592 KB Output is correct
3 Correct 123 ms 44464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 41284 KB Output is correct
2 Correct 198 ms 58588 KB Output is correct
3 Correct 230 ms 65984 KB Output is correct
4 Correct 268 ms 77772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 348 ms 73196 KB Output is correct
2 Correct 800 ms 99180 KB Output is correct
3 Correct 293 ms 56744 KB Output is correct
4 Correct 377 ms 84480 KB Output is correct
5 Correct 380 ms 86196 KB Output is correct
6 Correct 1037 ms 66252 KB Output is correct
7 Correct 407 ms 104200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 462 ms 131072 KB Output is correct
2 Correct 436 ms 115316 KB Output is correct
3 Correct 443 ms 131072 KB Output is correct
4 Correct 391 ms 78244 KB Output is correct
5 Correct 405 ms 98052 KB Output is correct
6 Correct 367 ms 84660 KB Output is correct
7 Correct 1059 ms 82004 KB Output is correct