#include"dreaming.h"
//#include"grader.c"
#include<bits/stdc++.h>
#define fr first
#define sc second
using namespace std;
vector<pair<int,int>>g[100005];
bool vis[100005];
int mx[100005],mx2[100005],id[100005],mm,idd;
int dfs(int v,int p){
vis[v]=1;
int i=0,x;
for(;i<g[v].size();i++)
if(g[v][i].fr!=p){
x=dfs(g[v][i].fr,v)+g[v][i].sc;
if(x>mx2[v]){
mx2[v]=x;
id[v]=g[v][i].fr;
}
}
return mx2[v];
}
void go(int v,int p,int val,int depth){
mx[v]=max(val,depth);
int m=0,i=0;
for(;i<g[v].size();i++)
if(g[v][i].fr!=p)
if(g[v][i].fr!=id[v]){
m=max(m,mx2[g[v][i].fr]+g[v][i].sc);
go(g[v][i].fr,v,max(mx2[v],val)+g[v][i].sc,depth+g[v][i].sc);
}
if(id[v]!=-1)go(id[v],v,max(m,val)+g[v][i].sc,depth+g[v][i].sc);
}
void calc(int v,int p){
if(max(mx2[v],mx[v])<mm){
mm=max(mx2[v],mx[v]);
idd=v;
}
for(auto to:g[v])if(to.fr!=p)calc(to.fr,v);
}
void diam(int v,int p,int depth){
if(depth>mm){
mm=depth;
idd=v;
}
for(auto to:g[v])if(to.fr!=p)diam(to.fr,v,depth+to.sc);
}
int travelTime(int n,int m,int l,int x[],int y[],int z[]){
memset(id,-1,sizeof(id));
for(int i=0;i<m;i++){
g[x[i]].push_back({y[i],z[i]});
g[y[i]].push_back({x[i],z[i]});
}
set<pair<int,int>>st;
g[n].push_back({n,0});
for(int i=0;i<n;i++)if(!vis[i]){
g[n][0].fr=i;
dfs(n,n);
go(n,n,0,0);
mm=2e9;
calc(n,n);
st.insert({mm,idd});
}
g[n].clear();
while(st.size()>1){
auto a=*st.begin();
st.erase(st.begin());
auto b=*st.begin();
st.erase(st.begin());
if(max(a.fr,b.fr+l)<max(b.fr,a.fr+l))
st.insert({max(a.fr,b.fr+l),a.sc});
else
st.insert({max(b.fr,a.fr+l),b.sc});
g[a.sc].push_back({b.sc,l});
g[b.sc].push_back({a.sc,l});
}
mm=0;
diam(0,0,0);
mm=0;
diam(idd,idd,0);
return mm;
}
Compilation message
dreaming.cpp: In function 'int dfs(int, int)':
dreaming.cpp:13:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(;i<g[v].size();i++)
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'void go(int, int, int, int)':
dreaming.cpp:26:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(;i<g[v].size();i++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12152 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
3 |
Correct |
49 ms |
9464 KB |
Output is correct |
4 |
Correct |
11 ms |
4352 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
18 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12152 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
3 |
Correct |
49 ms |
9464 KB |
Output is correct |
4 |
Correct |
11 ms |
4352 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
18 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12152 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
3 |
Correct |
49 ms |
9464 KB |
Output is correct |
4 |
Correct |
11 ms |
4352 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
18 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
6272 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12152 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
3 |
Correct |
49 ms |
9464 KB |
Output is correct |
4 |
Correct |
11 ms |
4352 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
18 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
12152 KB |
Output is correct |
2 |
Correct |
71 ms |
12024 KB |
Output is correct |
3 |
Correct |
49 ms |
9464 KB |
Output is correct |
4 |
Correct |
11 ms |
4352 KB |
Output is correct |
5 |
Correct |
9 ms |
3840 KB |
Output is correct |
6 |
Correct |
18 ms |
4992 KB |
Output is correct |
7 |
Incorrect |
2 ms |
3072 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |