#include <bits/stdc++.h>
#include "dreaming.h"
#define ff first
#define ss second
using namespace std;
int inf = INT_MAX;
vector<vector<pair<int,int> > > gp;
vector<int> vis, dis,parents;
void dfs(int u, int pes){
vis[u] = 1;
dis[u] += pes;
for(auto it: gp[u]){
if(vis[it.ff] == 0){
vis[it.ff] = 1;
parents[it.ff] = u;
dfs(it.ff, it.ss + dis[u]);
}
}
}
int radius(int u){
int distan1 = dis[u], distan2 = 0, ans = dis[u];
while(parents[u] != -1){
ans = min(ans,max(distan1,distan2));
int pospar;
for(int i = 0; i < gp[u].size(); i++){
if(gp[u][i].ff == parents[u]) pospar = gp[u][i].ss;
}
u = parents[u];
distan1 -= pospar; distan2 += pospar;
}
return ans;
}
int fndext(int n){
int ans, maypes = -1;
for(int i = 0; i < n; i++){
if(dis[i] > maypes){
maypes = dis[i];
ans = i;
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
gp.resize(N); vis.resize(N); dis.resize(N); parents.resize(N);
for(int i = 0; i < N; i++) vis[i] = 0, dis[i] = 0;
for(int i = 0; i < M; i++){
gp[A[i]].push_back(make_pair(B[i],T[i]));
gp[B[i]].push_back(make_pair(A[i],T[i]));
}
dfs(0,0);
int nodext1, nodext2, nodext11, nodext22;
nodext1 = fndext(N);
dis.clear();
for(int i = 0; i < N; i++) vis[i] = 0, dis[i] = 0;
parents[nodext1] = -1;
dfs(nodext1,0);
nodext2 = fndext(N);
int diam1 = dis[nodext2], diam2;
for(int i = 0; i < N; i++) dis[i] = 0;
for(int i = 0; i < N; i++){
if(vis[i] == 0){
dfs(i,0);
nodext11 = fndext(N);
}
}
dis.clear();
for(int i = 0; i < N; i++) vis[i] = 0, dis[i] = 0;
parents[nodext11] = -1;
dfs(nodext11,0);
nodext22 = fndext(N);
diam2 = dis[nodext22];
return max(diam1,max(diam2,radius(nodext22) + radius(nodext2) + L));
}
Compilation message
dreaming.cpp: In function 'int radius(int)':
dreaming.cpp:27:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < gp[u].size(); i++){
~~^~~~~~~~~~~~~~
dreaming.cpp:31:36: warning: 'pospar' may be used uninitialized in this function [-Wmaybe-uninitialized]
distan1 -= pospar; distan2 += pospar;
~~~~~~~~^~~~~~~~~
dreaming.cpp: In function 'int fndext(int)':
dreaming.cpp:44:12: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
return ans;
^~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:59:20: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
parents[nodext1] = -1;
^
dreaming.cpp:62:28: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
int diam1 = dis[nodext2], diam2;
^
dreaming.cpp:72:21: warning: 'nodext11' may be used uninitialized in this function [-Wmaybe-uninitialized]
parents[nodext11] = -1;
^
dreaming.cpp:76:38: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
return max(diam1,max(diam2,radius(nodext22) + radius(nodext2) + L));
~~~~~~^~~~~~~~~~
dreaming.cpp:73:8: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
dfs(nodext11,0);
~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11768 KB |
Output is correct |
2 |
Correct |
60 ms |
11708 KB |
Output is correct |
3 |
Correct |
41 ms |
7800 KB |
Output is correct |
4 |
Correct |
12 ms |
2048 KB |
Output is correct |
5 |
Correct |
10 ms |
1408 KB |
Output is correct |
6 |
Correct |
17 ms |
2816 KB |
Output is correct |
7 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11768 KB |
Output is correct |
2 |
Correct |
60 ms |
11708 KB |
Output is correct |
3 |
Correct |
41 ms |
7800 KB |
Output is correct |
4 |
Correct |
12 ms |
2048 KB |
Output is correct |
5 |
Correct |
10 ms |
1408 KB |
Output is correct |
6 |
Correct |
17 ms |
2816 KB |
Output is correct |
7 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11768 KB |
Output is correct |
2 |
Correct |
60 ms |
11708 KB |
Output is correct |
3 |
Correct |
41 ms |
7800 KB |
Output is correct |
4 |
Correct |
12 ms |
2048 KB |
Output is correct |
5 |
Correct |
10 ms |
1408 KB |
Output is correct |
6 |
Correct |
17 ms |
2816 KB |
Output is correct |
7 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1090 ms |
6028 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11768 KB |
Output is correct |
2 |
Correct |
60 ms |
11708 KB |
Output is correct |
3 |
Correct |
41 ms |
7800 KB |
Output is correct |
4 |
Correct |
12 ms |
2048 KB |
Output is correct |
5 |
Correct |
10 ms |
1408 KB |
Output is correct |
6 |
Correct |
17 ms |
2816 KB |
Output is correct |
7 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
11768 KB |
Output is correct |
2 |
Correct |
60 ms |
11708 KB |
Output is correct |
3 |
Correct |
41 ms |
7800 KB |
Output is correct |
4 |
Correct |
12 ms |
2048 KB |
Output is correct |
5 |
Correct |
10 ms |
1408 KB |
Output is correct |
6 |
Correct |
17 ms |
2816 KB |
Output is correct |
7 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |