Submission #233033

# Submission time Handle Problem Language Result Execution time Memory
233033 2020-05-19T00:51:47 Z FieryPhoenix Islands (IOI08_islands) C++11
80 / 100
890 ms 131076 KB
#pragma comment(linker, "/STACK: 2000000")
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007
#define f first
#define s second

int N;
vector<pair<int,int>>adj[1000001];
bool vis[1000001];
ll ans;
int C;
int cycle[1000001],adjDis[1000001];
ll deepest[1000001];
int bad,bad2;
ll maxCur;
map<pair<int,int>,int>mp;
ll pref[1000001],suff[1000001],pref2[1000001],suff2[1000001];

void findCycle(int node, int par){
  //cout<<"findCycle: "<<node<<endl;
  vis[node]=true;
  for (auto p:adj[node]){
    if (C==0 && vis[p.first] && p.first!=par){ //found cycle
      C=0;
      //cout<<"found cycle"<<endl;
      int cur=node;
      while (cur!=p.first){
	cycle[C]=cur;
	adjDis[C]=deepest[cur]; //fromDis
	C++;
	cur=pref[cur]; //from
      }
      cycle[C]=p.first;
      adjDis[C]=p.second;
      C++;
    }
    else if (!vis[p.first]){
      pref[p.first]=node; //from
      deepest[p.first]=p.second; //fromDis
      findCycle(p.first,node);
    }
  }
}

ll maxDis,maxNode;

queue<pair<pair<int,int>,ll>>q;

void findFarthest(int node0, int par0, ll dis0){
  maxDis=-1; maxNode=-1;
  q.push({{node0,par0},dis0});
  while (!q.empty()){
    pair<pair<int,int>,ll>P=q.front();
    q.pop();
    int node=P.f.f;
    int par=P.f.s;
    ll dis=P.s;
    if (dis>maxDis){
      maxDis=dis;
      maxNode=node;
    }
    for (auto p:adj[node]){
      if (p.first!=par && p.first!=bad && p.first!=bad2)
        q.push({{p.first,node},dis+p.second});
    }
  }
}
    

ll findDiameter(int node){
  findFarthest(node,node,0);
  int newNode=maxNode;
  findFarthest(newNode,newNode,0);
  return maxDis;
}

int main()
{
  //ios_base::sync_with_stdio(0);cin.tie(0);
  //freopen (".in","r",stdin);
  //freopen (".out","w",stdout);
  scanf("%d",&N);
  for (int i=1;i<=N;i++){
    int Y,L;
    scanf("%d%d",&Y,&L);
    int x=i,y=Y;
    if (x>y) swap(x,y);
    mp[{x,y}]=max(mp[{x,y}],L);
  }
  for (auto it:mp){
    //cout<<"edge: "<<it.first.first<<' '<<it.first.second<<endl;
    adj[it.first.first].push_back({it.first.second,it.second});
    adj[it.first.second].push_back({it.first.first,it.second});
  }
  mp.clear();
  for (int i=1;i<=N;i++)
    if (!vis[i]){
      //cout<<"Component: "<<i<<endl;
      C=0;
      findCycle(i,i);
      /*
      cout<<"Cycle: ";
      for (int i=0;i<C;i++)
      	cout<<cycle[i]<<' '<<adjDis[i]<<endl;;
      cout<<endl;
      */
      if (C==0){
	//cout<<"multiEdge: "<<findDiameter(i)<<endl;
	ans+=findDiameter(i);
	continue;
      }
      maxCur=0;
      for (int i=0;i<C;i++){
	pref[i]=0;
	suff[i]=0;
	pref2[i]=0;
	suff2[i]=0;
	int inFront=cycle[(i+1)%C];
	int inBack=cycle[(i-1+C)%C];
	//adjDis[i]=mp[{min(cycle[i],inFront),max(cycle[i],inFront)}];
	bad=inFront;
	bad2=inBack;
	maxCur=max(maxCur,findDiameter(cycle[i]));
	findFarthest(cycle[i],cycle[i],0);
	deepest[i]=maxDis;
      }
      for (int i=0;i<C;i++){
	if (i>0){
	  pref[i]=pref[i-1]+adjDis[i-1];
	  pref2[i]=max(pref2[i-1],pref[i]+deepest[i]);
	}
	else
	  pref2[i]=deepest[i];
      }
      /*
      cout<<"deepest: "<<' ';
      for (int x:deepest)
	cout<<x<<' ';
      cout<<endl;
      cout<<"adjDis: "<<' ';
      for (int x:adjDis)
	cout<<x<<' ';
      cout<<endl;
      cout<<"pref: ";
      for (int i=0;i<C;i++)
	cout<<pref[i]<<' ';
      cout<<endl;
      */
      for (int i=C-1;i>=0;i--){
	if (i+1<C){
	  suff[i]=suff[i+1]+adjDis[i];
	  suff2[i]=max(suff2[i+1],suff[i]+deepest[i]);
	}
	else
	  suff2[i]=deepest[i];
      }
      for (int i=0;i+1<C;i++){
	//cout<<i<<": "<<adjDis.back()<<' '<<pref2[i]<<' '<<suff2[i+1]<<endl;
	maxCur=max(maxCur,adjDis[C-1]+pref2[i]+suff2[i+1]);
      }
      ll sweep=0;
      for (int i=0;i<C;i++){
	maxCur=max(maxCur,sweep+deepest[i]);
	sweep=max(sweep,(ll)deepest[i]);
	sweep+=adjDis[i];
      }
      ans+=maxCur;
      //cout<<"maxCur: "<<maxCur<<endl;
    }
  printf("%lld\n",ans);
}

Compilation message

islands.cpp:1:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/STACK: 2000000")
 
islands.cpp: In function 'int main()':
islands.cpp:92:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&N);
   ~~~~~^~~~~~~~~
islands.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&Y,&L);
     ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 23808 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
3 Correct 18 ms 23968 KB Output is correct
4 Correct 18 ms 23808 KB Output is correct
5 Correct 18 ms 23808 KB Output is correct
6 Correct 17 ms 23808 KB Output is correct
7 Correct 17 ms 23808 KB Output is correct
8 Correct 18 ms 23808 KB Output is correct
9 Correct 18 ms 23936 KB Output is correct
10 Correct 18 ms 23808 KB Output is correct
11 Correct 18 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 24064 KB Output is correct
2 Correct 18 ms 23936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 24064 KB Output is correct
2 Correct 21 ms 24568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 25728 KB Output is correct
2 Correct 65 ms 29080 KB Output is correct
3 Correct 38 ms 26232 KB Output is correct
4 Correct 28 ms 25088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 31224 KB Output is correct
2 Correct 104 ms 36984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 48632 KB Output is correct
2 Correct 206 ms 55416 KB Output is correct
3 Correct 259 ms 64888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 356 ms 71068 KB Output is correct
2 Correct 471 ms 98040 KB Output is correct
3 Correct 548 ms 110712 KB Output is correct
4 Correct 662 ms 128504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 850 ms 131076 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 890 ms 131072 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -