Submission #413531

# Submission time Handle Problem Language Result Execution time Memory
413531 2021-05-28T20:38:55 Z LouayFarah Dreaming (IOI13_dreaming) C++14
47 / 100
152 ms 16316 KB
#include "bits/stdc++.h"
#include "dreaming.h"
using namespace std;
 
#define pb push_back
#define mp make_pair
#define fi first
#define se second
 
 
vector<vector<pair<int, int>>> adj;
vector<int> path1, path2;
vector<int> cnt1, cnt2;
 
void dfs1(vector<bool> &visited, int u)
{
    if(visited[u])
        return;
    visited[u] = true;
 
    for(auto v: adj[u])
    {
        dfs1(visited, v.fi);
    }
}
 
pair<int, int> extract_components(int N)
{
    vector<int> temp;
    vector<bool> visited(N, false);
 
    for(int i = 0; i<N; i++)
    {
        if(!visited[i])
        {
            dfs1(visited, i);
            temp.pb(i);
        }
    }
 
    pair<int, int> res = mp(0, 0);
    temp[0], res.se = temp[1];
    return res;
}
 
void depth(vector<bool> &visited, vector<int> &cnt, int u)
{
    visited[u] = true;
    for(auto v: adj[u])
    {
        if(!visited[v.fi])
        {
            cnt[v.fi] = cnt[u] + v.se;
            depth(visited, cnt, v.fi);
        }
    }
}
 
pair<int, int> diameter(int N, int u)
{
    int a = u, b = u, c = u;
 
    int maxi = 0;
    vector<bool> visited(N, false);
    vector<int> cnt(N, 0);
    depth(visited, cnt, a);
    for(int i = 0; i<N; i++)
    {
        if(maxi<cnt[i])
        {
            maxi = cnt[i];
            b = i;
        }
    }
 
    cnt.assign(N, 0);
    visited.assign(N, false);
    depth(visited, cnt, b);
    maxi = 0;
    for(int i = 0; i<N; i++)
    {
        if(maxi<cnt[i])
        {
            maxi = cnt[i];
            c = i;
        }
    }
 
    return mp(b, c);
}
 
vector<int> bfs(int N, pair<int, int> d)
{
    int st = d.fi, en = d.se;
    if(st==en)
    {
        vector<int> temp;
        temp.pb(st);
        return temp;
    }
 
    vector<bool> visited(N, false);
    vector<int> parent(N, -1);
    queue<int> q;
    q.push(st);
    visited[st] = true;
 
    bool flag = true;
    while(!q.empty()&&flag)
    {
        int u  = q.front();
        q.pop();
 
        for(auto v: adj[u])
        {
            if(visited[v.fi])
                continue;
            visited[v.fi] = true;
            parent[v.fi] = u;
            q.push(v.fi);
            if(v.fi==en)
                flag = false;
        }
    }
 
    vector<int> path;
    while(parent[en]!=-1)
    {
        path.pb(en);
        en = parent[en];
    }
 
    path.pb(st);
    reverse(path.begin(), path.end());
    return path;
}
 
int solve(int N)
{
    int a = 0, b = 0;
 
    int maxi = 0;
    vector<bool> visited(N, false);
    vector<int> cnt(N, 0);
    depth(visited, cnt, a);
    for(int i = 0; i<N; i++)
    {
        if(maxi<cnt[i])
        {
            maxi = cnt[i];
            b = i;
        }
    }
 
    cnt.assign(N, 0);
    visited.assign(N, false);
    depth(visited, cnt, b);
    maxi = 0;
    for(int i = 0; i<N; i++)
    {
        if(maxi<cnt[i])
        {
            maxi = cnt[i];
        }
    }
 
    return maxi;
}
 
vector<pair<int, int>> emp(0);
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
    adj.assign(N, emp);
 
    for(int i = 0; i<M; i++)
    {
        adj[A[i]].pb(mp(B[i], T[i]));
        adj[B[i]].pb(mp(A[i], T[i]));
    }
 
    pair<int, int> comps = extract_components(N);
    int a = comps.fi, b = comps.se;
 
    pair<int, int> d1 = diameter(N, a);
    pair<int, int> d2 = diameter(N, b);
 
    path1 = bfs(N, d1);
    path2 = bfs(N, d2);
 
    int len1 = int(path1.size());
    int len2 = int(path2.size());
 
    vector<bool> visited(N, false);
  	cnt1.assign(N, 0);
  	cnt2.assign(N, 0);
    depth(visited, cnt1, path1[0]);
    depth(visited, cnt2, path2[0]);
 
    int n1 = path1[0], n2 = path2[0];
 
    int dif = 1e8;
    for(int i = 1; i<len1; i++)
    {
        if(abs(cnt1[path1[i]]-(cnt1[path1[len1-1]]-cnt1[path1[i]]))<dif)
        {
            dif = abs(cnt1[path1[i]]-(cnt1[path1[len1-1]]-cnt1[path1[i]]));
            n1 = path1[i];
        }
    }
 
    dif = 1e8;
    for(int i = 1; i<len2; i++)
    {
        if(abs(cnt2[path2[i]]-(cnt2[path2[len2-1]]-cnt2[path2[i]]))<dif)
        {
            dif = abs(cnt2[path2[i]]-(cnt2[path2[len2-1]]-cnt2[path2[i]]));
            n2 = path2[i];
        }
    }
    path1.clear();
    path2.clear();
    cnt1.clear();
    cnt2.clear();
    adj[n1].pb(mp(n2, L));
    adj[n2].pb(mp(n1, L));
 
    int res = solve(N);
    return res;
}
# Verdict Execution time Memory Grader output
1 Correct 74 ms 14660 KB Output is correct
2 Correct 75 ms 14648 KB Output is correct
3 Correct 53 ms 9904 KB Output is correct
4 Correct 9 ms 2420 KB Output is correct
5 Correct 8 ms 1612 KB Output is correct
6 Correct 18 ms 3392 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 42 ms 5688 KB Output is correct
9 Correct 44 ms 7644 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 69 ms 10312 KB Output is correct
12 Correct 88 ms 12348 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 74 ms 14660 KB Output is correct
2 Correct 75 ms 14648 KB Output is correct
3 Correct 53 ms 9904 KB Output is correct
4 Correct 9 ms 2420 KB Output is correct
5 Correct 8 ms 1612 KB Output is correct
6 Correct 18 ms 3392 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 42 ms 5688 KB Output is correct
9 Correct 44 ms 7644 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 69 ms 10312 KB Output is correct
12 Correct 88 ms 12348 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 296 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 300 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 77 ms 14732 KB Output is correct
40 Correct 79 ms 14736 KB Output is correct
41 Correct 47 ms 9916 KB Output is correct
42 Correct 12 ms 2380 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 296 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 101 ms 10292 KB Output is correct
58 Correct 134 ms 10300 KB Output is correct
59 Correct 152 ms 10260 KB Output is correct
60 Correct 105 ms 10324 KB Output is correct
61 Correct 96 ms 10276 KB Output is correct
62 Correct 103 ms 10264 KB Output is correct
63 Correct 85 ms 10064 KB Output is correct
64 Correct 110 ms 10044 KB Output is correct
65 Correct 112 ms 10272 KB Output is correct
66 Correct 89 ms 10332 KB Output is correct
67 Correct 126 ms 10172 KB Output is correct
68 Correct 104 ms 10228 KB Output is correct
69 Correct 114 ms 10724 KB Output is correct
70 Correct 103 ms 10512 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 3 ms 588 KB Output is correct
73 Correct 3 ms 588 KB Output is correct
74 Correct 3 ms 588 KB Output is correct
75 Correct 3 ms 572 KB Output is correct
76 Correct 3 ms 572 KB Output is correct
77 Correct 4 ms 588 KB Output is correct
78 Correct 3 ms 564 KB Output is correct
79 Correct 3 ms 616 KB Output is correct
80 Correct 133 ms 10228 KB Output is correct
81 Correct 103 ms 10244 KB Output is correct
82 Correct 115 ms 10356 KB Output is correct
83 Correct 106 ms 10452 KB Output is correct
84 Correct 2 ms 460 KB Output is correct
85 Correct 3 ms 460 KB Output is correct
86 Correct 2 ms 460 KB Output is correct
87 Correct 3 ms 460 KB Output is correct
88 Correct 2 ms 460 KB Output is correct
89 Correct 2 ms 460 KB Output is correct
90 Correct 3 ms 476 KB Output is correct
91 Correct 3 ms 588 KB Output is correct
92 Correct 3 ms 568 KB Output is correct
93 Correct 3 ms 464 KB Output is correct
94 Correct 1 ms 216 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 1 ms 204 KB Output is correct
98 Correct 1 ms 204 KB Output is correct
99 Correct 1 ms 204 KB Output is correct
100 Correct 1 ms 204 KB Output is correct
101 Correct 1 ms 296 KB Output is correct
102 Correct 1 ms 296 KB Output is correct
103 Correct 1 ms 204 KB Output is correct
104 Correct 8 ms 1500 KB Output is correct
105 Correct 18 ms 3500 KB Output is correct
106 Correct 2 ms 308 KB Output is correct
107 Correct 36 ms 5752 KB Output is correct
108 Correct 47 ms 7604 KB Output is correct
109 Correct 1 ms 332 KB Output is correct
110 Correct 83 ms 10256 KB Output is correct
111 Correct 127 ms 12472 KB Output is correct
112 Correct 1 ms 332 KB Output is correct
113 Correct 139 ms 16316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 22 ms 6296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 296 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Incorrect 2 ms 332 KB Output isn't correct
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 74 ms 14660 KB Output is correct
2 Correct 75 ms 14648 KB Output is correct
3 Correct 53 ms 9904 KB Output is correct
4 Correct 9 ms 2420 KB Output is correct
5 Correct 8 ms 1612 KB Output is correct
6 Correct 18 ms 3392 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 42 ms 5688 KB Output is correct
9 Correct 44 ms 7644 KB Output is correct
10 Correct 1 ms 304 KB Output is correct
11 Correct 69 ms 10312 KB Output is correct
12 Correct 88 ms 12348 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 300 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 1 ms 204 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 296 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 204 KB Output is correct
35 Correct 1 ms 204 KB Output is correct
36 Correct 1 ms 300 KB Output is correct
37 Correct 1 ms 204 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 77 ms 14732 KB Output is correct
40 Correct 79 ms 14736 KB Output is correct
41 Correct 47 ms 9916 KB Output is correct
42 Correct 12 ms 2380 KB Output is correct
43 Correct 1 ms 204 KB Output is correct
44 Correct 1 ms 204 KB Output is correct
45 Correct 1 ms 296 KB Output is correct
46 Correct 1 ms 204 KB Output is correct
47 Correct 1 ms 204 KB Output is correct
48 Correct 1 ms 204 KB Output is correct
49 Correct 1 ms 204 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 1 ms 204 KB Output is correct
54 Correct 1 ms 204 KB Output is correct
55 Correct 1 ms 204 KB Output is correct
56 Correct 1 ms 204 KB Output is correct
57 Correct 101 ms 10292 KB Output is correct
58 Correct 134 ms 10300 KB Output is correct
59 Correct 152 ms 10260 KB Output is correct
60 Correct 105 ms 10324 KB Output is correct
61 Correct 96 ms 10276 KB Output is correct
62 Correct 103 ms 10264 KB Output is correct
63 Correct 85 ms 10064 KB Output is correct
64 Correct 110 ms 10044 KB Output is correct
65 Correct 112 ms 10272 KB Output is correct
66 Correct 89 ms 10332 KB Output is correct
67 Correct 126 ms 10172 KB Output is correct
68 Correct 104 ms 10228 KB Output is correct
69 Correct 114 ms 10724 KB Output is correct
70 Correct 103 ms 10512 KB Output is correct
71 Correct 1 ms 204 KB Output is correct
72 Correct 3 ms 588 KB Output is correct
73 Correct 3 ms 588 KB Output is correct
74 Correct 3 ms 588 KB Output is correct
75 Correct 3 ms 572 KB Output is correct
76 Correct 3 ms 572 KB Output is correct
77 Correct 4 ms 588 KB Output is correct
78 Correct 3 ms 564 KB Output is correct
79 Correct 3 ms 616 KB Output is correct
80 Correct 133 ms 10228 KB Output is correct
81 Correct 103 ms 10244 KB Output is correct
82 Correct 115 ms 10356 KB Output is correct
83 Correct 106 ms 10452 KB Output is correct
84 Correct 2 ms 460 KB Output is correct
85 Correct 3 ms 460 KB Output is correct
86 Correct 2 ms 460 KB Output is correct
87 Correct 3 ms 460 KB Output is correct
88 Correct 2 ms 460 KB Output is correct
89 Correct 2 ms 460 KB Output is correct
90 Correct 3 ms 476 KB Output is correct
91 Correct 3 ms 588 KB Output is correct
92 Correct 3 ms 568 KB Output is correct
93 Correct 3 ms 464 KB Output is correct
94 Correct 1 ms 216 KB Output is correct
95 Correct 1 ms 204 KB Output is correct
96 Correct 1 ms 204 KB Output is correct
97 Correct 1 ms 204 KB Output is correct
98 Correct 1 ms 204 KB Output is correct
99 Correct 1 ms 204 KB Output is correct
100 Correct 1 ms 204 KB Output is correct
101 Correct 1 ms 296 KB Output is correct
102 Correct 1 ms 296 KB Output is correct
103 Correct 1 ms 204 KB Output is correct
104 Correct 8 ms 1500 KB Output is correct
105 Correct 18 ms 3500 KB Output is correct
106 Correct 2 ms 308 KB Output is correct
107 Correct 36 ms 5752 KB Output is correct
108 Correct 47 ms 7604 KB Output is correct
109 Correct 1 ms 332 KB Output is correct
110 Correct 83 ms 10256 KB Output is correct
111 Correct 127 ms 12472 KB Output is correct
112 Correct 1 ms 332 KB Output is correct
113 Correct 139 ms 16316 KB Output is correct
114 Incorrect 22 ms 6296 KB Output isn't correct
115 Halted 0 ms 0 KB -