#include<bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define pii pair<int, int>
struct Tree{
vector<vector<pii>> adj;
vector<int> longest;
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<int> ch;
ch.push_back(0);
ch.push_back(0);
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);
max_diam = max(max_diam, ch_ans.second);
}
}
sort(ch.begin(), ch.end());
return pii(ch[ch.size()-1], max(max_diam, ch[ch.size()-1]+ch[ch.size()-2]));
}
pii find_center(Tree& tr){
vector<int> dist;
dist.resize(tr.adj.size(), 1e9);
priority_queue<pii> pq;
for(int i = 0; i<tr.adj.size(); i++){
if(tr.adj[i].size() == 1){
pq.push({0, i});
dist[i] =0;
}
}
int last= 0;
while(pq.size()>0){
auto cur = pq.top();
pq.pop();
int d = -cur.first;
int id = cur.second;
if(d== dist[id]){
for(auto e: tr.adj[id]){
if(d+e.second<dist[e.first]){
pq.push({-d-e.second, e.first});
}
}
}
last = id;
}
return {last, get_len(last, -1, tr).first};
}
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;
for(int j = 0; j<forest.size(); j++){
forest[j].longest.resize(forest[j].adj.size());
pii d = get_len(0, -1, forest[j]);
forest[j].diameter = d.second;
forest[j].min_len= find_center(forest[j]).second;
//cout<<forest[j].diameter<<endl;
}
if(forest.size()==1){
return forest[0].diameter;
}
return max(forest[0].min_len + forest[1].min_len + L, max(forest[0].diameter, forest[1].diameter));
}
Compilation message
dreaming.cpp: In function 'std::pair<int, int> find_center(Tree&)':
dreaming.cpp:61: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]
61 | for(int i = 0; i<tr.adj.size(); i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:113:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Tree>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for(int j = 0; j<forest.size(); j++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
30404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3672 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
30404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
16172 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3672 KB |
Output is correct |
2 |
Correct |
1 ms |
3676 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3676 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
67 ms |
30404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |