#include "dreaming.h"
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
const int R=1e5+5;
int mx;
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);
mx=max(mx, answer.val);
/*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());
if(t>2) mx=max(ans[1]+ans[2]+2*l, mx);
if(t>1) mx= max(mx, ans[0]+ans[1]+l);
return mx;
}
/*#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];
int dpD1[R], dpD2[R], dpU[R], mn,mx;
vector<bool> visited(R);
vector<int> ans;
void dfsDown(int k){
visited[k]=true;
for(auto u:adj[k]){
int dist=u.ss; int ch=u.ff;
if(visited[ch]) continue;
dfsDown(ch);
if(dpD1[ch]+dist > dpD1[k]){
dpD2[k]=dpD1[k];
dpD1[k]=dpD1[ch]+dist;
}
else if(dpD1[ch]+dist > dpD2[k]){
dpD2[k]=dpD1[ch]+dist;
}
}
}
void dfsUp(int k, int par){
for(auto u:adj[k]){
int dist=u.ss; int ch=u.ff;
if(ch==par) continue;
dpU[ch]=dpU[k]+dist;
if(dpD1[k]==dpD1[ch]+dist) dpU[ch]=max(dpU[ch], dpD2[k]+dist);
else dpU[ch]=max(dpU[ch], dpD1[k]+dist);
//cout<<"child: " << ch << "parent: "<< k<< "UP" << dpU[ch]<<endl;
dfsUp(ch, k);
}
mn=min(mn, max(dpD1[k], dpU[k]));
mx=max(mx, dpD1[k]+dpU[k]);
}
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]));
}
for(int i=0; i<n; i++){
if(visited[i]) continue;
mn=INT_MAX;
dfsDown(i);
dfsUp(i, -1);
//cout<<mn<<endl;
ans.push_back(mn);
}
sort(ans.begin(), ans.end());
reverse(ans.begin(), ans.end());
if(ans.size()>2) mx=max(mx, ans[1]+ans[2]+2*l);
if(ans.size()>1) mx=max(mx, ans[0]+ans[1]+l);
return mx;
return 42;
}
*/
Compilation message
dreaming.cpp: In function 'int find_max(std::vector<int>)':
dreaming.cpp:84: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]
84 | for(int i=0; i<way.size(); i++){
| ~^~~~~~~~~~~
dreaming.cpp:66:25: warning: unused variable 'r' [-Wunused-variable]
66 | int l=answer.l; int r=answer.r;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17260 KB |
Output is correct |
2 |
Correct |
66 ms |
17256 KB |
Output is correct |
3 |
Correct |
44 ms |
14056 KB |
Output is correct |
4 |
Correct |
12 ms |
7660 KB |
Output is correct |
5 |
Correct |
11 ms |
6764 KB |
Output is correct |
6 |
Correct |
19 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
5868 KB |
Output is correct |
8 |
Correct |
33 ms |
9988 KB |
Output is correct |
9 |
Correct |
44 ms |
12396 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
80 ms |
12964 KB |
Output is correct |
12 |
Correct |
80 ms |
15332 KB |
Output is correct |
13 |
Correct |
5 ms |
5868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17260 KB |
Output is correct |
2 |
Correct |
66 ms |
17256 KB |
Output is correct |
3 |
Correct |
44 ms |
14056 KB |
Output is correct |
4 |
Correct |
12 ms |
7660 KB |
Output is correct |
5 |
Correct |
11 ms |
6764 KB |
Output is correct |
6 |
Correct |
19 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
5868 KB |
Output is correct |
8 |
Correct |
33 ms |
9988 KB |
Output is correct |
9 |
Correct |
44 ms |
12396 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
80 ms |
12964 KB |
Output is correct |
12 |
Correct |
80 ms |
15332 KB |
Output is correct |
13 |
Correct |
5 ms |
5868 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Incorrect |
5 ms |
5888 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17260 KB |
Output is correct |
2 |
Correct |
66 ms |
17256 KB |
Output is correct |
3 |
Correct |
44 ms |
14056 KB |
Output is correct |
4 |
Correct |
12 ms |
7660 KB |
Output is correct |
5 |
Correct |
11 ms |
6764 KB |
Output is correct |
6 |
Correct |
19 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
5868 KB |
Output is correct |
8 |
Correct |
33 ms |
9988 KB |
Output is correct |
9 |
Correct |
44 ms |
12396 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
80 ms |
12964 KB |
Output is correct |
12 |
Correct |
80 ms |
15332 KB |
Output is correct |
13 |
Correct |
5 ms |
5868 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Incorrect |
5 ms |
5888 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1085 ms |
10604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17260 KB |
Output is correct |
2 |
Correct |
66 ms |
17256 KB |
Output is correct |
3 |
Correct |
44 ms |
14056 KB |
Output is correct |
4 |
Correct |
12 ms |
7660 KB |
Output is correct |
5 |
Correct |
11 ms |
6764 KB |
Output is correct |
6 |
Correct |
19 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
5868 KB |
Output is correct |
8 |
Correct |
33 ms |
9988 KB |
Output is correct |
9 |
Correct |
44 ms |
12396 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
80 ms |
12964 KB |
Output is correct |
12 |
Correct |
80 ms |
15332 KB |
Output is correct |
13 |
Correct |
5 ms |
5868 KB |
Output is correct |
14 |
Incorrect |
21 ms |
5868 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
17260 KB |
Output is correct |
2 |
Correct |
66 ms |
17256 KB |
Output is correct |
3 |
Correct |
44 ms |
14056 KB |
Output is correct |
4 |
Correct |
12 ms |
7660 KB |
Output is correct |
5 |
Correct |
11 ms |
6764 KB |
Output is correct |
6 |
Correct |
19 ms |
8556 KB |
Output is correct |
7 |
Correct |
5 ms |
5868 KB |
Output is correct |
8 |
Correct |
33 ms |
9988 KB |
Output is correct |
9 |
Correct |
44 ms |
12396 KB |
Output is correct |
10 |
Correct |
5 ms |
5868 KB |
Output is correct |
11 |
Correct |
80 ms |
12964 KB |
Output is correct |
12 |
Correct |
80 ms |
15332 KB |
Output is correct |
13 |
Correct |
5 ms |
5868 KB |
Output is correct |
14 |
Correct |
4 ms |
5868 KB |
Output is correct |
15 |
Incorrect |
5 ms |
5888 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |