Submission #648784

# Submission time Handle Problem Language Result Execution time Memory
648784 2022-10-08T10:06:26 Z LouayFarah Dreaming (IOI13_dreaming) C++14
10 / 100
1000 ms 22704 KB
#include "bits/stdc++.h"
#include "dreaming.h"

using namespace std;

#define endl "\n"
#define ll long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second


const long long MOD = 1e9+7;
const long long INF = 1e18;

int nx[8] = {0, 0, -1, 1, 1, -1, 1, -1};
int ny[8] = {1, -1, 0, 0, -1, 1, 1, -1};

void dfs(vector<pair<ll, ll>> adj[], int u, vector<bool> &visited, vector<ll> &curr_comp)
{
    if(visited[u])
        return;
    visited[u] = true;
    curr_comp.pb(u);

    for(auto v: adj[u])
    {
        dfs(adj, v.fi, visited, curr_comp);
    }
}

void dfs_dist(vector<pair<ll, ll>> adj[], ll u, ll p, vector<int> &dist)
{
    for(auto v: adj[u])
    {
        if(v.fi!=p)
        {
            dist[v.fi] = dist[u] + v.se;
            dfs_dist(adj, v.fi, u, dist);
        }
    }
}

int calculate_diameter(vector<pair<ll, ll>> adj[], int N)
{
    int a = 1;
    vector<int> dist_a(N+1, 0);
    dfs_dist(adj, a, -1, dist_a);

    int b = -1;
    int maximum_a = 0;

    for(int i = 1; i<=N; i++)
    {
        if(dist_a[i]>maximum_a)
        {
            maximum_a = dist_a[i];
            b = i;
        }
    }

    vector<int> dist_b(N+1, 0);
    dfs_dist(adj, b, -1, dist_b);

    int c = -1;
    int maximum_b = 0;
    for(int i = 1; i<=N; i++)
    {
        if(dist_b[i]>maximum_b)
        {
            maximum_b = dist_b[i];
            c = i;
        }
    }

    return dist_b[c];
}

int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
    //creating the graph
    vector<pair<ll, ll>> adj[N+1];
    for(int i = 0; i<M; i++)
    {
        adj[A[i]+1].pb(mp(B[i]+1, T[i]));
        adj[B[i]+1].pb(mp(A[i]+1, T[i]));
    }


    //extracting the 2 components
    vector<vector<ll>> comps;
    vector<bool> visited(N+1, false);
    for(int u = 1; u<=N; u++)
    {
        if(visited[u])
            continue;
        vector<ll> curr_comp;
        dfs(adj, u, visited, curr_comp);

        comps.pb(curr_comp);
    }


    int res = 1e9;
    for(int i = 0; i<int(comps[0].size()); i++)
    {
        for(int j = 0; j<int(comps[1].size()); j++)
        {
            //adding the edge
            vector<pair<ll, ll>> tempadj[N+1];
            for(int i = 1; i<=N; i++)
            {
                tempadj[i] = adj[i];
            }
            tempadj[comps[0][i]].pb(mp(comps[1][j], L));
            tempadj[comps[1][j]].pb(mp(comps[0][i], L));

            //Calculating tree diameter
            int diameter = calculate_diameter(tempadj, N);
            //cout << diameter << endl << flush;

            res = min(res, diameter);
        }
    }

    return res;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 22704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 11 ms 332 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 9 ms 304 KB Output is correct
5 Correct 12 ms 212 KB Output is correct
6 Correct 13 ms 212 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 13 ms 436 KB Output is correct
12 Correct 11 ms 328 KB Output is correct
13 Correct 12 ms 336 KB Output is correct
14 Correct 6 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 11 ms 304 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 6 ms 212 KB Output is correct
19 Correct 5 ms 212 KB Output is correct
20 Correct 10 ms 212 KB Output is correct
21 Correct 11 ms 340 KB Output is correct
22 Correct 12 ms 308 KB Output is correct
23 Correct 1 ms 304 KB Output is correct
24 Correct 8 ms 212 KB Output is correct
25 Correct 11 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 22704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 41 ms 13076 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 11 ms 332 KB Output is correct
3 Correct 3 ms 340 KB Output is correct
4 Correct 9 ms 304 KB Output is correct
5 Correct 12 ms 212 KB Output is correct
6 Correct 13 ms 212 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 1 ms 308 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 13 ms 436 KB Output is correct
12 Correct 11 ms 328 KB Output is correct
13 Correct 12 ms 336 KB Output is correct
14 Correct 6 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 11 ms 304 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 6 ms 212 KB Output is correct
19 Correct 5 ms 212 KB Output is correct
20 Correct 10 ms 212 KB Output is correct
21 Correct 11 ms 340 KB Output is correct
22 Correct 12 ms 308 KB Output is correct
23 Correct 1 ms 304 KB Output is correct
24 Correct 8 ms 212 KB Output is correct
25 Correct 11 ms 308 KB Output is correct
26 Incorrect 1 ms 440 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1098 ms 22704 KB Time limit exceeded
2 Halted 0 ms 0 KB -