Submission #1035359

#TimeUsernameProblemLanguageResultExecution timeMemory
1035359c2zi6Dreaming (IOI13_dreaming)C++14
14 / 100
42 ms15336 KiB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "dreaming.h"

int n, m, L;
VVPI gp;
void dfs(int u, int p, VI& dist, VI& comp) {
    comp.pb(u);
    for (auto[v, w] : gp[u]) if (v != p) {
        dist[v] = dist[u] + w;
        dfs(v, u, dist, comp);
    }
}
void dfs(int u, int p, VI& dist) {
    for (auto[v, w] : gp[u]) if (v != p) {
        dist[v] = dist[u] + w;
        dfs(v, u, dist);
    }
}

int amenaheru(VI& comp, VI& dist) {
    int ret;
    int mx = -2e9;
    for (int u : comp) if (dist[u] > mx) {
        ret = u;
        mx = dist[u];
    }
    return ret;
}
int amenamejtex(VI& comp, VI& maxdist) {
    int ret = 2e9;
    for (int u : comp) setmin(ret, maxdist[u]);
    return ret;
}

int travelTime(int N, int M, int L_arg, int A[], int B[], int T[]) {
    n = N;
    m = M;
    L = L_arg;
    gp = VVPI(n);
    rep(i, m) {
        int u = A[i];
        int v = B[i];
        int w = T[i];
        gp[u].pb({v, w});
        gp[v].pb({u, w});
    }

    VI dist(n, 2e9);
    VI distd1(n);
    VI distd2(n);
    VI maxdist(n);
    int globald = 0;
    VVI comp;
    rep(u, n) if (dist[u] == 2e9) {
        dist[u] = 0;
        comp.pb(VI());
        dfs(u, -1, dist, comp.back());
        int d1 = amenaheru(comp.back(), dist);
        dfs(d1, -1, distd1);
        int d2 = amenaheru(comp.back(), distd1);
        dfs(d2, -1, distd2);
        for (int u : comp.back()) maxdist[u] = max(distd1[u], distd2[u]);
        int diameter = distd1[d2];
        setmax(globald, diameter);
        /*cout << "COMPONENT: ";*/
        /*for (int u : comp.back()) cout << u << " "; cout << endl;*/
        /*cout << "DIAMETER: " << d1 << " " << d2 << " " << distd1[d2] << endl;*/
        /*cout << "FARTHEST PATH LENGTH FROM " << endl;*/
        /*for (int u : comp.back()) {*/
        /*    cout << u << ": " << maxdist[u] << endl;*/
        /*}*/
        /*cout << endl;*/
    }

    /*if (comp.size() > 2) {*/
    /*    VI a(3, 0);*/
    /*    rep(u, n) for (auto[v, w] : gp[u]) {*/
    /*        if (u < v) a.pb(w);*/
    /*    }*/
    /*    sort(all(a));*/
    /*    reverse(all(a));*/
    /*    VI b;*/
    /*    b.pb(a[0]);*/
    /*    b.pb(a[1]+L);*/
    /*    b.pb(a[2]+L);*/
    /*    sort(all(b));*/
    /*    reverse(all(b));*/
    /*    return b[0] + b[1];*/
    /*}*/

    VI mid;
    for (VI& c : comp) {
        mid.pb(amenamejtex(c, maxdist));
    }

    int answer = 2e9;

    int mxi;
    int mx = 0;
    rep(i, comp.size()) {
        if (mid[i] > mx) {
            mxi = i;
            mx = mid[i];
        }
    }

    replr(i, mxi, mxi) {
        int diameter = globald;

        VI children;
        children.pb(0);
        children.pb(mid[i]);
        rep(j, comp.size()) if (j != i) {
            children.pb(mid[j] + L);
        }
        sort(all(children));
        reverse(all(children));
        
        setmax(diameter, children[0] + children[1]);
        /*cout << "IF THE CENTER IS " << i << ", THE DIAMETER IS " << diameter << endl;*/
        setmin(answer, diameter);
    }


    return answer;
}



Compilation message (stderr)

dreaming.cpp: In function 'void dfs(int, int, VI&, VI&)':
dreaming.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |     for (auto[v, w] : gp[u]) if (v != p) {
      |              ^
dreaming.cpp: In function 'void dfs(int, int, VI&)':
dreaming.cpp:44:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   44 |     for (auto[v, w] : gp[u]) if (v != p) {
      |              ^
dreaming.cpp: In function 'int amenaheru(VI&, VI&)':
dreaming.cpp:57:12: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
   57 |     return ret;
      |            ^~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:143:29: warning: 'mxi' may be used uninitialized in this function [-Wmaybe-uninitialized]
  143 |         rep(j, comp.size()) if (j != i) {
      |                             ^~
dreaming.cpp:93:33: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
   93 |         int diameter = distd1[d2];
      |                                 ^
dreaming.cpp:89:12: warning: 'ret' may be used uninitialized in this function [-Wmaybe-uninitialized]
   89 |         dfs(d1, -1, distd1);
      |         ~~~^~~~~~~~~~~~~~~~
#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...