Submission #386390

# Submission time Handle Problem Language Result Execution time Memory
386390 2021-04-06T13:51:20 Z kshitij_sodani Islands (IOI08_islands) C++14
9 / 100
381 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<llo,llo>> adj[1000001];
vector<pair<llo,llo>> adj2[1000001];

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


vector<llo> tt;
void dfs(llo no,llo 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 vis[1000001];
llo maa=0;

void dfs2(llo no,llo par2=-1){
	dp[no][0]=0;
	dp[no][1]=0;
	vector<llo> ss;
	llo su=0;

	for(auto j:adj2[no]){
		if(j.a!=par2){
			dfs2(j.a,no);
	//		dp[no][0]=max(dp[no][0],j.b+dp[j.a][0]);
			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]=0;
	dp[no][0]=0;
	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<llo,llo>,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});
			//cout<<i<<":"<<aa<<":"<<cc<<endl;
		}
		
	}
	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<llo,llo>,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});
					}
				}
			}
		}
	/*	for(auto i:pc){
			cout<<i.a.a<<","<<i.a.b<<","<<i.b<<endl;
		}*/
		if(pp.size()==2){
			llo zz5=0;
			for(auto i:tt){
				for(auto k:adj[i]){
					//cout<<i<<":"<<k.a<<":"<<k.b<<endl;
					if(vis[k.a]+vis[i]<2){
						adj2[i].pb(k);
					}
					else{
						//if(k.a<i){
						zz5=max(zz5,k.b);

						//}
					}
				}
			}
			maa=0;
			for(auto i:pp){
				dfs2(i.a);
			}

			zz5=max(zz5,j.b);
			cost=max(cost,maa);
			//cost=max(cost,dp[pp[0].a][1]+dp[pp[1].a][1]);
			cost=max(cost,dp[pp[0].a][0]+dp[pp[1].a][0]+zz5);
			ans+=cost;
			//cout<<cost<<":"<<endl;
			continue;
		}
		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);
		vector<pair<llo,llo>> pp2=pp;
		for(auto i:pp){
			pp2.pb(i);
		}
		llo xx=pp.size();
		multiset<llo> zz;
		vector<llo> xd;
		llo su2=0;
		/*for(auto i:pp2){
			cout<<i.a<<"<"<<i.b<<endl;
		}*/
		for(llo i=0;i<pp2.size();i++){
			if(i>=xx){
				auto jj=zz.find(xd[i-xx]);
				zz.erase(jj);
			}
			su2+=pp2[i].b;
			if(xd.size()){
				auto jj=xd.end();
				jj--;
				cost=max(cost,dp[pp2[i].a][0]+su2+(*jj));
			}
			xd.pb(dp[pp2[i].a][0]-su2);
			zz.insert(xd.back());
		}
		/*for(auto ii:pc){
			for(auto i:tt){
				adj2[i].clear();
			}
			for(auto i:tt){
				for(auto k:adj[i]){
					if(vis[k.a]+vis[i]<2){
						adj2[i].pb(k);
					}
					else{
						if(k.a==ii.a.a and i==ii.a.b){
							continue;
						}
						if(k.a==ii.a.b and i==ii.a.a ){
							continue;
						}
					}
				}
			}
			maa=0;
			for(auto i:pc){
				if(i.a.a!=ii.a.a or i.a.b!=ii.a.b){
					adj2[i.a.a].pb({i.a.b,i.b});
					adj2[i.a.b].pb({i.a.a,i.b});
				}
			}
			dfs2(tt[0]);
			cost=max(cost,dp[tt[0]][1]);
			cost=max(cost,maa);
			//cout<<ii.a.a<<"<"<<ii.a.b<<","<<ii.b<<endl;
			//cout<<dp[tt[0]][1]<<endl;
		}*/
		ans+=cost;







		continue;
		/*for(auto i:tt){
			cout<<i<<",";
		}
		cout<<endl;*/
	/*	llo kk=0;
		vector<llo> re;
		for(auto i:pp){
			dfs2(i.a);
			kk+=dp[i.a][1];
			llo ac=0;
			for(auto j:adj2[i.a]){
				ac+=dp[j.a][1];
			}
			re.pb(ac);
			//cout<<i.a<<":"<<1<<endl;
		}*/
		
	/*	for(auto j:pp){
			cout<<j.a<<":"<<j.b<<endl;
			cout<<dp[j.a][0]<<"."<<dp[j.a][1]<<endl;
		}*/
	/*	cost=max(cost,kk);

		
		
		llo xx=pp.size();

		for(llo i=0;i<pp.size();i++){
			llo ind=i;
			llo su=0;
			for(llo cot=0;cot<xx-1;cot++){
				ind++;
				if(ind==xx){
					ind=0;
				}
				//cout<<i<<".."<<ind<<endl;
				su+=pp[ind].b;
				llo cur2=su;
				cur2+=dp[pp[i].a][0]-dp[pp[i].a][1];
				cur2+=dp[pp[ind].a][0]-dp[pp[ind].a][1];
				cost=max(cost,cur2+kk);
				su-=(dp[pp[ind].a][1]);
				su+=re[ind];
			}
		}
		for(auto i:pp){
			vis[i.a]=0;
		}
		ans+=cost;*/
	}
	cout<<ans<<endl;





 
 
	return 0;
}
 

Compilation message

islands.cpp: In function 'void dfs2(llo, llo)':
islands.cpp:72:15: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |  for(llo i=0;i<ss.size();i++){
      |              ~^~~~~~~~~~
islands.cpp: In function 'int main()':
islands.cpp:196:16: warning: comparison of integer expressions of different signedness: 'llo' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  196 |   for(llo i=0;i<pp2.size();i++){
      |               ~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 47340 KB Output isn't correct
2 Incorrect 30 ms 47340 KB Output isn't correct
3 Incorrect 30 ms 47468 KB Output isn't correct
4 Correct 30 ms 47340 KB Output is correct
5 Incorrect 30 ms 47340 KB Output isn't correct
6 Incorrect 30 ms 47340 KB Output isn't correct
7 Incorrect 31 ms 47340 KB Output isn't correct
8 Incorrect 34 ms 47340 KB Output isn't correct
9 Incorrect 30 ms 47340 KB Output isn't correct
10 Correct 30 ms 47340 KB Output is correct
11 Correct 33 ms 47360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 31 ms 47596 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 32 ms 47468 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 43 ms 49644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 76 ms 58784 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 161 ms 79748 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 279 ms 94800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 378 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 381 ms 131076 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -