답안 #408896

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
408896 2021-05-19T19:06:47 Z Jasiekstrz Islands (IOI08_islands) C++17
90 / 100
1060 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;
pair<long long,long long> ww[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])
			dfs_fill(v);
	}
	return;
}
void dfs_tree(int x)
{
	on_cycle[x]=true;
	for(auto v:e[x])
	{
		if(!on_cycle[v])
		{
			dfs_tree(v);
			ww[x].fi=max({ww[x].fi,ww[v].fi,ww[x].se+ww[v].se+(v==f[x] ? l[x]:l[v])});
			ww[x].se=max(ww[x].se,ww[v].se+(v==f[x] ? l[x]:l[v]));
		}
	}
	return;
}
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];
		dfs_tree(x);
		ans=max(ans,ww[x].fi);
		ans=max({ans,mx+ww[x].se,mn+ww[x].se});
		mx=max(mx,ww[x].se)+l[x];
		mn=max(mn,ww[x].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);
		e[a].emplace_back(i);
	}
	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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23756 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23872 KB Output is correct
4 Correct 13 ms 23740 KB Output is correct
5 Correct 13 ms 23756 KB Output is correct
6 Correct 13 ms 23828 KB Output is correct
7 Correct 13 ms 23776 KB Output is correct
8 Correct 14 ms 23828 KB Output is correct
9 Correct 13 ms 23756 KB Output is correct
10 Correct 15 ms 23816 KB Output is correct
11 Correct 15 ms 23744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23816 KB Output is correct
2 Correct 15 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23892 KB Output is correct
2 Correct 15 ms 24140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24744 KB Output is correct
2 Correct 31 ms 26944 KB Output is correct
3 Correct 24 ms 25164 KB Output is correct
4 Correct 17 ms 24396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 28132 KB Output is correct
2 Correct 51 ms 30248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 37096 KB Output is correct
2 Correct 97 ms 39488 KB Output is correct
3 Correct 120 ms 48004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 154 ms 45744 KB Output is correct
2 Correct 214 ms 65964 KB Output is correct
3 Correct 224 ms 66464 KB Output is correct
4 Correct 278 ms 85928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 344 ms 73032 KB Output is correct
2 Correct 785 ms 107052 KB Output is correct
3 Correct 293 ms 68120 KB Output is correct
4 Correct 388 ms 100176 KB Output is correct
5 Correct 390 ms 100736 KB Output is correct
6 Correct 1060 ms 80064 KB Output is correct
7 Correct 427 ms 120124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 425 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -