This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define pii pair<int, int>
struct Tree{
vector<vector<pii>> adj;
vector<pair<pii, pii>> longest;
vector<int> furthest;
int min_len = 1e9;
int diameter= 0;
};
const int MAX_N= 1e5;
vector<pii> madj[MAX_N+2];
bool vis[MAX_N+2];
vector<Tree> forest;
Tree sTree;
void get_tree(int id, int anc){
//cout<<id<<endl;
vis[id] = true;
int tree_id = sTree.adj.size();
sTree.adj.push_back(vector<pii>(0));
for(auto e: madj[id]){
if(e.first!=anc){
int future_id = sTree.adj.size();
get_tree(e.first, id);
sTree.adj[tree_id].push_back(pii(future_id, e.second));
sTree.adj[future_id].push_back(pii(tree_id, e.second));
}
}
//cout<<id<<" done "<<endl;
}
pii get_len(int id, int anc, Tree& tr){
vector<pii> ch;
ch.push_back({0, -1});
ch.push_back({0, -1});
int max_diam = 0;
for(auto e: tr.adj[id]){
if(e.first!=anc){
pii ch_ans = get_len(e.first, id, tr);
ch.push_back({ch_ans.first+e.second, e.first});
max_diam = max(max_diam, ch_ans.second);
}
}
sort(ch.begin(), ch.end());
tr.longest[id] = {ch[ch.size()-1], ch[ch.size()-2]};
return pii(ch[ch.size()-1].first, max(max_diam, ch[ch.size()-1].first+ch[ch.size()-2].first));
}
void dp_reroot(int id, int anc, int from_above, Tree& tr){
//cout<<id<<endl;
tr.furthest[id] = max(from_above, tr.longest[id].first.first);
//cout<<id<<" "<<tr.furthest[id]<<endl;
vector<pii> cands;
cands.push_back(tr.longest[id].first);
cands.push_back(tr.longest[id].second);
cands.push_back({from_above, -1});
sort(cands.begin(), cands.end());
reverse(cands.begin(), cands.end());
for(auto e: tr.adj[id]){
if(e.first!=anc){
if(e.first == cands[0].second){
dp_reroot(e.first, id, cands[1].first + e.second, tr);
}
else{
dp_reroot(e.first, id, cands[0].first + e.second, tr);
}
}
}
}
pii find_center(Tree& tr){
dp_reroot(0, -1, 0, tr);
int res_id = 0;
int res_dist = 1e9;
for(int i = 0; i<tr.adj.size(); i++){
if(tr.furthest[i]<res_dist){
res_id =i;
res_dist = tr.furthest[i];
}
}
return {res_id, res_dist};
}
signed travelTime(signed N,signed M, signed L,signed A[], signed B[], signed T[]) {
for(int i =0; i<M; i++){
madj[A[i]].push_back(pii(B[i], T[i]));
madj[B[i]].push_back(pii(A[i], T[i]));
}
fill(&vis[0], &vis[MAX_N], false);
for(int i = 0; i<N; i++){
/*for(auto e: madj[i]){
cout<<e.first<<" ";
}
cout<<endl;*/
if(!vis[i]){
//cerr<<i<<endl;
sTree.adj.clear();
get_tree(i, -1);
forest.push_back(sTree);
}
}
//cout<<forest.size()<<endl;
vector<int> radius;
int res= 0;
for(int j = 0; j<forest.size(); j++){
forest[j].longest.resize(forest[j].adj.size());
forest[j].furthest.resize(forest[j].adj.size());
pii d = get_len(0, -1, forest[j]);
forest[j].diameter = d.second;
res = max(res, forest[j].diameter);
forest[j].min_len= find_center(forest[j]).second;
radius.push_back(forest[j].min_len);
//cout<<forest[j].diameter<<endl;
}
sort(radius.begin(), radius.end());
if(radius.size()>1){
res =max(res, radius[radius.size()-1]+radius[radius.size()-2]+L);
}
if(radius.size()>2){
res =max(res, radius[radius.size()-2]+radius[radius.size()-3]+L*2);
}
return res;
}
Compilation message (stderr)
dreaming.cpp: In function 'std::pair<int, int> find_center(Tree&)':
dreaming.cpp:86:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i = 0; i<tr.adj.size(); i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:123:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Tree>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int j = 0; j<forest.size(); j++){
| ~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |