답안 #408866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
408866 2021-05-19T18:19:16 Z Jasiekstrz Islands (IOI08_islands) C++17
90 / 100
1063 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];
void dfs_fill(int x,vector<int> &vec)
{
	vec.push_back(x);
	vis_fill[x]=true;
	for(auto v:e[x])
	{
		if(!vis_fill[v.fi])
			dfs_fill(v.fi,vec);
	}
	return;
}
pair<long long,long long> dfs_tree(int x,int p)
{
	pair<long long,long long> ans={0,0};
	for(auto v:e[x])
	{
		if(on_cycle[v.fi] || v.fi==p)
			continue;
		auto tmp=dfs_tree(v.fi,x);
		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(vector<int> &t)
{
	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];
	vector<int> cycle={c};
	on_cycle[c]=true;
	for(int i=f[c];i!=c;l_all+=l[i],i=f[i])
	{
		on_cycle[i]=true;
		cycle.push_back(i);
	}
	long long mx=0,mn=0;
	for(size_t i=0;i<cycle.size();i++)
	{
		auto ww=dfs_tree(cycle[i],0);
		ans=max(ans,ww.fi);
		ans=max({ans,mx+ww.se,mn+ww.se});
		mx=max(mx,ww.se)+l[cycle[i]];
		mn=max(mn,ww.se+l_all)-l[cycle[i]];
	}
	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])
		{
			vector<int> tmp;
			dfs_fill(i,tmp);
			ans+=solve(tmp);
		}
	}
	cout<<ans<<"\n";
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23756 KB Output is correct
2 Correct 16 ms 23800 KB Output is correct
3 Correct 16 ms 23772 KB Output is correct
4 Correct 16 ms 23748 KB Output is correct
5 Correct 18 ms 23704 KB Output is correct
6 Correct 15 ms 23740 KB Output is correct
7 Correct 16 ms 23804 KB Output is correct
8 Correct 16 ms 23756 KB Output is correct
9 Correct 15 ms 23756 KB Output is correct
10 Correct 16 ms 23816 KB Output is correct
11 Correct 16 ms 23780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23884 KB Output is correct
2 Correct 17 ms 23884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 23856 KB Output is correct
2 Correct 20 ms 24076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 24708 KB Output is correct
2 Correct 32 ms 26836 KB Output is correct
3 Correct 29 ms 25004 KB Output is correct
4 Correct 20 ms 24392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 27844 KB Output is correct
2 Correct 53 ms 29804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 35504 KB Output is correct
2 Correct 100 ms 39468 KB Output is correct
3 Correct 134 ms 47740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 170 ms 43428 KB Output is correct
2 Correct 233 ms 64332 KB Output is correct
3 Correct 223 ms 68624 KB Output is correct
4 Correct 284 ms 84784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 352 ms 79776 KB Output is correct
2 Correct 814 ms 112776 KB Output is correct
3 Correct 297 ms 63032 KB Output is correct
4 Correct 401 ms 94496 KB Output is correct
5 Correct 391 ms 96384 KB Output is correct
6 Correct 1063 ms 71816 KB Output is correct
7 Correct 466 ms 127244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 434 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -