#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
#define fi first
#define se second
#define INF (int)(1e9)
vector<vector<pair<int,int> > > vec;
bool vis[100005];
int maxi=0,a,idx=0;
vector<pair<int,int> > tree_d;
pair<int,int> p[100005],center[100005];
int comp[100005];
vector<int> nodes;
int dfs(int node,int d){
int ans=1;
nodes.push_back(node);
if(d>maxi)
a=node,maxi=d;
vis[node]=1;
for(auto child:vec[node])
if(child.fi!=p[node].fi){
vis[child.fi]=1;
p[child.fi]={node,child.se};
ans+=dfs(child.fi,d+child.se);
}
return ans;
}
pair<int,int> findCenter(int node){
maxi=0;
int s=dfs(node,0);
for(auto i:nodes)
p[i]={-1,-1};
nodes.clear();
int B=a,sum=0,mini=INF,c=node,sh=INF;
maxi=0;
dfs(B,0);
nodes.clear();
for(;a!=-1;sum+=p[a].se,a=p[a].fi){
if(abs(maxi/2-sum)<mini)
mini=abs(maxi/2-sum),c=a,sh=sum;
}
tree_d.push_back({s,idx});
return {c,max(maxi-sh,0)};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vec.resize(N);
for (int i = 0; i < M; ++i)
{
vec[A[i]].push_back({B[i],T[i]});
vec[B[i]].push_back({A[i],T[i]});
}
for (int i = 0; i < N; ++i)
if(!vis[i])
center[idx++]=findCenter(i);
sort(tree_d.begin(),tree_d.end(),greater<pair<int,int> >());
/*int centroid=center[0].first,ans=tree_d[0].fi,max_d=0;
for (int i = 0; i < idx-1; ++i)
{
if(center[tree_d[i+1].se].se>center[tree_d[i].se].se)
centroid=center[tree_d[i+1].se].fi;
ans=max(ans,center[tree_d[i+1].se].se+center[tree_d[i].se].se+2);
}*/
for (int i = 1; i < tree_d.size(); ++i)
{
vec[center[tree_d[i].se].fi].push_back({center[tree_d[0].se].fi,2});
vec[center[tree_d[0].se].fi].push_back({center[tree_d[i].se].fi,2});
}
maxi=0;
memset(p,-1,sizeof p);
dfs(0,0);
nodes.clear();
int b=a;
memset(p,-1,sizeof p);
maxi=0;
dfs(b,0);
return maxi;
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:63:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < tree_d.size(); ++i)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
13556 KB |
Output is correct |
2 |
Correct |
69 ms |
13428 KB |
Output is correct |
3 |
Correct |
45 ms |
9204 KB |
Output is correct |
4 |
Correct |
11 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
2176 KB |
Output is correct |
6 |
Correct |
19 ms |
3968 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
13556 KB |
Output is correct |
2 |
Correct |
69 ms |
13428 KB |
Output is correct |
3 |
Correct |
45 ms |
9204 KB |
Output is correct |
4 |
Correct |
11 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
2176 KB |
Output is correct |
6 |
Correct |
19 ms |
3968 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
13556 KB |
Output is correct |
2 |
Correct |
69 ms |
13428 KB |
Output is correct |
3 |
Correct |
45 ms |
9204 KB |
Output is correct |
4 |
Correct |
11 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
2176 KB |
Output is correct |
6 |
Correct |
19 ms |
3968 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
23 ms |
9628 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
13556 KB |
Output is correct |
2 |
Correct |
69 ms |
13428 KB |
Output is correct |
3 |
Correct |
45 ms |
9204 KB |
Output is correct |
4 |
Correct |
11 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
2176 KB |
Output is correct |
6 |
Correct |
19 ms |
3968 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
13556 KB |
Output is correct |
2 |
Correct |
69 ms |
13428 KB |
Output is correct |
3 |
Correct |
45 ms |
9204 KB |
Output is correct |
4 |
Correct |
11 ms |
2944 KB |
Output is correct |
5 |
Correct |
10 ms |
2176 KB |
Output is correct |
6 |
Correct |
19 ms |
3968 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1152 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |