#include "dreaming.h"
#include <iostream>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#define ll long long
#define pii pair <ll, int>
using namespace std;
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
map <int, vector <pii>> adj;
map <int, vector <int>> cc;
map <pii, int> edges;
map <int, int> ccnum;
int maxgrp;
ll maxlen = 0;
map <int, int> roots;
map <int, int> layer;
vector <int> mins;
ll m1 = -1, m2 = -1, m3 = -1;
for (int i = 0; i < M; i++)
{
adj[A[i]].push_back(make_pair(B[i], T[i]));
adj[B[i]].push_back(make_pair(A[i], T[i]));
edges[make_pair(A[i], B[i])] = T[i];
edges[make_pair(B[i], A[i])] = T[i];
}
int cur = 0;
for (int i = 0; i < N; i++)
{
ccnum[i] = -1;
}
for (int i = 0; i < N; i++)
{
if (ccnum[i] == -1)
{
queue <int> curs;
curs.push(i);
ccnum[i] = cur;
while (!curs.empty())
{
int a = curs.front();
curs.pop();
for (auto x : adj[a])
{
if (ccnum[x.first] == -1)
{
ccnum[x.first] = cur;
curs.push(x.first);
}
}
}
cur++;
}
}
for (int i = 0; i < N; i++)
{
cc[ccnum[i]].push_back(i);
layer[i] = -1;
}
maxgrp = cur - 1;
for (int i = 0; i <= maxgrp; i++)
{
if (cc[i].size() == 1)
{
if (m3 < 0)
{
m3 = 0;
if (m3 > m2)
{
swap(m3, m2);
if (m2 > m1)
{
swap(m2, m1);
}
}
}
continue;
}
bool path = true;
roots[i] = cc[i][0];
queue <pii> depth;
layer[roots[i]] = 0;
depth.push(make_pair(roots[i], 0));
map <int, vector <int>> layers;
layers[0].push_back(roots[i]);
int maxlayer = 0;
map <int, vector <int>> children;
map <int, int> parent;
map <int, pii> big;
for (int a : cc[i])
{
parent[a] = -1;
}
while (!depth.empty())
{
pii a = depth.front();
depth.pop();
for (pii b : adj[a.first])
{
if (layer[b.first] == -1)
{
depth.push(make_pair(b.first, a.second + 1));
layer[b.first] = a.second + 1;
layers[a.second + 1].push_back(b.first);
maxlayer = max(maxlayer, a.second + 1);
children[a.first].push_back(b.first);
if (children[a.first].size() >= 2)
{
path = false;
}
parent[b.first] = a.first;
}
}
}
queue <int> calc;
map <int, bool> seen;
seen[-1] = true;
map <int, int> cseen;
for (int a : cc[i])
{
if (children[a].size() == 0)
{
calc.push(a);
seen[a] = true;
}
}
while (!calc.empty())
{
int a = calc.front();
calc.pop();
if (!seen[parent[a]])
{
cseen[parent[a]]++;
if (cseen[parent[a]] == children[parent[a]].size())
{
calc.push(parent[a]);
seen[parent[a]] = true;
}
}
if (children[a].size() == 0)
{
big[a] = make_pair(0, a);
continue;
}
pii x = make_pair(-1, -1);
for (int b : children[a])
{
int l1 = big[b].first;
if (l1 >= 0 && l1 + edges[make_pair(a, b)] > x.first)
{
x.first = l1 + edges[make_pair(a, b)];
x.second = big[b].second;
}
}
big[a] = x;
}
int start = -1;
int end = -1;
int common = -1;
ll length = 0;
ll tot = 0;
if (path)
{
start = roots[i];
int a = roots[i];
while (children[a].size() > 0)
{
length += edges[make_pair(a, children[a][0])];
a = children[a][0];
}
end = a;
}
else
{
for (int a : cc[i])
{
pii max1 = make_pair(0, 0);
pii max2 = make_pair(0, 0);
if (children[a].size() >= 2)
{
for (int b : children[a])
{
if (edges[make_pair(a, b)] + big[b].first > max2.first)
{
max2 = make_pair(edges[make_pair(a, b)] + big[b].first, big[b].second);
if (max2 > max1)
{
swap(max2, max1);
}
}
}
if (max1.first + max2.first > length)
{
length = max1.first + max2.first;
start = max1.second;
end = max2.second;
common = a;
path = false;
}
}
else if (children[a].size() == 1)
{
if (big[a].first > length)
{
length = big[a].first;
start = a;
end = big[a].second;
common = a;
path = true;
}
}
}
}
if (length > maxlen)
{
maxlen = length;
}
int curpos;
if (path)
{
curpos = end;
}
else
{
curpos = start;
}
int minmax = 0;
while (tot * 2 < length)
{
tot += edges[make_pair(curpos, parent[curpos])];
if (tot * 2 == length)
{
minmax = tot;
}
if (tot * 2 > length)
{
if (tot * 2 - edges[make_pair(curpos, parent[curpos])] >= length)
{
minmax = length - tot + edges[make_pair(curpos, parent[curpos])];
}
else
{
minmax = tot;
}
}
curpos = parent[curpos];
}
if (m3 < minmax)
{
m3 = minmax;
if (m3 > m2)
{
swap(m3, m2);
if (m2 > m1)
{
swap(m2, m1);
}
}
}
}
if (m2 == -1)
{
return max(maxlen, m1);
}
else if (m3 == -1)
{
return max(maxlen, m1 + L + m2);
}
else
{
return max(maxlen, max(m1 + L + m2, m2 + m3 + 2 * L));
}
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:136:26: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::mapped_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | if (cseen[parent[a]] == children[parent[a]].size())
dreaming.cpp:161:7: warning: variable 'common' set but not used [-Wunused-but-set-variable]
161 | int common = -1;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1059 ms |
60908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1059 ms |
60908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
418 ms |
30920 KB |
Output is correct |
2 |
Correct |
405 ms |
30976 KB |
Output is correct |
3 |
Correct |
408 ms |
30912 KB |
Output is correct |
4 |
Correct |
410 ms |
31080 KB |
Output is correct |
5 |
Correct |
443 ms |
30788 KB |
Output is correct |
6 |
Correct |
457 ms |
32708 KB |
Output is correct |
7 |
Correct |
439 ms |
32316 KB |
Output is correct |
8 |
Correct |
412 ms |
30404 KB |
Output is correct |
9 |
Correct |
394 ms |
30184 KB |
Output is correct |
10 |
Correct |
495 ms |
32068 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
140 ms |
28404 KB |
Output is correct |
13 |
Correct |
173 ms |
28456 KB |
Output is correct |
14 |
Correct |
145 ms |
28456 KB |
Output is correct |
15 |
Correct |
143 ms |
28560 KB |
Output is correct |
16 |
Correct |
144 ms |
28484 KB |
Output is correct |
17 |
Correct |
150 ms |
28356 KB |
Output is correct |
18 |
Correct |
144 ms |
28564 KB |
Output is correct |
19 |
Correct |
143 ms |
28540 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
4 ms |
1100 KB |
Output is correct |
23 |
Correct |
142 ms |
28432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
4 ms |
588 KB |
Output is correct |
27 |
Correct |
10 ms |
972 KB |
Output is correct |
28 |
Correct |
14 ms |
1356 KB |
Output is correct |
29 |
Correct |
5 ms |
692 KB |
Output is correct |
30 |
Correct |
9 ms |
1088 KB |
Output is correct |
31 |
Correct |
13 ms |
1484 KB |
Output is correct |
32 |
Correct |
6 ms |
588 KB |
Output is correct |
33 |
Correct |
9 ms |
1100 KB |
Output is correct |
34 |
Correct |
13 ms |
1484 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
352 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
2 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
2 ms |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
1 ms |
204 KB |
Output is correct |
53 |
Correct |
1 ms |
204 KB |
Output is correct |
54 |
Correct |
20 ms |
2492 KB |
Output is correct |
55 |
Correct |
20 ms |
2508 KB |
Output is correct |
56 |
Correct |
13 ms |
1356 KB |
Output is correct |
57 |
Correct |
6 ms |
1212 KB |
Output is correct |
58 |
Correct |
19 ms |
1868 KB |
Output is correct |
59 |
Correct |
19 ms |
2128 KB |
Output is correct |
60 |
Correct |
9 ms |
1300 KB |
Output is correct |
61 |
Correct |
7 ms |
1204 KB |
Output is correct |
62 |
Correct |
19 ms |
1888 KB |
Output is correct |
63 |
Correct |
19 ms |
1932 KB |
Output is correct |
64 |
Correct |
6 ms |
1064 KB |
Output is correct |
65 |
Correct |
11 ms |
1356 KB |
Output is correct |
66 |
Correct |
20 ms |
2224 KB |
Output is correct |
67 |
Correct |
18 ms |
1900 KB |
Output is correct |
68 |
Correct |
12 ms |
1352 KB |
Output is correct |
69 |
Correct |
12 ms |
1368 KB |
Output is correct |
70 |
Correct |
8 ms |
1228 KB |
Output is correct |
71 |
Correct |
12 ms |
1228 KB |
Output is correct |
72 |
Correct |
20 ms |
2124 KB |
Output is correct |
73 |
Correct |
21 ms |
2200 KB |
Output is correct |
74 |
Correct |
15 ms |
1464 KB |
Output is correct |
75 |
Correct |
15 ms |
1484 KB |
Output is correct |
76 |
Correct |
23 ms |
1944 KB |
Output is correct |
77 |
Correct |
19 ms |
2124 KB |
Output is correct |
78 |
Correct |
8 ms |
1228 KB |
Output is correct |
79 |
Correct |
9 ms |
1228 KB |
Output is correct |
80 |
Correct |
4 ms |
1100 KB |
Output is correct |
81 |
Correct |
4 ms |
1100 KB |
Output is correct |
82 |
Correct |
15 ms |
1412 KB |
Output is correct |
83 |
Correct |
15 ms |
1420 KB |
Output is correct |
84 |
Correct |
9 ms |
1488 KB |
Output is correct |
85 |
Correct |
7 ms |
1100 KB |
Output is correct |
86 |
Correct |
13 ms |
1756 KB |
Output is correct |
87 |
Correct |
18 ms |
2124 KB |
Output is correct |
88 |
Correct |
11 ms |
1320 KB |
Output is correct |
89 |
Correct |
14 ms |
1332 KB |
Output is correct |
90 |
Correct |
9 ms |
1228 KB |
Output is correct |
91 |
Correct |
8 ms |
1248 KB |
Output is correct |
92 |
Correct |
12 ms |
1484 KB |
Output is correct |
93 |
Correct |
11 ms |
1540 KB |
Output is correct |
94 |
Correct |
14 ms |
1508 KB |
Output is correct |
95 |
Correct |
17 ms |
1680 KB |
Output is correct |
96 |
Correct |
15 ms |
1640 KB |
Output is correct |
97 |
Correct |
17 ms |
1612 KB |
Output is correct |
98 |
Correct |
16 ms |
1708 KB |
Output is correct |
99 |
Correct |
24 ms |
2136 KB |
Output is correct |
100 |
Correct |
18 ms |
1868 KB |
Output is correct |
101 |
Correct |
21 ms |
1840 KB |
Output is correct |
102 |
Correct |
1 ms |
332 KB |
Output is correct |
103 |
Correct |
1 ms |
300 KB |
Output is correct |
104 |
Correct |
1 ms |
332 KB |
Output is correct |
105 |
Correct |
1 ms |
332 KB |
Output is correct |
106 |
Correct |
1 ms |
332 KB |
Output is correct |
107 |
Correct |
1 ms |
304 KB |
Output is correct |
108 |
Correct |
1 ms |
332 KB |
Output is correct |
109 |
Correct |
1 ms |
332 KB |
Output is correct |
110 |
Correct |
1 ms |
332 KB |
Output is correct |
111 |
Correct |
1 ms |
332 KB |
Output is correct |
112 |
Correct |
3 ms |
460 KB |
Output is correct |
113 |
Correct |
5 ms |
688 KB |
Output is correct |
114 |
Correct |
6 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1059 ms |
60908 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |