#include "dreaming.h"
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
const int R=1e5+5;
vector<pair<int, int>> adj[R];
vector <bool> visited(R);
vector<int> trees[R], ans;
vector<pair<int, int>> par(R, {-1,0});
int t, distance;
typedef struct{
int mx, start, dist;
} path;
typedef struct {
int l , r, val;
} ANS;
ANS answer;
void dfs (int k){
trees[t].push_back(k);
visited[k]=true;
for(auto u:adj[k]){
if(visited[u.ff]) continue;
visited[u.ff]=true;
dfs(u.ff);
}
}
pair<int, int> max_dist(int k, int par){
vector <path> x(2);
x[0].mx=0; x[1].mx=0;
if(adj[k].size()<=1){
x[1].start=k;
x[0].start=k;
}
for(auto u:adj[k]){
if(u.ff==par) continue;
auto child=max_dist(u.ff,k);
if(child.ff >= x[0].mx){
x[1]=x[0];
x[0]={child.ff, child.ss, u.ss};
}
else if(child.ff >= x[1].mx){
x[1]={child.ff, child.ss, u.ss};
}
}
if(x[0].mx+x[1].mx+x[0].dist+x[1].dist > answer.val){
answer.val=x[0].mx+x[1].mx+x[0].dist+x[1].dist;
answer.l=x[0].start;
answer.r=x[1].start;
}
return {x[0].mx+x[0].dist, x[0].start};
}
void find_par(int k, int p){
for(auto u:adj[k]){
if(u.ff==p) continue;
par[u.ff]=make_pair(k, u.ss);
find_par(u.ff, k);
}
}
int find_max(vector<int> v){
answer.val=INT_MIN;
max_dist(v[0], -1);
int l=answer.l; int r=answer.r;
par.clear(); par.resize(R);
par[l]={-1,0};
find_par(l, -1);
/*for(int i=0; i<12; i++){
cout<<i<<" "<<par[i].ff << " "<<par[i].ss<<endl;
}*/
vector<pair<int, int>> way;
int k=answer.r;
while(k!=-1){
way.push_back({k, par[k].ss});
k=par[k].ff;
}
reverse(way.begin(), way.end());
int dist=answer.val;
int max_distance=0;
int x=0, y=INT_MAX; // x is distance y is max apart?
for(int i=0; i<way.size(); i++){
int k=max(abs(dist/2 - x), abs(dist/2 - (dist-x)));
if(k<y) {
y=k;
max_distance=max(x, dist-x);
}
x+=way[i].ss;
}
return max_distance;
//cout<<max_distance<<endl;
//cout<<"----"<<endl;
//return answer.val;
}
int travelTime(int n, int m, int l, int a[], int b[], int time[]) {
for(int i=0; i<m; i++){
adj[a[i]].push_back(make_pair(b[i], time[i]));
adj[b[i]].push_back(make_pair(a[i], time[i]));
}
t=0;
for(int i=0; i<n; i++){
if(visited[i]) continue;
dfs(i);
t++;
}
for(int i=0; i<t; i++){
int k=find_max(trees[i]);
//cout<<k<<endl;
ans.push_back(k);
}
sort(ans.begin(), ans.end());
reverse(ans.begin(), ans.end());
return ans[0]+ans[1]+l;
//return 42; // cause 42 is answer to everything???
}
Compilation message
dreaming.cpp: In function 'int find_max(std::vector<int>)':
dreaming.cpp:82:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i=0; i<way.size(); i++){
| ~^~~~~~~~~~~
dreaming.cpp:65:25: warning: unused variable 'r' [-Wunused-variable]
65 | int l=answer.l; int r=answer.r;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
18284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
18284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
18284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
10780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
18284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
18284 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |