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 <set>
#include <cstdio>
#include <vector>
#include <utility>
#include <iterator>
#include <algorithm>
using namespace std;
struct Item {
int posde = (int)-1e9;
int negde = (int)-1e9;
int ans = (int)-1e9;
} tree_split[1048576];
Item f(const Item a, const Item b) {
Item ret = { max(a.posde, b.posde), max(a.negde, b.negde), max(a.ans, b.ans) };
ret.ans = max(ret.ans, a.posde + b.negde);
return ret;
}
Item query_split(int i, int b, int e, int l, int r) {
if (r < l || r < b || e < l) return Item{ (int)-1e9, (int)-1e9, (int)-1e9 };
if (l <= b && e <= r) return tree_split[i];
int m = (b + e) / 2;
return f(query_split(i * 2 + 1, b, m, l, r), query_split(i * 2 + 2, m + 1, e, l, r));
}
int n, m;
vector<int> tree[500006], child[500006];
vector<pair<int, int>> adj[500006];
int sz[500006], depth[500006], par[500006];
int in[500006], top[500006], T;
int sp[19][500006], sp_dist[19][500006], sp_half[19][500006];
multiset<int> lt_depth[500006], lt_dist[500006];
int max_depth[500006], rev_max_depth[500006], dist[500006], rev_dist[500006];
int f_dist[500006], edge[500006], half[500006];
int query_sp_half(int Ap, int x) {
int ret = (lt_depth[x].empty() ? 0 : *lt_depth[x].rbegin()) - depth[x];
if (Ap == -1) return ret;
for (int t = 18; t >= 0; t--) if (sp[t][x] != -1 && depth[sp[t][x]] >= depth[Ap]) {
ret = max(ret, sp_half[t][x]);
x = sp[t][x];
}
return max(ret, sp_half[0][x]);
}
int query_hld_res(int u, int v) {
vector<Item> z;
int pv = -1;
while (top[u] != top[v]) {
if (depth[top[u]] < depth[top[v]]) swap(u, v);
if (pv != -1) lt_depth[u].erase(lt_depth[u].find(max_depth[pv] + 1));
int V = (lt_depth[u].empty() ? 0 : *lt_depth[u].rbegin());
z.push_back(Item{ V + depth[u], V - depth[u], (int)-1e9 });
if (pv != -1) lt_depth[u].insert(max_depth[pv] + 1);
z.push_back(query_split(0, 0, 524287, in[top[u]], in[u] - 1));
if (z.back().posde == (int)-1e9) z.pop_back();
pv = top[u];
u = par[top[u]];
}
if (depth[u] < depth[v]) swap(u, v);
if (pv != -1) lt_depth[u].erase(lt_depth[u].find(max_depth[pv] + 1));
int V = (lt_depth[u].empty() ? 0 : *lt_depth[u].rbegin());
z.push_back(Item{ V + depth[u], V - depth[u], (int)-1e9 });
if (pv != -1) lt_depth[u].insert(max_depth[pv] + 1);
z.push_back(query_split(0, 0, 524287, in[v], in[u] - 1));
if (z.back().posde == (int)-1e9) z.pop_back();
reverse(z.begin(), z.end());
int ret = (int)-1e9;
for (auto &i: z) ret = max(ret, i.ans);
for (int i = 0; i < (int)z.size(); i++) for (int j = i + 1; j < (int)z.size(); j++) ret = max(ret, z[i].posde + z[j].negde);
return ret;
}
int dfs_dist(int x) {
vector<int> v;
max_depth[x] = 0;
dist[x] = 0;
for (auto &i: child[x]) {
v.push_back(dfs_dist(i) + 1);
max_depth[x] = max(max_depth[x], 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 max_depth[x];
}
void dfs_rev_dist(int x) {
multiset<int> v, u, w;
v.insert(rev_max_depth[x]);
u.insert(rev_dist[x]);
for (auto &i: child[x]) {
v.insert(max_depth[i] + 1);
u.insert(dist[i]);
}
for (auto &i: child[x]) {
v.erase(v.find(max_depth[i] + 1));
u.erase(u.find(dist[i]));
if (!u.empty()) rev_dist[i] = *u.rbegin();
if (!v.empty()) rev_dist[i] = max(rev_dist[i], rev_max_depth[i] = *v.rbegin() + 1);
if ((int)v.size() > 1) rev_dist[i] = max(rev_dist[i], *v.rbegin() + *prev(prev(v.end())));
v.insert(max_depth[i] + 1);
u.insert(dist[i]);
}
for (auto &i: child[x]) dfs_rev_dist(i);
}
void dfs_f_dist(int x) {
for (auto &i: child[x]) {
lt_dist[x].erase(lt_dist[x].find(dist[i]));
f_dist[i] = (lt_dist[x].empty() ? 0 : *lt_dist[x].rbegin());
lt_dist[x].insert(dist[i]);
}
for (auto &i: child[x]) dfs_f_dist(i);
}
void dfs_edge(int x) {
multiset<int> v;
for (auto &i: child[x]) v.insert(max_depth[i] + 1);
for (auto &i: child[x]) {
v.erase(v.find(max_depth[i] + 1));
if ((int)v.size() > 1) edge[i] = *v.rbegin() + *prev(prev(v.end()));
else if (!v.empty()) edge[i] = *v.rbegin();
int V = (v.empty() ? 0 : *v.rbegin());
half[i] = V - depth[x];
v.insert(max_depth[i] + 1);
}
for (auto &i: child[x]) dfs_edge(i);
}
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 lca(int u, int v) {
int ret = 0;
while (top[u] != top[v]) {
if (depth[top[u]] < depth[top[v]]) swap(u, v);
ret += depth[u] - depth[par[top[u]]];
u = par[top[u]];
}
if (depth[u] > depth[v]) swap(u, v);
return u;
}
int pr(int x, int y) {
for (int t = 18; t >= 0; t--) if (y >= 1 << t) {
y -= 1 << t;
x = sp[t][x];
}
return x;
}
int query_sp_dist(int u, int v, int l, int A, int B) {
int ret = 0;
if (u != l) {
if (!lt_dist[u].empty()) ret = max(ret, *lt_dist[u].rbegin());
if ((int)lt_depth[u].size() > 1) ret = max(ret, *lt_depth[u].rbegin() + *prev(prev(lt_depth[u].end())));
u = sp[0][u];
}
if (v != l) {
if (!lt_dist[v].empty()) ret = max(ret, *lt_dist[v].rbegin());
if ((int)lt_depth[v].size() > 1) ret = max(ret, *lt_depth[v].rbegin() + *prev(prev(lt_depth[v].end())));
v = sp[0][v];
}
for (int t = 18; t >= 0; t--) if (sp[t][u] != -1 && depth[sp[t][u]] > depth[l]) {
ret = max(ret, sp_dist[t][u]);
u = sp[t][u];
}
for (int t = 18; t >= 0; t--) if (sp[t][v] != -1 && depth[sp[t][v]] > depth[l]) {
ret = max(ret, sp_dist[t][v]);
v = sp[t][v];
}
if (A != -1) lt_dist[l].erase(lt_dist[l].find(dist[A]));
if (B != -1) lt_dist[l].erase(lt_dist[l].find(dist[B]));
ret = max(ret, lt_dist[l].empty() ? 0 : *lt_dist[l].rbegin());
if (A != -1) lt_dist[l].insert(dist[A]);
if (B != -1) lt_dist[l].insert(dist[B]);
if (A != -1) lt_depth[l].erase(lt_depth[l].find(max_depth[A] + 1));
if (B != -1) lt_depth[l].erase(lt_depth[l].find(max_depth[B] + 1));
if (!lt_depth[l].empty()) ret = max(ret, *lt_depth[l].rbegin() + rev_max_depth[l]);
else ret = max(ret, rev_max_depth[l]);
if ((int)lt_depth[l].size() > 1) ret = max(ret, *lt_depth[l].rbegin() + *prev(prev(lt_depth[l].end())));
if (A != -1) lt_depth[l].insert(max_depth[A] + 1);
if (B != -1) lt_depth[l].insert(max_depth[B] + 1);
return ret;
}
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);
dfs_rev_dist(0);
for (int i = 0; i < n; i++) for (auto &j: child[i]) {
lt_depth[i].insert(max_depth[j] + 1);
lt_dist[i].insert(dist[j]);
}
dfs_f_dist(0);
dfs_edge(0);
for (int i = 0; i < n; i++) {
sp[0][i] = par[i];
sp_dist[0][i] = max(f_dist[i], edge[i]);
sp_half[0][i] = half[i];
}
for (int t = 1; t < 19; t++) for (int i = 0; i < n; i++) {
if (sp[t - 1][i] == -1) {
sp[t][i] = -1;
sp_dist[t][i] = sp_dist[t - 1][i];
sp_half[t][i] = sp_half[t - 1][i];
} else {
sp[t][i] = sp[t - 1][sp[t - 1][i]];
sp_dist[t][i] = max(sp_dist[t - 1][i], sp_dist[t - 1][sp[t - 1][i]]);
sp_half[t][i] = max(sp_half[t - 1][i], sp_half[t - 1][sp[t - 1][i]]);
}
}
for (int i = 0; i < n; i++) {
int v = 0;
if (!child[i].empty()) lt_depth[i].erase(lt_depth[i].find(max_depth[child[i][0]] + 1));
if (!lt_depth[i].empty()) v = *lt_depth[i].rbegin();
if (!child[i].empty()) lt_depth[i].insert(max_depth[child[i][0]] + 1);
tree_split[524287 + in[i]] = { v + depth[i], v - depth[i], (int)-1e9 };
}
for (int i = 524286; i >= 0; i--) {
tree_split[i] = f(tree_split[i * 2 + 1], tree_split[i * 2 + 2]);
}
int zero = 2 * (n - 1) - dist[0], one = (int)1e9;
for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) {
int l = lca(i, j.first);
int ds = depth[i] + depth[j.first] - depth[l] - depth[l];
int curr = j.second + 2 * (n - 1) - ds - 1;
int A = (i == l ? -1 : pr(i, depth[i] - depth[l] - 1));
int B = (j.first == l ? -1 : pr(j.first, depth[j.first] - depth[l] - 1));
int y = max(rev_dist[l] - 1, query_sp_dist(i, j.first, l, A, B) - 1);
int Ap = (depth[i] - depth[l] - 2 >= 0 ? pr(i, depth[i] - depth[l] - 2) : -1);
int Bp = (depth[j.first] - depth[l] - 2 >= 0 ? pr(j.first, depth[j.first] - depth[l] - 2) : -1);
if (A != -1) lt_depth[l].erase(lt_depth[l].find(max_depth[A] + 1));
if (B != -1) lt_depth[l].erase(lt_depth[l].find(max_depth[B] + 1));
int G = 0;
if (!lt_depth[l].empty()) G = *lt_depth[l].rbegin();
else y = max(y, rev_max_depth[l] - 1);
if ((int)lt_depth[l].size() > 1) y = max(y, *lt_depth[l].rbegin() + *prev(prev(lt_depth[l].end())) - 1);
if (A != -1) lt_depth[l].insert(max_depth[A] + 1);
if (B != -1) lt_depth[l].insert(max_depth[B] + 1);
int Alt = (A == -1 ? (int)-1e9 : query_sp_half(Ap, i) + depth[l]);
int Brt = (B == -1 ? (int)-1e9 : query_sp_half(Bp, j.first) + depth[l]);
int Lt = max(rev_max_depth[l], G);
y = max(y, Alt + max(Brt, Lt) + 1);
y = max(y, max(Alt, Lt) + Brt + 1);
if (A != -1) y = max(y, query_hld_res(A, i) + 1);
if (B != -1) y = max(y, query_hld_res(B, j.first) + 1);
curr -= y;
one = min(one, curr);
}
printf("%d", min(zero, one));
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:220:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
220 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:223:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
223 | 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... |