답안 #335494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335494 2020-12-12T19:07:25 Z keta_tsimakuridze Islands (IOI08_islands) C++14
50 / 100
2000 ms 131076 KB
#include<bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N=1e6+5;
long long mx,h,ans1,dm[N],n,m,a[N],b[N],fix[N],sz,mx1,c,k,u,ans,v;
vector<pair<int,long long> >V[N],cycle;
void dfs(int u,int p,long long h,int S){
	if(!fix[u])fix[u]=1;
	if(h>mx) {
		mx=h; c=u;
	}
	for(int i=0;i<V[u].size();i++){
		if((fix[V[u][i].f]!=2 || V[u][i].f==S) && V[u][i].f!=p){
			dfs(V[u][i].f,u,h+V[u][i].s,S);
		}
	}
}
void diameter(int u){
    mx=0;
	dfs(u,0,0,u);dm[u]=mx;
	dfs(c,0,0,u);
	ans1=max(ans1,mx);	
}
int main(){
	cin>>n;
	for(k=1;k<=n;k++){
		cin>>a[k]>>b[k];
		V[a[k]].push_back({k,b[k]});
		V[k].push_back({a[k],b[k]});
	}
	for(k=1;k<=n;k++){
		if(!fix[k]){   
			u=k;
			ans1=0; sz=0; cycle.clear();
			while(!fix[u]){
				fix[u]=1;
				u=a[u];
			} 
			while(fix[u]!=2){
				fix[u]=2;
				u=a[u];
				cycle.push_back({u,b[u]});
				sz+=b[u];
			}
			for(int i=0;i<cycle.size();i++){
				diameter(cycle[i].f);
			} 
			mx=mx1=dm[cycle[0].f]; ans1=max(ans1,sz-cycle[0].s);
			long long bef=cycle[0].s;
			for(int i=1;i<cycle.size();i++){
			
				ans1=max(ans1,bef+mx+dm[cycle[i].f]);
				ans1=max(ans1,sz+mx1-bef+dm[cycle[i].f]);
				mx=max(mx,dm[cycle[i].f]-bef);
				mx1=max(mx1,dm[cycle[i].f]+bef);
				bef+=cycle[i].s;
			}
			ans+=ans1;
		} 
	}
	cout<<ans;
}

Compilation message

islands.cpp: In function 'void dfs(int, int, long long int, int)':
islands.cpp:13:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:46:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |    for(int i=0;i<cycle.size();i++){
      |                ~^~~~~~~~~~~~~
islands.cpp:51:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |    for(int i=1;i<cycle.size();i++){
      |                ~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23788 KB Output is correct
2 Correct 15 ms 23788 KB Output is correct
3 Correct 15 ms 23916 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 15 ms 23788 KB Output is correct
6 Correct 15 ms 23788 KB Output is correct
7 Correct 15 ms 23788 KB Output is correct
8 Correct 15 ms 23788 KB Output is correct
9 Correct 15 ms 23788 KB Output is correct
10 Correct 15 ms 23788 KB Output is correct
11 Correct 15 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 23916 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 24044 KB Output is correct
2 Correct 18 ms 24172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 25196 KB Output is correct
2 Correct 46 ms 27244 KB Output is correct
3 Incorrect 36 ms 25708 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 28552 KB Output is correct
2 Correct 93 ms 32488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 179 ms 40680 KB Output is correct
2 Execution timed out 2076 ms 43100 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 57072 KB Output is correct
2 Correct 385 ms 68868 KB Output is correct
3 Execution timed out 2084 ms 70100 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 826 ms 94552 KB Output is correct
2 Correct 1249 ms 131044 KB Output is correct
3 Correct 653 ms 101228 KB Output is correct
4 Correct 804 ms 124624 KB Output is correct
5 Correct 776 ms 124568 KB Output is correct
6 Incorrect 1732 ms 115052 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 871 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -