#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100000];
vector<int> adjw[100000];
int sub[100000];
bool v[100000];
int dfs1(int now, int from){
v[now] = true;
sub[now] = 0;
for(int i = 0; i<adj[now].size(); i++){
int to = adj[now][i];
if(to==from){
continue;
}
sub[now] = max(sub[now],adjw[now][i] + dfs1(to, now));
}
return sub[now];
}
int dfs2(int now, int from, int above){
int ret = 1000000007;
vector<pair<int, int> > li;
//deep, loc
for(int i = 0; i<adj[now].size(); i++){
int to = adj[now][i];
if(to==from){
continue;
}
li.push_back(make_pair(sub[to]+adjw[now][i],to));
}
if(li.size()==0){
return above;
}
int maxi = 0;
for(int i = 1; i<li.size(); i++){
if(li[i].first > li[maxi].first){
maxi = i;
}
}
ret = min(ret, max(above,li[maxi].first));
for(int i = 0; i<li.size(); i++){
if(i==maxi){
continue;
}
above = max(above,li[i].first);
}
above += adjw[now][maxi];
ret = min(ret, dfs2(adj[now][maxi],now,above));
return ret;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
for(int i = 0; i<m; i++){
adj[a[i]].push_back(b[i]);
adj[b[i]].push_back(a[i]);
adjw[a[i]].push_back(t[i]);
adjw[b[i]].push_back(t[i]);
}
for(int i = 0; i<n; i++){
v[i] = false;
}
vector<int> all;
for(int i = 0; i<n; i++){
if(adj[i].size()==0){
v[i] = true;
all.push_back(0);
}
if(v[i] || adj[i].size()!=1){
continue;
}
dfs1(i,-1);
all.push_back(dfs2(i,-1,0));
}
for(int i = 0; i<n; i++){
assert(v[i]);
}
sort(all.begin(),all.end());
int ans = 0;
if(all.size()>0){
ans = max(ans,all[all.size()-1]);
}
if(all.size()>1){
ans = max(ans,all[all.size()-1] + all[all.size()-2] + l);
}
if(all.size()>2){
ans = max(ans,all[all.size()-3] + all[all.size()-2] + l + l);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'int dfs1(int, int)':
dreaming.cpp:11:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<adj[now].size(); i++){
~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int dfs2(int, int, int)':
dreaming.cpp:24:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<adj[now].size(); i++){
~^~~~~~~~~~~~~~~~
dreaming.cpp:35:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i<li.size(); i++){
~^~~~~~~~~~
dreaming.cpp:41:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<li.size(); i++){
~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
26616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
26616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
26616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
10104 KB |
Output is correct |
2 |
Correct |
41 ms |
10232 KB |
Output is correct |
3 |
Correct |
45 ms |
10120 KB |
Output is correct |
4 |
Correct |
46 ms |
10104 KB |
Output is correct |
5 |
Correct |
46 ms |
10088 KB |
Output is correct |
6 |
Correct |
53 ms |
10740 KB |
Output is correct |
7 |
Correct |
57 ms |
10380 KB |
Output is correct |
8 |
Correct |
44 ms |
10104 KB |
Output is correct |
9 |
Correct |
45 ms |
10104 KB |
Output is correct |
10 |
Correct |
47 ms |
10360 KB |
Output is correct |
11 |
Correct |
5 ms |
4992 KB |
Output is correct |
12 |
Correct |
9 ms |
6140 KB |
Output is correct |
13 |
Correct |
10 ms |
6272 KB |
Output is correct |
14 |
Correct |
9 ms |
6268 KB |
Output is correct |
15 |
Correct |
9 ms |
6268 KB |
Output is correct |
16 |
Correct |
9 ms |
6264 KB |
Output is correct |
17 |
Correct |
12 ms |
6012 KB |
Output is correct |
18 |
Correct |
10 ms |
6272 KB |
Output is correct |
19 |
Correct |
9 ms |
6140 KB |
Output is correct |
20 |
Correct |
6 ms |
4992 KB |
Output is correct |
21 |
Correct |
6 ms |
4992 KB |
Output is correct |
22 |
Correct |
6 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
6140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
26616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
26616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |