| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1289474 | zaresrdic26 | 꿈 (IOI13_dreaming) | C++20 | 0 ms | 0 KiB |
#include <iostream>
#include <cstdio>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <limits>
#include <tuple>
#include <stack>
#include <bitset>
#include <cstring>
#include <sstream>
#include <functional>
#include <random>
#include "dreaming.h"
using namespace std;
vector<vector<pair<int, int>>> adj;
vector<int> par;
vector<bool> vis;
void mark_cmp(int u) {
vis[u] = true;
for (auto& e : adj[u]) {
int v = e.first;
if (!vis[v]) {
mark_cmp(v);
}
}
}
void dfs(int u, int p, int d, pair<int, int>& mx) {
if (d > mx.second) {
mx = { u, d };
}
par[u] = p;
for (auto& e : adj[u]) {
int v = e.first, w = e.second;
if (v == p) {
continue;
}
dfs(v, u, d + w, mx);
}
}
pair<int, int> find_centar(int s) {
par.assign(adj.size(), -1);
pair<int, int> mx = { -1, -1 };
dfs(s, -1, 0, mx);
int B = mx.first;
par.assign(adj.size(), -1);
mx = { -1, -1 };
dfs(B, -1, 0, mx);
int C = mx.first;
int tot = mx.second;
vector<int> path;
for (int v = C; v != -1; v = par[v]) {
path.push_back(v);
}
reverse(path.begin(), path.end());
vector<int> dist(path.size(), 0);
for (int i = 1; i < path.size(); ++i) {
int a = path[i - 1], b = path[i];
for (auto& e : adj[a]) {
if (e.first == b) {
dist[i] = dist[i - 1] + e.second;
break;
}
}
}
for (int i = 0; i < path.size(); ++i) {
if (dist[i] * 2 >= tot) {
return { path[i], tot };
}
}
return { path.back(), tot };
}
int mx_dist(int s) {
par.assign(adj.size(), -1);
pair<int, int> mx = { -1, -1 };
dfs(s, -1, 0, mx);
return mx.second;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int n, m, l;
n = N, m = M, l = L;
adj.assign(n, {});
for (int i = 0; i < m; ++i) {
int u, v, w;
u = A[i], v = B[i], w = T[i];
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
vis.assign(n, false);
par.assign(n, -1);
vector<int> c;
int pom;
for (int u = 0; u < n; ++u) {
if (!vis[u]) {
mark_cmp(u);
pair<int, int> x = find_centar(u);
c.push_back(mx_dist(x.first));
pom = max(pom, x.second);
}
}
int ans = pom;
sort(c.rbegin(), c.rend());
if (c.size() == 1) {
//ans = pom;
}
else if (c.size() == 2) {
ans = max(ans, c[0] + l + c[1]);
}
else {
ans = max({ ans, c[1] + l + c[0], c[1] + 2 * l + c[2] });
}
return ans;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
