Submission #153722

#TimeUsernameProblemLanguageResultExecution timeMemory
153722Ruxandra985꿈 (IOI13_dreaming)C++14
100 / 100
127 ms17528 KiB
#include <cstdio>
#include <vector>
#include "dreaming.h"
#include <algorithm>
#define DIMN 100010
using namespace std;
int f[DIMN] , vc , rc,dist[DIMN] , a[DIMN] , b[DIMN] , c[DIMN],dp[DIMN],diam_max;
vector <pair <int,int> > v[DIMN];
int arb[DIMN];
int dfs_initial (int nod){ /// return diametru arbore + calcul distante
    int i,vecin , maxi1, maxi2;
    f[nod] = 1;
    //printf ("%d ",nod);
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i].first;
        if (!f[vecin]){
            dfs_initial (vecin);
            dist[nod] = max (dist[nod] , dist[vecin] + v[nod][i].second);
        }
    }
    if (v[nod].size() == 1){
        return dist[nod];
    }
    maxi1 = maxi2 = 0;
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i].first;
        if (maxi1 < dist[vecin] + v[nod][i].second){
            maxi2 = maxi1;
            maxi1 = dist[vecin] + v[nod][i].second;
        }
        else if (maxi2 < dist[vecin] + v[nod][i].second){
            maxi2 = dist[vecin] + v[nod][i].second;
        }
    }
    diam_max = max (diam_max , maxi1 + maxi2); /// diametrul arborelui curent
}
void dfs_process (int nod, int tt, int dup){
    int i,vecin , maxi1 , maxi2 , pmaxi1;
    /// daca il faci pe nod radacina , dmax e fie dist[nod] fie dup
    dp[nod] = max (dist[nod] , dup);
    vc = min(vc , max(dist[nod] , dup) );
    maxi1 = maxi2 = 0;
    pmaxi1 = 0;
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i].first;
        if (vecin == tt)
            continue;
        if (maxi1 < dist[vecin] + v[nod][i].second){
            maxi2 = maxi1;
            maxi1 = dist[vecin] + v[nod][i].second;
            pmaxi1 = vecin;
        }
        else if (maxi2 < dist[vecin] + v[nod][i].second){
            maxi2 = dist[vecin] + v[nod][i].second;
        }
    }
    for (i=0;i<v[nod].size();i++){
        vecin = v[nod][i].first;
        if (vecin==tt)
            continue;
        if (vecin == pmaxi1)
            dfs_process (vecin , nod , v[nod][i].second + max (dup , maxi2));
        else
            dfs_process (vecin , nod , v[nod][i].second + max (dup , maxi1));
    }

}
int travelTime (int n , int m , int l , int a[] , int b[] , int c[]){
    int i , elem , inter ;
    elem = 0;
    for (i=1;i<=m;i++){
        a[i-1]++;
        b[i-1]++;
        v[a[i-1]].push_back(make_pair(b[i-1] , c[i-1]));
        v[b[i-1]].push_back(make_pair(a[i-1] , c[i-1]));
    }
    for (i=1;i<=n;i++){
        if (!f[i]){
           // printf ("\n");
            dfs_initial(i);
            vc = 2000000000;
            dfs_process (i, 0 , 0);
            /// vc = valoarea arborelui curent
            arb[++elem] = vc;
        }
    }
    sort (arb + 1, arb + elem + 1);
    int sol = diam_max;
    if (elem > 1)
        sol = max ( sol , arb[elem] + l + arb[elem-1] );
    if (elem > 2)
        sol = max ( sol , arb[elem-1] + 2 * l + arb[elem-2] );
    return sol;
}

Compilation message (stderr)

dreaming.cpp: In function 'int dfs_initial(int)':
dreaming.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
dreaming.cpp:25:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs_process(int, int, int)':
dreaming.cpp:44:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
dreaming.cpp:57:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i=0;i<v[nod].size();i++){
              ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:69:20: warning: unused variable 'inter' [-Wunused-variable]
     int i , elem , inter ;
                    ^~~~~
dreaming.cpp: In function 'int dfs_initial(int)':
dreaming.cpp:36:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...