답안 #29641

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
29641 2017-07-20T09:47:19 Z kavun 꿈 (IOI13_dreaming) C++14
14 / 100
99 ms 15220 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, mx[10];
bool mk[100010];
int pf1[100010], pf2[100010];

int 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);
    }
  return mx;
}

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,mx[0],endpoint1);
  dfs(endpoint1,endpoint1,0,mx[1],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,mx[2],ep3);
	  dfs(ep3,ep3,0,mx[3],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++)
    for(int j = 0; j < adj[diam1[i]].size(); j++)
      if(adj[diam1[i]][j].first == diam1[i+1])
	pf1[i+1] = pf1[i] + adj[diam1[i]][j].second;

  for(int i = 0; i < diam2.size()-1; i++)
    for(int j= 0;j < adj[diam2[i]].size(); j++)
      if(adj[diam2[i]][j].first == diam2[i+1])
	pf2[i+1] = pf2[i] + adj[diam2[i]][j].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,mx[4],final1);
  return dfs(final1,final1,0,mx[5],final2);


}

Compilation message

dreaming.cpp: In function 'int 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:34: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:84:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam1.size() - 1; i++)
                  ~~^~~~~~~~~~~~~~~~~~
dreaming.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 0; j < adj[diam1[i]].size(); j++)
                    ~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:89: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 j= 0;j < adj[diam2[i]].size(); j++)
                  ~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < diam1.size(); i++)
                  ~~^~~~~~~~~~~~~~
dreaming.cpp:102: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:39: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:94:37: warning: 'mn2ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver; 
                                     ^~~~~~
dreaming.cpp:94:29: warning: 'mn1ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
   int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver; 
                             ^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 15220 KB Output is correct
2 Correct 88 ms 14964 KB Output is correct
3 Correct 60 ms 10768 KB Output is correct
4 Correct 13 ms 4608 KB Output is correct
5 Correct 12 ms 3712 KB Output is correct
6 Correct 21 ms 5504 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 37 ms 6912 KB Output is correct
9 Correct 47 ms 8696 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 71 ms 10488 KB Output is correct
12 Correct 90 ms 12756 KB Output is correct
13 Correct 4 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 15220 KB Output is correct
2 Correct 88 ms 14964 KB Output is correct
3 Correct 60 ms 10768 KB Output is correct
4 Correct 13 ms 4608 KB Output is correct
5 Correct 12 ms 3712 KB Output is correct
6 Correct 21 ms 5504 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 37 ms 6912 KB Output is correct
9 Correct 47 ms 8696 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 71 ms 10488 KB Output is correct
12 Correct 90 ms 12756 KB Output is correct
13 Correct 4 ms 2816 KB Output is correct
14 Incorrect 3 ms 2688 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 15220 KB Output is correct
2 Correct 88 ms 14964 KB Output is correct
3 Correct 60 ms 10768 KB Output is correct
4 Correct 13 ms 4608 KB Output is correct
5 Correct 12 ms 3712 KB Output is correct
6 Correct 21 ms 5504 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 37 ms 6912 KB Output is correct
9 Correct 47 ms 8696 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 71 ms 10488 KB Output is correct
12 Correct 90 ms 12756 KB Output is correct
13 Correct 4 ms 2816 KB Output is correct
14 Incorrect 3 ms 2688 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 4992 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 15220 KB Output is correct
2 Correct 88 ms 14964 KB Output is correct
3 Correct 60 ms 10768 KB Output is correct
4 Correct 13 ms 4608 KB Output is correct
5 Correct 12 ms 3712 KB Output is correct
6 Correct 21 ms 5504 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 37 ms 6912 KB Output is correct
9 Correct 47 ms 8696 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 71 ms 10488 KB Output is correct
12 Correct 90 ms 12756 KB Output is correct
13 Correct 4 ms 2816 KB Output is correct
14 Incorrect 4 ms 2688 KB Output isn't correct
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 15220 KB Output is correct
2 Correct 88 ms 14964 KB Output is correct
3 Correct 60 ms 10768 KB Output is correct
4 Correct 13 ms 4608 KB Output is correct
5 Correct 12 ms 3712 KB Output is correct
6 Correct 21 ms 5504 KB Output is correct
7 Correct 3 ms 2688 KB Output is correct
8 Correct 37 ms 6912 KB Output is correct
9 Correct 47 ms 8696 KB Output is correct
10 Correct 4 ms 2816 KB Output is correct
11 Correct 71 ms 10488 KB Output is correct
12 Correct 90 ms 12756 KB Output is correct
13 Correct 4 ms 2816 KB Output is correct
14 Incorrect 3 ms 2688 KB Output isn't correct
15 Halted 0 ms 0 KB -