#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define DIM 100010
#define INF 2000000000
#include "dreaming.h"
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
const int N = 1e5 + 11;
const ll inf = 2e9;
int path[N], viz[N], s[N], dp_up[N], dp_down[N], nr;
vector<pi> v[N];
vector<int> c[N];
void dfs(int nod, int P){
c[nr].pb(nod);
viz[nod] = 1;
for(auto it : v[nod]){
if(it.ff == P)continue;
s[nod] = max(s[nod], s[it.ff] + it.ss);
}
int mx1 = 0, mx2 = 0;
for(auto it : v[nod]){
if(it.ff == P)continue;
if(s[it.ff] + it.ss > mx1){
mx2 = mx1;
mx1 = s[it.ff] + it.ss;
continue;
}
if(s[it.ff] + it.ss > mx2){
mx2 = s[it.ff] + it.ss;
continue;
}
}
dp_down[nod] = mx1 + mx2;
}
void dfs2 (int nod, int tata, int cost){
dp_up[nod] = max (dp_up[nod],dp_up[tata]+cost);
int maxim = 0, maxim2 = 0, p = 0;
for (int i=0;i<v[nod].size();i++){
int fiu = v[nod][i].first, cst = v[nod][i].second;
if (fiu == tata)
continue;
if (s[fiu]+cst > maxim){
maxim2 = maxim;
maxim = s[fiu]+cst, p = fiu;
} else
if (s[fiu]+cst > maxim2)
maxim2 = s[fiu]+cst;
}
for (int i=0;i<v[nod].size();i++){
int fiu = v[nod][i].first;
if (fiu == tata)
continue;
if (fiu == p)
dp_up[fiu] = max (dp_up[fiu],maxim2+v[nod][i].second);
else dp_up[fiu] = max (dp_up[fiu],maxim+v[nod][i].second);
}
for (int i=0;i<v[nod].size();i++){
int vecin = v[nod][i].first;
if (vecin != tata)
dfs2 (vecin,nod,v[nod][i].second);
}}
int travelTime (int n, int m, int l, int a[], int b[], int cost[]){
for(int i = 0; i <= n; i++){
v[i].clear();
}
for(int i = 0; i < m; i++){
int x = a[i], y = b[i], z = cost[i];
x++, y++;
v[x].push_back({y, z});
v[y].push_back({x, z});
}
memset(viz,0,sizeof(viz));
memset(dp_up,0,sizeof(dp_up));
memset(dp_down,0,sizeof(dp_down));
memset(s,0,sizeof(s));
for(int i = 1; i <= n; i++){
if(viz[i])continue;
nr++;
dfs(i, 0);
dfs2(i, 0, 0);
}
int ans = 0;
for(int i = 1;i <= nr; i++){
int mn = inf;
for(auto nod : c[i]){
int dist_max = max(dp_up[nod] + s[nod], dp_down[nod]);
ans = max(ans, dist_max);
int dist = max(s[nod], dp_up[nod]);
mn = min(mn,dist);
}
path[i] = mn;
}
sort(path + 1, path + nr + 1);
if(nr >= 2)ans = max(ans, path[nr] + path[nr - 1] + l);
if(nr >= 3)ans = max(ans, path[nr - 1] + path[nr - 2] + 2*l);
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:54: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]
54 | for (int i=0;i<v[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp:65: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]
65 | for (int i=0;i<v[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp:74: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]
74 | for (int i=0;i<v[nod].size();i++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
14840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
14840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
14840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
12160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
14840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
14840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |