#include "dreaming.h"
#include<bits/stdc++.h>
#define pii pair<int, int>
#define f first
#define s second
using namespace std;
vector<pii> adj[100005];
vector<int> group[100005];
int maxdis[100005], n;
bool vis[100005], vis0[100005];
void dfs(int cur, int prev, int dis) {
maxdis[cur] = max(maxdis[cur], dis);
for(pii next:adj[cur]) {
if(prev==next.f) continue;
dfs(next.f, cur, dis+next.s);
}
}
int mnGroup(int i) {
int last = n;
for(int j=1;j<group[i].size();j++) {
int st = n;
for(int node:group[i]) {
if(!vis[node] && maxdis[node]>=maxdis[st] && maxdis[node]>=maxdis[last]) st = node;
}
if(st==n) break;
dfs(st, -1, 0);
vis[st] = 1;
last = st;
}
int ret = INT_MAX;
for(int mxdis:group[i]) {
ret = min(ret, maxdis[mxdis]);
}
return ret;
}
void inGroup(int root, int cur, int prev) {
group[root].push_back(cur);
vis0[cur] = 1;
for(pii next:adj[cur]) {
if(next.f==prev) continue;
inGroup(root, next.f, cur);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N;
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]});
}
for(int i=0;i<N;i++) {
if(vis0[i]) continue;
inGroup(i, i, -1);
}
int mx = -1, mx2 = -1, mx3 = -1;
for(int i=0;i<N;i++) {
if(group[i].empty()) continue;
int mn = mnGroup(i);
if(mn >= mx) {
mx3 = mx2;
mx2 = mx;
mx = mn;
} else if(mn >= mx2) {
mx3 = mx2;
mx2 = mn;
} else if(mn > mx3) {
mx3 = mn;
}
}
int ans = mx;
if(mx3!=-1) {
ans = max(ans, mx3+mx2+L+L);
}
if(mx2!=-1) {
ans = max(ans, mx+mx2+L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'int mnGroup(int)':
dreaming.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int j=1;j<group[i].size();j++) {
| ~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
14164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
14164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
9812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
34 ms |
14164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |