Submission #29625

# Submission time Handle Problem Language Result Execution time Memory
29625 2017-07-20T09:07:17 Z kavun Dreaming (IOI13_dreaming) C++14
0 / 100
85 ms 29784 KB
#include "dreaming.h"
#include <bits/stdc++.h>
#define pb push_back

using namespace std;
typedef pair <int,int> ii;
vector <ii> adj[100010];
int ans = 1e9, p[100010], endpoint1, endpoint2, ep3, ep4, cen1, cen2;
bool mk[100010];
int pf1[100010], pf2[100010];

void dfs(int v, int par, int len, int mx, int& mxver)
{
  mk[v] = true;
  if(len > mx)
    {
      mx = len;
      mxver = v;
    }
  for(int i = 0; i < adj[v].size(); i++)
    {
      int u = adj[v][i].first;
      if(u != par)
	dfs(u,v,len+adj[v][i].second,mx,mxver);
    }
}

int recoverdiam(int v, int par, int dest, int len)
{
  p[v] = par;
  if(v == dest)
    return len;
  for(int i = 0; i < adj[v].size(); i++)
    {
      if(adj[v][i].first != par)
	recoverdiam(adj[v][i].first,v,dest, len + adj[v][i].second);
    }
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {

  for(int i = 0;i < M; i++)
    {
      int v = A[i], u = B[i], len = T[i];
      adj[v].pb(ii(u,len));
      adj[u].pb(ii(v,len)); 
    }

  vector <int> diam1, diam2;

  dfs(0,0,0,0,endpoint1);
  dfs(endpoint1,endpoint1,0,0,endpoint2);
  recoverdiam(endpoint1,endpoint1,endpoint2,0);

  int ver= endpoint2, par = p[endpoint2];
  while(ver != endpoint1)
    {
      diam1.pb(ver);
      ver = par;
      par = p[ver];
    }
  diam1.pb(endpoint1);

  for(int i = 0; i < N; i++)
    {
      if(!mk[i])
	{
	  dfs(i,i,0,0,ep3);
	  dfs(ep3,ep3,0,0,ep4);
	  recoverdiam(ep3,ep3,ep4,0);
	  int ver= ep4, par = p[ep4];
	  while(ver != ep3)
	    {
	      diam2.pb(ver);
	      ver = par;
	      par = p[ver];
	    }
	  diam2.pb(ep3);
	  break;
	}
    }

  for(int i = 0; i < diam1.size() - 1; i++)
    pf1[i+1] = pf1[i] + adj[diam1[i]][diam1[i+1]].second;

  for(int i = 0; i < diam2.size()-1; i++)
    pf2[i+1] = pf2[i] + adj[diam2[i]][diam2[i+1]].second;

  int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver; 
  for(int i = 0; i < diam1.size(); i++)
    if(abs(pf1[diam1.size()-1] - 2*pf1[i]) < mn1)
      {
	mn1 = abs(pf1[diam1.size()-1] - 2*pf1[i]);
	mn1ver = diam1[i];
      }

  for(int i = 0; i < diam2.size();i++)
    if(abs(pf2[diam2.size()-1] - 2*pf2[i]) < mn2)
      {
	mn2 = abs(pf2[diam2.size()-1] - 2*pf2[i]);
	mn2ver = diam2[i];
      }

  //----------------------------------------

  adj[mn1ver].pb(ii(mn2ver,L));
  adj[mn2ver].pb(ii(mn1ver,L));

  int final1, final2;
  dfs(0,0,0,0,final1);
  dfs(final1,final1,0,0,final2);
  return recoverdiam(final1,final1,final2,0);

}

Compilation message

dreaming.cpp: In function 'void dfs(int, int, int, int, int&)':
dreaming.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[v].size(); i++)
                  ~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int recoverdiam(int, int, int, int)':
dreaming.cpp:33:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < adj[v].size(); i++)
                  ~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:83:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam1.size() - 1; i++)
                  ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp:86:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam2.size()-1; i++)
                  ~~^~~~~~~~~~~~~~~~
dreaming.cpp:90:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam1.size(); i++)
                  ~~^~~~~~~~~~~~~~
dreaming.cpp:97:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam2.size();i++)
                  ~~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int recoverdiam(int, int, int, int)':
dreaming.cpp:38:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:89:37: warning: 'mn2ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver; 
                                     ^~~~~~
dreaming.cpp:89:29: warning: 'mn1ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver; 
                             ^~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 29784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 29784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 29784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 29784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 85 ms 29784 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -