#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5 ;
vector< vector< pair<int , int> > >adj(MAX) ;
int vis[MAX] , parent[MAX] , Distance[MAX];
int now , dist = 0 ;
void bfs(int node)
{
now = node , dist = 0 ;
queue< pair<int , pair<int , int> > >q ;
q.push({node , {0 , -1}}) ;
Distance[node] = 0 ;
parent[node] = node ;
while(!q.empty())
{
pair<int , pair<int , int> >p = q.front() ;
q.pop() ;
int cur = p.first , d = p.second.first , par = p.second.second ;
vis[cur] = 1 ;
parent[cur] = par ;
if(d > dist)
now = cur , dist = d ;
for(auto &child : adj[cur])
{
if(child.first == par)
continue;
Distance[child.first] = child.second ;
q.push({child.first , {d + child.second , cur}}) ;
}
}
return ;
}
int travelTime(int n , int m , int l , int A[] , int B[] , int T[] )
{
memset(parent , -1 , sizeof(parent)) ;
for(int i = 0 ; i < m ; ++i)
{
int x = A[i] , y = B[i] , c = T[i] ;
adj[x].push_back({y , c}) ;
adj[y].push_back({x , c}) ;
}
vector<int>v ;
//loop on every Tree
for(int i = 0 ; i < n ; ++i)
{
if(vis[i] == 1)
continue ;
//found new Tree
bfs(i) ;
int x = now ;
bfs(now) ;
int sum = 0 , cur = now;
for(int j = 0 ; j < n ; ++j)
{
sum += Distance[cur] ;
if(sum >= dist / 2)
break;
cur = parent[cur] ;
}
int a = max(sum , dist - sum) ;
sum -= Distance[cur] ;
a = min(a , max(sum , dist - sum)) ;
v.push_back(a) ;
}
sort(v.rbegin() , v.rend()) ;
if(v.size() == 1)
{
//all nodes are connected
bfs(1) ;
bfs(now) ;
return dist ;
}
int ans = v[0] + v[1] + l ;
if(v.size() >= 3)
ans = max(ans , v[1] + v[2] + 2 * l) ;
return ans ;
}
/*int main()
{
int n , m , l ;
cin>>n>>m>>l ;
int a[m] , b[m] , t[m] ;
for(int i = 0 ; i < m ; ++i)
cin>>a[i]>>b[i]>>t[i] ;
cout<<travelTime(n , m , l , a , b , t)<<"\n" ;
return 0 ;
}*/
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:56:13: warning: unused variable 'x' [-Wunused-variable]
int x = now ;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
6644 KB |
Output is correct |
2 |
Correct |
60 ms |
6572 KB |
Output is correct |
3 |
Correct |
55 ms |
6520 KB |
Output is correct |
4 |
Correct |
59 ms |
6520 KB |
Output is correct |
5 |
Correct |
58 ms |
6520 KB |
Output is correct |
6 |
Correct |
64 ms |
6908 KB |
Output is correct |
7 |
Correct |
60 ms |
6648 KB |
Output is correct |
8 |
Correct |
59 ms |
6528 KB |
Output is correct |
9 |
Correct |
54 ms |
6392 KB |
Output is correct |
10 |
Correct |
58 ms |
6648 KB |
Output is correct |
11 |
Correct |
4 ms |
3072 KB |
Output is correct |
12 |
Correct |
40 ms |
4596 KB |
Output is correct |
13 |
Correct |
38 ms |
4468 KB |
Output is correct |
14 |
Correct |
37 ms |
4476 KB |
Output is correct |
15 |
Correct |
38 ms |
4596 KB |
Output is correct |
16 |
Correct |
38 ms |
4472 KB |
Output is correct |
17 |
Correct |
37 ms |
4436 KB |
Output is correct |
18 |
Correct |
38 ms |
4604 KB |
Output is correct |
19 |
Correct |
38 ms |
4476 KB |
Output is correct |
20 |
Correct |
4 ms |
3072 KB |
Output is correct |
21 |
Correct |
4 ms |
3072 KB |
Output is correct |
22 |
Correct |
5 ms |
3072 KB |
Output is correct |
23 |
Correct |
39 ms |
4604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
7800 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |