#include <bits/stdc++.h>
#include "dreaming.h"
#define MAXN 100001
using namespace std;
int maxpar1[MAXN];
int node1[MAXN];
int maxpar2[MAXN];
int maxlength[MAXN];
int maxbyp[MAXN];
bool vis[MAXN];
int tree[MAXN];
int cont;
vector<int> mintree;
vector<pair<int, int>> adj[MAXN];
void dfs1(int v, int p){
tree[v]=cont;
vis[v]=true;
if(adj[v].size()==1&&p!=v){
maxpar1[v]=0;
maxpar2[v]=0;
}
else{
for (auto u : adj[v]) {
if (u.first != p){
dfs1(u.first, v);
if(maxpar1[v]<maxpar1[u.first]+u.second){
maxpar2[v]=maxpar1[v];
maxpar1[v]=maxpar1[u.first]+u.second;
node1[v]=u.first;
}
else if(maxpar2[v]<maxpar1[u.first]+u.second){
maxpar2[v]=maxpar1[u.first]+u.second;
}
}
}
}
maxlength[v]=maxpar1[v];
}
void dfs2(int v, int p, int d){
vis[v]=true;
if(v!=p){
if(node1[p]!=v){
maxbyp[v]= max(maxpar1[p]+d, maxbyp[p]+d);
maxlength[v]=max(maxlength[v], maxbyp[v]);
}
else{
maxbyp[v]=max(maxpar2[p]+d, maxbyp[p]+d);
maxlength[v]=max(maxlength[v], maxbyp[v]);
}
}
for (auto u : adj[v]) {
if (u.first != p){
dfs2(u.first, v, u.second);
}
}
}
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], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
mintree = {};
for(int i = 0; i<N; i++){
maxpar1[i]=0;
maxpar2[i]=0;
maxlength[i]=0;
maxbyp[i]=0;
vis[i]=false;
tree[i]=-1;
node1[i]=-1;
}
cont =0;
for(int i =0; i<N; i++){
if(!vis[i]){
mintree.push_back(INT_MAX);
dfs1(i,i);
cont++;
}
}
for(int i =0; i<N; i++) vis[i]=false;
for(int i =0; i<N; i++){
if(!vis[i]){
dfs2(i, i, 0);
}
}
int maxbest1=-1;
int maxbest2=-1;
int maxbest3=-1;
for(int i =0; i<N; i++){
if(maxlength[i]<mintree[tree[i]]){
mintree[tree[i]]=maxlength[i];
}
}
for(int j: mintree){
if(j>maxbest1){
maxbest3=maxbest2;
maxbest2=maxbest1;
maxbest1=j;
}
else if(j>maxbest2){
maxbest3=maxbest2;
maxbest2=j;
}
else if(j>maxbest3){
maxbest3=j;
}
}
int sol;
if(maxbest3!=-1){
sol =max(maxbest1+L+maxbest2, maxbest2+maxbest3+2*L);
}
else sol=maxbest1+L+maxbest2;
return sol;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
8524 KB |
Output is correct |
2 |
Correct |
14 ms |
8504 KB |
Output is correct |
3 |
Correct |
18 ms |
8540 KB |
Output is correct |
4 |
Correct |
14 ms |
8540 KB |
Output is correct |
5 |
Correct |
13 ms |
8536 KB |
Output is correct |
6 |
Correct |
15 ms |
8928 KB |
Output is correct |
7 |
Correct |
13 ms |
8652 KB |
Output is correct |
8 |
Correct |
13 ms |
8416 KB |
Output is correct |
9 |
Correct |
13 ms |
8408 KB |
Output is correct |
10 |
Correct |
14 ms |
8664 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
3 ms |
6872 KB |
Output is correct |
13 |
Correct |
3 ms |
6872 KB |
Output is correct |
14 |
Correct |
3 ms |
6872 KB |
Output is correct |
15 |
Correct |
3 ms |
6716 KB |
Output is correct |
16 |
Correct |
3 ms |
6872 KB |
Output is correct |
17 |
Correct |
3 ms |
6868 KB |
Output is correct |
18 |
Correct |
3 ms |
6872 KB |
Output is correct |
19 |
Correct |
3 ms |
6872 KB |
Output is correct |
20 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |