#include <cstdio>
#include "dreaming.h"
#include <vector>
#include <algorithm>
#define DIMN 100010
using namespace std;
int f[DIMN] , vc , rc,dist[DIMN] , a[DIMN] , b[DIMN] , c[DIMN],dp[DIMN];
vector <pair <int,int> > v[DIMN];
int arb[DIMN];
int dfs_initial (int nod , int ok){ /// return diametru arbore + calcul distante
int i,vecin , maxi1, maxi2;
f[nod] = 1;
for (i=0;i<v[nod].size();i++){
vecin = v[nod][i].first;
if (!f[vecin]){
dfs_initial (vecin , 0);
dist[nod] = max (dist[nod] , dist[vecin] + v[nod][i].second);
}
}
if (ok == 1){ /// acum aflam diametrul
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;
}
}
return maxi1 + maxi2; /// diametrul arborelui curent
}
else return 0;
}
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 , diam_max = 0;
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]){
diam_max = max (diam_max , dfs_initial(i,1));
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
dreaming.cpp: In function 'int dfs_initial(int, int)':
dreaming.cpp:13:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
dreaming.cpp:25:19: 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:46:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
dreaming.cpp:59: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:71:20: warning: unused variable 'inter' [-Wunused-variable]
int i , elem , inter , diam_max = 0;
^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
12920 KB |
Output is correct |
2 |
Correct |
70 ms |
12492 KB |
Output is correct |
3 |
Correct |
46 ms |
10872 KB |
Output is correct |
4 |
Correct |
14 ms |
4160 KB |
Output is correct |
5 |
Correct |
11 ms |
3576 KB |
Output is correct |
6 |
Correct |
20 ms |
4984 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
35 ms |
6904 KB |
Output is correct |
9 |
Correct |
43 ms |
9208 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
10284 KB |
Output is correct |
12 |
Correct |
69 ms |
11484 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
12920 KB |
Output is correct |
2 |
Correct |
70 ms |
12492 KB |
Output is correct |
3 |
Correct |
46 ms |
10872 KB |
Output is correct |
4 |
Correct |
14 ms |
4160 KB |
Output is correct |
5 |
Correct |
11 ms |
3576 KB |
Output is correct |
6 |
Correct |
20 ms |
4984 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
35 ms |
6904 KB |
Output is correct |
9 |
Correct |
43 ms |
9208 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
10284 KB |
Output is correct |
12 |
Correct |
69 ms |
11484 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 ms |
2684 KB |
Output is correct |
19 |
Correct |
4 ms |
2684 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2704 KB |
Output is correct |
22 |
Incorrect |
5 ms |
2680 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
12920 KB |
Output is correct |
2 |
Correct |
70 ms |
12492 KB |
Output is correct |
3 |
Correct |
46 ms |
10872 KB |
Output is correct |
4 |
Correct |
14 ms |
4160 KB |
Output is correct |
5 |
Correct |
11 ms |
3576 KB |
Output is correct |
6 |
Correct |
20 ms |
4984 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
35 ms |
6904 KB |
Output is correct |
9 |
Correct |
43 ms |
9208 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
10284 KB |
Output is correct |
12 |
Correct |
69 ms |
11484 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 ms |
2684 KB |
Output is correct |
19 |
Correct |
4 ms |
2684 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2704 KB |
Output is correct |
22 |
Incorrect |
5 ms |
2680 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
6776 KB |
Output is correct |
2 |
Correct |
34 ms |
6904 KB |
Output is correct |
3 |
Correct |
32 ms |
6776 KB |
Output is correct |
4 |
Correct |
31 ms |
6776 KB |
Output is correct |
5 |
Correct |
33 ms |
6788 KB |
Output is correct |
6 |
Correct |
32 ms |
7032 KB |
Output is correct |
7 |
Correct |
34 ms |
6904 KB |
Output is correct |
8 |
Correct |
30 ms |
6652 KB |
Output is correct |
9 |
Correct |
32 ms |
6740 KB |
Output is correct |
10 |
Correct |
34 ms |
6904 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
8 ms |
4216 KB |
Output is correct |
13 |
Correct |
8 ms |
4216 KB |
Output is correct |
14 |
Correct |
9 ms |
4284 KB |
Output is correct |
15 |
Correct |
8 ms |
4216 KB |
Output is correct |
16 |
Correct |
8 ms |
4216 KB |
Output is correct |
17 |
Correct |
8 ms |
3960 KB |
Output is correct |
18 |
Correct |
10 ms |
4344 KB |
Output is correct |
19 |
Correct |
8 ms |
4216 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
9 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
12920 KB |
Output is correct |
2 |
Correct |
70 ms |
12492 KB |
Output is correct |
3 |
Correct |
46 ms |
10872 KB |
Output is correct |
4 |
Correct |
14 ms |
4160 KB |
Output is correct |
5 |
Correct |
11 ms |
3576 KB |
Output is correct |
6 |
Correct |
20 ms |
4984 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
35 ms |
6904 KB |
Output is correct |
9 |
Correct |
43 ms |
9208 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
10284 KB |
Output is correct |
12 |
Correct |
69 ms |
11484 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Incorrect |
5 ms |
2680 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
12920 KB |
Output is correct |
2 |
Correct |
70 ms |
12492 KB |
Output is correct |
3 |
Correct |
46 ms |
10872 KB |
Output is correct |
4 |
Correct |
14 ms |
4160 KB |
Output is correct |
5 |
Correct |
11 ms |
3576 KB |
Output is correct |
6 |
Correct |
20 ms |
4984 KB |
Output is correct |
7 |
Correct |
4 ms |
2808 KB |
Output is correct |
8 |
Correct |
35 ms |
6904 KB |
Output is correct |
9 |
Correct |
43 ms |
9208 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
75 ms |
10284 KB |
Output is correct |
12 |
Correct |
69 ms |
11484 KB |
Output is correct |
13 |
Correct |
4 ms |
2808 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
4 ms |
2684 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
4 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 ms |
2684 KB |
Output is correct |
19 |
Correct |
4 ms |
2684 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2704 KB |
Output is correct |
22 |
Incorrect |
5 ms |
2680 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |