#include"dreaming.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
vector<pair<int,int>>nodes[100000];
bool vis[100000];
vector<int>node_list;
void dfs(int node){
if(vis[node])
return;
vis[node]=true;
node_list.push_back(node);
for(auto ne:nodes[node])
dfs(ne.first);
}
map<pair<int,int>,int>dp;
int get_max_dist(int node,int prev){
if(dp.count({node,prev}))
return dp[{node,prev}];
int res=0;
for(auto ne:nodes[node]){
if(ne.first==prev)
continue;
res=max(res,get_max_dist(ne.first,node)+ne.second);
}
dp[{node,prev}]=res;
return res;
}
int get_diameter(int root){
node_list.clear();
dfs(root);
/*cout<<"- ";
for(int i:node_list)
cout<<i<<" ";
cout<<"\n";*/
int res=2e9;
for(int node:node_list){
//cout<<node<<": "<<get_max_dist(node,node)<<"\n";
res=min(res,get_max_dist(node,node));
}
return res;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
for(int i=0;i<M;i++){
nodes[A[i]].emplace_back(B[i],T[i]);
nodes[B[i]].emplace_back(A[i],T[i]);
}
vector<int>dists;
for(int i=0;i<N;i++)
if(!vis[i])
dists.push_back(get_diameter(i));
/*for(int i:dists)
cout<<i<<" ";
cout<<"\n";*/
sort(dists.begin(),dists.end());
reverse(dists.begin(),dists.end());
if(dists.size()==1)
return dists[0];
else
return dists[0]+dists[1]+L;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
181 ms |
29788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
181 ms |
29788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
15820 KB |
Output is correct |
2 |
Incorrect |
58 ms |
15944 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
3672 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
181 ms |
29788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |