답안 #408858

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
408858 2021-05-19T18:09:46 Z Jasiekstrz Islands (IOI08_islands) C++17
90 / 100
1048 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);
	}
	vector<long long> wc(cycle.size());
	for(size_t i=0;i<cycle.size();i++)
	{
		auto ww=dfs_tree(cycle[i],0);
		ans=max(ans,ww.fi);
		wc[i]=ww.se;
	}
	long long mx=0,mn=0;
	for(size_t i=0;i<cycle.size();i++)
	{
		ans=max({ans,mx+wc[i],mn+wc[i]});
		mx=max(mx,wc[i])+l[cycle[i]];
		mn=max(mn,wc[i]+l_all)-l[cycle[i]];
	}
	//cerr<<"t: ";
	//for(auto v:t)
	//	cerr<<v<<" ";
	//cerr<<"\ncycle: ";
	//for(auto v:cycle)
	//	cerr<<v<<" ";
	//cerr<<"\nwc: ";
	//for(auto v:wc)
	//	cerr<<v<<" ";
	//cerr<<"\n"<<ans<<"\n\n";
	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 14 ms 23756 KB Output is correct
2 Correct 15 ms 23756 KB Output is correct
3 Correct 14 ms 23808 KB Output is correct
4 Correct 14 ms 23764 KB Output is correct
5 Correct 15 ms 23824 KB Output is correct
6 Correct 14 ms 23756 KB Output is correct
7 Correct 15 ms 23832 KB Output is correct
8 Correct 15 ms 23800 KB Output is correct
9 Correct 14 ms 23824 KB Output is correct
10 Correct 14 ms 23756 KB Output is correct
11 Correct 14 ms 23804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23876 KB Output is correct
2 Correct 15 ms 23892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 23956 KB Output is correct
2 Correct 16 ms 24268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 24840 KB Output is correct
2 Correct 31 ms 27352 KB Output is correct
3 Correct 25 ms 25220 KB Output is correct
4 Correct 22 ms 24524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 28776 KB Output is correct
2 Correct 50 ms 31200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 38996 KB Output is correct
2 Correct 102 ms 42688 KB Output is correct
3 Correct 127 ms 52932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 48832 KB Output is correct
2 Correct 210 ms 73628 KB Output is correct
3 Correct 224 ms 77632 KB Output is correct
4 Correct 295 ms 99792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 347 ms 95240 KB Output is correct
2 Correct 773 ms 131072 KB Output is correct
3 Correct 298 ms 73888 KB Output is correct
4 Correct 399 ms 109216 KB Output is correct
5 Correct 403 ms 113716 KB Output is correct
6 Correct 1048 ms 89312 KB Output is correct
7 Correct 439 ms 131072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 428 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -