This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int n, m;
vector<int> tree[200006], child[200006];
vector<pair<int, int>> adj[200006];
int _par[200006], _sz[200006];
bool cl[200006];
int dfs(int x, int prev = -1) {
_par[x] = prev;
_sz[x] = 1;
for (auto &i: tree[x]) if (i != prev) {
_sz[x] = max(_sz[x], dfs(i, x) + 1);
}
return _sz[x];
}
int dist[200006];
int dfs_dist(int x, int prev = -1) {
vector<int> v;
int ret = 0;
dist[x] = 0;
for (auto &i: tree[x]) if (i != prev) {
v.push_back(dfs_dist(i, x) + 1);
ret = max(ret, v.back());
dist[x] = max(dist[x], dist[i]);
}
int v0 = max_element(v.begin(), v.end()) - v.begin();
int X = v[v0];
if (!v.empty()) v[v0] = 0;
int v1 = max_element(v.begin(), v.end()) - v.begin();
v[v0] = X;
if (!v.empty()) dist[x] = max(dist[x], v[v0]);
if ((int)v.size() > 1) dist[x] = max(dist[x], v[v0] + v[v1]);
return ret;
}
int sz[200006], depth[200006], par[200006];
int in[200006], top[100006], T;
void dfs_child(int x, int prev = -1) {
par[x] = prev;
for (auto &i: tree[x]) if (i != prev) {
child[x].push_back(i);
dfs_child(i, x);
}
}
int dfs_sz(int x) {
sz[x] = 1;
for (auto &i: child[x]) {
depth[i] = depth[x] + 1;
sz[x] += dfs_sz(i);
if (sz[i] > sz[child[x][0]]) swap(child[x][0], i);
}
return sz[x];
}
void dfs_hld(int x) {
in[x] = T++;
for (auto &i: child[x]) {
top[i] = (i == child[x][0] ? top[x] : i);
dfs_hld(i);
}
}
int get_dist(int u, int v) {
int ret = 0;
while (top[u] != top[v]) {
if (depth[top[u]] < depth[top[v]]) swap(u, v);
ret += depth[top[u]] - depth[u] + 1;
u = par[top[u]];
}
return ret + abs(in[u] - in[v]);
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
if (w == 1) {
tree[x].push_back(y);
tree[y].push_back(x);
} else {
adj[x].push_back({ y, w });
adj[y].push_back({ x, w });
}
}
dfs_child(0);
dfs_sz(0);
dfs_hld(0);
dfs_dist(0);
int zero = 2 * (n - 1) - dist[0], one = (int)1e9;
for (int i = 0; i < n; i++) {
dfs(i);
dfs_dist(i);
for (auto &j: adj[i]) if (i < j.first) {
int curr = j.second + 2 * (n - 1) - get_dist(i, j.first) - 1;
int x = j.first, pv = -1;
vector<pair<int, int>> v;
int y = -1, T = 0;
while (x != -1) {
for (auto &k: tree[x]) if (k != pv && k != par[x]) {
if (_sz[k]) {
if (!v.empty() && v.back().second == T) {
y = max(y, v.back().first + _sz[k] - 1);
v.back().first = max(v.back().first, _sz[k]);
} else v.push_back({ _sz[k], T });
}
y = max(y, dist[k] - 1);
}
T++;
pv = x;
x = _par[x];
}
int ans = -(int)1e9;
for (int i = 0; i < (int)v.size(); i++) {
if (i) ans -= v[i].second - v[i - 1].second;
y = max(y, ans + v[i].first + 1);
ans = max(ans, v[i].first);
}
curr -= y;
one = min(one, curr);
}
}
printf("%d", min(zero, one));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d%d%d", &x, &y, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |