Submission #386425

# Submission time Handle Problem Language Result Execution time Memory
386425 2021-04-06T14:10:39 Z kshitij_sodani Islands (IOI08_islands) C++14
80 / 100
729 ms 131076 KB
//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first 
#define b second
#define endl '\n'
 
 
llo n;
vector<pair<int,int>> adj[1000001];

int par[1000001];
int find(int no){
	if(par[no]==no){
		return no;
	}
	par[no]=find(par[no]);
	return par[no];
}
llo dp[1000001][2];
pair<int,int> cur;
vector<pair<int,int>> pp;
vector<pair<int,int>> xx;

bool vis[1000001];
 
vector<int> tt;
void dfs(int no,int par2=-1){
	
	if(no==cur.b){
		pp=xx;
	}
	tt.pb(no);
	for(auto j:adj[no]){
		/*if(j.a==cur.a and no==cur.b){
			continue;
		}
		if(j.a==cur.b and no==cur.a){
			continue;
		}*/
		
		if(j.a!=par2){
			xx.pb({j.a,j.b});
			dfs(j.a,no);
			xx.pop_back();
		}
	}
}
 
llo maa=0;
 
void dfs2(llo no,llo par2=-1){
	dp[no][0]=0;
	dp[no][1]=0;
	//vector<llo> ss;
	//llo su=0;
 	llo ma=0;
 	llo ma2=0;
	for(auto j:adj[no]){
		if(vis[j.a]){
			continue;
		}
		if(j.a!=par2){
			dfs2(j.a,no);
	//		dp[no][0]=max(dp[no][0],j.b+dp[j.a][0]);

			if(dp[j.a][0]+j.b>=ma){
				ma2=ma;
				ma=dp[j.a][0]+j.b;
			}
			else if(dp[j.a][0]+j.b>=ma2){
				ma2=dp[j.a][0]+j.b;
			}
			//su+=dp[j.a][1];
			//ss.pb(dp[j.a][0]+j.b);
		}
	}
	//sort(ss.begin(),ss.end());
	//reverse(ss.begin(),ss.end());
	dp[no][1]=ma+ma2;
	dp[no][0]=ma;


	/*for(llo i=0;i<ss.size();i++){
		if(i==2){
			break;
		}
 
		if(i==0){
			if(ss[i]>=0){
				dp[no][0]+=ss[i];
			}
		}
		if(ss[i]>=0){
			dp[no][1]+=ss[i];
		}
	}*/
	//dp[no][1]=max(dp[no][1],dp[no][0]);
	maa=max(maa,dp[no][1]);
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n;
	for(llo i=0;i<n;i++){
		par[i]=i;
	}
	vector<pair<pair<int,int>,llo>> ed;
	for(llo i=0;i<n;i++){
		llo aa,cc;
		cin>>aa>>cc;
		aa--;
		llo x=find(i);
		llo y=find(aa);
		if(x!=y){
			par[x]=y;
			adj[i].pb({aa,cc});
			adj[aa].pb({i,cc});
		}
		else{
			ed.pb({{i,aa},cc});
		}
	}
	llo ans=0;
	for(auto j:ed){
		cur=j.a;
		llo cost=0;
		tt.clear();
		dfs(j.a.a);
		pp.pb({cur.a,j.b});
		for(auto i:pp){
			vis[i.a]=1;
		}
		vector<pair<pair<int,int>,llo>> pc;
		pc.pb(j);
 
		for(auto i:tt){
			for(auto k:adj[i]){
				if(vis[k.a]+vis[i]<2){
					//adj2[i].pb(k);
				}
				else{
					if(k.a<i){
						pc.pb({{i,k.a},k.b});
					}
				}
			}
		}

		maa=0;
		for(auto i:tt){
				for(auto k:adj[i]){
					if(vis[k.a]+vis[i]<2){
					//	adj2[i].pb(k);
					}
					else{
 
					}
				}
			}
		for(auto i:pp){
			dfs2(i.a);
		}
		cost=max(cost,maa);
	
		llo xx=pp.size();
		multiset<llo> zz;
		for(int i=0;i<xx;i++){
			pp.pb(pp[i]);
		}
		vector<llo> xd;
		llo su2=0;
		for(llo i=0;i<pp.size();i++){
			if(i>=xx){
				auto jj=zz.find(xd[i-xx]);
				zz.erase(jj);
			}
			su2+=pp[i].b;
			if(zz.size()){
				auto jj=zz.end();
				jj--;
				cost=max(cost,dp[pp[i].a][0]+su2+(*jj));
			}
			if(i<xx){
				xd.pb(dp[pp[i].a][0]-su2);
				zz.insert(xd.back());
			}
		}
		ans+=cost;
 
 
 
 
 
 
 
 
	}
	cout<<ans<<endl;
 
 
 
 
 
 
 
	return 0;
}
 

Compilation message

islands.cpp: In function 'int main()':
islands.cpp:176:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  176 |   for(llo i=0;i<pp.size();i++){
      |               ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23792 KB Output is correct
2 Correct 16 ms 23916 KB Output is correct
3 Correct 18 ms 23916 KB Output is correct
4 Correct 16 ms 23788 KB Output is correct
5 Correct 17 ms 23788 KB Output is correct
6 Correct 16 ms 23788 KB Output is correct
7 Correct 17 ms 23788 KB Output is correct
8 Correct 16 ms 23788 KB Output is correct
9 Correct 15 ms 23788 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
11 Correct 17 ms 23916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 24044 KB Output is correct
2 Correct 17 ms 24044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 23916 KB Output is correct
2 Correct 18 ms 24428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 25196 KB Output is correct
2 Correct 38 ms 28624 KB Output is correct
3 Correct 26 ms 25324 KB Output is correct
4 Correct 22 ms 24556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 30392 KB Output is correct
2 Correct 62 ms 32208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 39520 KB Output is correct
2 Correct 140 ms 49412 KB Output is correct
3 Correct 178 ms 65344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 186 ms 48328 KB Output is correct
2 Correct 285 ms 89460 KB Output is correct
3 Correct 401 ms 101260 KB Output is correct
4 Correct 431 ms 131072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 90568 KB Output is correct
2 Runtime error 729 ms 131076 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 387 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -