#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> ii;
vector<int> sz, dep;
vector<vector<ii>> adj;
void dfs(int cur, int par) {
sz[cur] = 1;
for (auto nx : adj[cur]) {
if (nx.fi == par) continue;
dfs(nx.fi,cur);
sz[cur] += sz[nx.fi];
}
}
int maxDist(int cur, int par) {
int ret = dep[cur];
for (auto nx : adj[cur]) {
if (nx.fi == par) continue;
dep[nx.fi] = dep[cur]+nx.se;
ret = max(ret,maxDist(nx.fi,cur));
}
return ret;
}
int centroid(int cur, int par, int sub) {
for (auto nx : adj[cur]) {
if (nx.fi == par) continue;
if (sz[nx.fi]*2 >= sub) return centroid(nx.fi,cur,sub);
}
return cur;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
adj = vector<vector<ii>>(N);
for (int i = 0; i < M; i++) {
adj[A[i]].push_back(ii(B[i],T[i]));
adj[B[i]].push_back(ii(A[i],T[i]));
}
sz = dep = vector<int>(N,0);
vector<int> tmp;
for (int i = 0; i < N; i++) {
if (!sz[i]) {
dfs(i,-1);
int cent = centroid(i,-1,sz[i]);
int maxi = maxDist(cent,-1);
tmp.push_back(maxi);
}
}
sort(tmp.begin(),tmp.end());
N = tmp.size();
vector<int> v(N);
for (int i = 0, l = (N-1)/2, r = (N-1)/2+1; i < N; i++) {
if (i&1) v[r++] = tmp.back();
else v[l--] = tmp.back();
tmp.pop_back();
}
int ans = 0;
priority_queue<int> pq;
for (int i = 0; i < N; i++) {
if (!pq.empty()) ans = max(ans,v[i]+i*L+pq.top());
pq.push(v[i]-i*L);
}
return ans;
}
/*
12 8 2
0 8 4
2 8 2
2 7 4
6 10 3
1 3 1
1 9 5
1 5 7
5 11 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
11460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
11460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
6804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
11460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |