#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18 + 10
#define INF (ll)1e9
#define MOD (ll)(1e9 + 7)
#include "dreaming.h"
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 2e5 + 10;
int n, m, l;
vector<pair<int, int>> adj[MAX];
int dp_in[MAX], dp_out[MAX];
bool visited[MAX];
void dfs1(vector<int> &save, int u, int p = -1){
visited[u] = true;
save.push_back(u);
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
dfs1(save, v, u);
maximize(dp_in[u], dp_in[v] + c);
}
}
void dfs2(int u, int p = -1){
int first = dp_out[u], second = 0;
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
if(dp_in[v] + c > first){
second = first;
first = dp_in[v] + c;
}
else maximize(second, dp_in[v] + c);
}
for(auto o: adj[u]){
int v = o.fi, c = o.se;
if(v == p) continue;
if(dp_in[v] + c == first)
dp_out[v] = second + c;
else
dp_out[v] = first + c;
dfs2(v, u);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]){
n = N;
m = M;
l = L;
for(int i = 0; i < m; i++){
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
vector<int> ord;
for(int i = 0; i < n; i++) if(!visited[i]){
vector<int> save;
dfs1(save, i);
dfs2(i);
int mi = INF * 2;
for(int x: save) minimize(mi, max(dp_in[x], dp_out[x]));
ord.push_back(mi);
}
sort(ALL(ord), greater<int>());
int ans = 0;
if(m + 1 == n) ans = ord[0];
else{
maximize(ans, ord[0] + ord[1] + l);
if(sz(ord) > 2) maximize(ans, ord[1] + ord[2] + 2 * l);
}
return ans;
}
//void solve(){
//
//}
//
//int main(){
// ios_base::sync_with_stdio(0); cin.tie(0);
//// freopen("bdbang.inp","r",stdin);
//// freopen("bdbang.out","w",stdout);
//
// cout << travelTime(12, 8, 2, {0,8,2,5,5,1,1,10}, {8,2,7,11,1,3,9,6}, {4,2,4,3,7,1,5,3});
//
// return 0;
//}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
15460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
15460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
9060 KB |
Output is correct |
2 |
Correct |
19 ms |
8924 KB |
Output is correct |
3 |
Correct |
22 ms |
8928 KB |
Output is correct |
4 |
Correct |
17 ms |
9176 KB |
Output is correct |
5 |
Correct |
20 ms |
8872 KB |
Output is correct |
6 |
Correct |
18 ms |
9440 KB |
Output is correct |
7 |
Correct |
23 ms |
9108 KB |
Output is correct |
8 |
Correct |
20 ms |
8984 KB |
Output is correct |
9 |
Correct |
26 ms |
8980 KB |
Output is correct |
10 |
Correct |
21 ms |
9172 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
7 ms |
6360 KB |
Output is correct |
13 |
Correct |
7 ms |
6560 KB |
Output is correct |
14 |
Correct |
7 ms |
6360 KB |
Output is correct |
15 |
Correct |
7 ms |
6360 KB |
Output is correct |
16 |
Correct |
7 ms |
6428 KB |
Output is correct |
17 |
Correct |
7 ms |
6104 KB |
Output is correct |
18 |
Correct |
8 ms |
6360 KB |
Output is correct |
19 |
Correct |
13 ms |
6224 KB |
Output is correct |
20 |
Correct |
2 ms |
4960 KB |
Output is correct |
21 |
Correct |
3 ms |
4960 KB |
Output is correct |
22 |
Correct |
2 ms |
5216 KB |
Output is correct |
23 |
Correct |
7 ms |
6424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
5140 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
40 ms |
15460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |