#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
int lau;
stack<int> S;
vector<vector<pair<int,int>>> adjlist;
vector<int> maxin;
vector<int> maxout;
vector<int> visited;
int n,m;
void dfs(int node) {
S.push(node);
visited[node] = 1;
int maxmaxin=0;
for (pair<int,int> nei:adjlist[node]) {
if (visited[nei.first]) {
continue;
}
dfs(nei.first);
maxin[node] = max(maxin[node], maxin[nei.first]+nei.second); //maximum path from the node to any
//node in its subtree
}
}
void dfs2(int node) {
visited[node] = 1;
int maxmaxin=0;
vector<int> values;
vector<pair<int,int>> children;
int mout = maxout[node];
for (pair<int,int> nei:adjlist[node]) {
if (visited[nei.first]) {
continue;
}
children.push_back(nei);
values.push_back(nei.second+maxin[nei.first]);
}
int c=children.size();
vector<int> prefmax(c+1,0);
vector<int> suffmax(c+1,0);
for (int i=0; i<c; i++) {
prefmax[i+1] = max(prefmax[i], values[i]);
}
for (int i=c; i>=1; i--) {
suffmax[i-1] = max(suffmax[i], values[i-1]);
}
for (int i=0; i<c; i++) {
pair<int,int> nei=children[i];
if (visited[nei.first]) {
continue;
}
maxout[nei.first] = max(maxout[node],max(prefmax[i],suffmax[i+1]))+nei.second;
dfs2(nei.first);
}
}
signed travelTime(signed N, signed M, signed L, signed A[], signed B[], signed T[]) {
n=N;
m=M;
lau=L;
adjlist=vector<vector<pair<int,int>>>(N);
visited=vector<int>(N,0);
maxin=vector<int>(N,0);
maxout=vector<int>(N,0);
for (int i=0; i<M; i++) {
adjlist[A[i]].push_back({B[i],T[i]});
adjlist[B[i]].push_back({A[i],T[i]});
}
vector<vector<int>> conn;
for (int i=0; i<n; i++) {
if (!visited[i]) {
dfs(i);
vector<int> curcon;
while (S.size()) {
curcon.push_back(S.top());
S.pop();
}
conn.push_back(curcon);
}
}
visited=vector<int>(N,0);
for (int i=0; i<n; i++) {
if (!visited[i]) {
dfs2(i);
}
}
int c=conn.size();
int INF=1000000000007;
vector<int> weights(c,INF);
int k=0;
for (auto i:conn) {
for (auto j:i) {
weights[k]=min(weights[k],max(maxin[j],maxout[j]));
}
k++;
}
sort(weights.begin(), weights.end());
reverse(weights.begin(), weights.end());
/*for (auto i:weights) {
cout << i << " ";
}
cout << endl;*/
if (weights.size()==1) {
return weights[0];
}
else if (weights.size()==2) {
return weights[0]+weights[1]+L;
}
else {
return max(weights[1]+weights[2]+2*L, weights[0]+weights[1]+L);
}
}
Compilation message
dreaming.cpp: In function 'void dfs(long long int)':
dreaming.cpp:17:9: warning: unused variable 'maxmaxin' [-Wunused-variable]
17 | int maxmaxin=0;
| ^~~~~~~~
dreaming.cpp: In function 'void dfs2(long long int)':
dreaming.cpp:30:9: warning: unused variable 'maxmaxin' [-Wunused-variable]
30 | int maxmaxin=0;
| ^~~~~~~~
dreaming.cpp:33:9: warning: unused variable 'mout' [-Wunused-variable]
33 | int mout = maxout[node];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
26052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
26052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
11140 KB |
Output is correct |
2 |
Correct |
28 ms |
11280 KB |
Output is correct |
3 |
Correct |
30 ms |
11280 KB |
Output is correct |
4 |
Correct |
28 ms |
11276 KB |
Output is correct |
5 |
Correct |
29 ms |
11280 KB |
Output is correct |
6 |
Correct |
38 ms |
13928 KB |
Output is correct |
7 |
Correct |
29 ms |
11792 KB |
Output is correct |
8 |
Correct |
27 ms |
11024 KB |
Output is correct |
9 |
Correct |
28 ms |
11024 KB |
Output is correct |
10 |
Correct |
39 ms |
11792 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
19 ms |
11896 KB |
Output is correct |
13 |
Correct |
17 ms |
11532 KB |
Output is correct |
14 |
Correct |
17 ms |
11532 KB |
Output is correct |
15 |
Correct |
18 ms |
12028 KB |
Output is correct |
16 |
Correct |
18 ms |
11788 KB |
Output is correct |
17 |
Correct |
21 ms |
11664 KB |
Output is correct |
18 |
Correct |
17 ms |
11432 KB |
Output is correct |
19 |
Correct |
18 ms |
11520 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
18 ms |
11276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
26052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |