답안 #408929

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
408929 2021-05-19T19:52:11 Z Jasiekstrz Islands (IOI08_islands) C++17
90 / 100
1094 ms 131076 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(auto 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(auto v:e[x])
	{
		if(on_cycle[v])
			continue;
		auto 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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 15 ms 23792 KB Output is correct
3 Correct 13 ms 23756 KB Output is correct
4 Correct 13 ms 23756 KB Output is correct
5 Correct 14 ms 23756 KB Output is correct
6 Correct 16 ms 23756 KB Output is correct
7 Correct 13 ms 23752 KB Output is correct
8 Correct 13 ms 23756 KB Output is correct
9 Correct 13 ms 23756 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23884 KB Output is correct
2 Correct 14 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23788 KB Output is correct
2 Correct 16 ms 24140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 24524 KB Output is correct
2 Correct 32 ms 26552 KB Output is correct
3 Correct 24 ms 24872 KB Output is correct
4 Correct 19 ms 24268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 27340 KB Output is correct
2 Correct 48 ms 28996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 34260 KB Output is correct
2 Correct 97 ms 38168 KB Output is correct
3 Correct 117 ms 44100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 153 ms 40916 KB Output is correct
2 Correct 198 ms 58188 KB Output is correct
3 Correct 220 ms 65412 KB Output is correct
4 Correct 275 ms 77360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 379 ms 73400 KB Output is correct
2 Correct 827 ms 99724 KB Output is correct
3 Correct 298 ms 57176 KB Output is correct
4 Correct 402 ms 84800 KB Output is correct
5 Correct 417 ms 86504 KB Output is correct
6 Correct 1094 ms 66616 KB Output is correct
7 Correct 419 ms 104708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 424 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -