diff --git a/BZOJ/1977/1977.cpp b/BZOJ/1977/1977.cpp new file mode 100644 index 00000000..a4ba4b9b --- /dev/null +++ b/BZOJ/1977/1977.cpp @@ -0,0 +1,262 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5, + M = 3e5 + 5; + +int n, m, a[N + M]; +long long res, ans = std::numeric_limits::max(); +std::tuple edges[M]; +bool vis[M]; + +class LinkCutTree { + private: + struct node { + size_t lchild, rchild, father; + int max1, max2; + bool reversed; + + node() + : lchild(0), + rchild(0), + father(0), + max1(0), + max2(0), + reversed(false) {} + + size_t& child(const unsigned& x) { + return !x ? lchild : rchild; + } + } tr[N + M]; + + void pushup(const size_t& u) { + tr[u].max1 = a[u]; + + if (tr[u].max1 < tr[tr[u].lchild].max1) { + tr[u].max2 = tr[u].max1; + tr[u].max1 = tr[tr[u].lchild].max1; + } else if (tr[u].max1 > tr[tr[u].lchild].max1) { + tr[u].max2 = std::max(tr[u].max2, tr[tr[u].lchild].max1); + } + + if (tr[u].max1 < tr[tr[u].rchild].max1) { + tr[u].max2 = tr[u].max1; + tr[u].max1 = tr[tr[u].rchild].max1; + } else if (tr[u].max1 > tr[tr[u].rchild].max1) { + tr[u].max2 = std::max(tr[u].max2, tr[tr[u].rchild].max1); + } + + tr[u].max2 = std::max({tr[u].max2, tr[tr[u].lchild].max2, tr[tr[u].rchild].max2}); + } + + void pushdown(const size_t& u) { + if (!tr[u].reversed) return; + + std::swap(tr[u].lchild, tr[u].rchild); + tr[tr[u].lchild].reversed = !tr[tr[u].lchild].reversed; + tr[tr[u].rchild].reversed = !tr[tr[u].rchild].reversed; + tr[u].reversed = false; + } + + bool relation(const size_t& u) { + return u == tr[tr[u].father].lchild ? 0 : 1; + } + + bool is_root(const size_t& u) { + return tr[tr[u].father].lchild != u && tr[tr[u].father].rchild != u; + } + + void rotate(const size_t& u) { + size_t p = tr[u].father; + bool x = relation(u); + + if (!is_root(p)) tr[tr[p].father].child(relation(p)) = u; + tr[u].father = tr[p].father; + + if (tr[u].child(x ^ 1)) tr[tr[u].child(x ^ 1)].father = p; + tr[p].child(x) = tr[u].child(x ^ 1); + + tr[u].child(x ^ 1) = p; + tr[p].father = u; + + pushup(p); + pushup(u); + } + + void splay(size_t u) { + std::stack st; + + size_t cur = u; + st.emplace(cur); + + while (!is_root(cur)) { + st.emplace(tr[cur].father); + cur = tr[cur].father; + } + + while (!st.empty()) { + pushdown(st.top()); + st.pop(); + } + + while (!is_root(u)) { + if (is_root(tr[u].father)) { + rotate(u); + } else if (relation(u) == relation(tr[u].father)) { + rotate(tr[u].father); + rotate(u); + } else { + rotate(u); + rotate(u); + } + } + } + + void access(size_t u) { + for (size_t f = 0; u; u = tr[f = u].father) { + splay(u); + tr[u].rchild = f; + pushup(u); + } + } + + void make_root(const size_t& u) { + access(u); + splay(u); + tr[u].reversed = !tr[u].reversed; + } + + size_t find_root(size_t u) { + access(u); + splay(u); + + while (tr[u].lchild) { + u = tr[u].lchild; + } + + return u; + } + + void split(const size_t& x, const size_t& y) { + make_root(x); + access(y); + splay(y); + } + + public: + bool check(const int& x, const int& y) { + return find_root(x) == find_root(y); + } + + std::pair query(const int& x, const int& y) { + split(x, y); + + return {tr[y].max1, tr[y].max2}; + } + + void link(const int& x, const int& y) { + make_root(x); + + if (find_root(y) != x) { + tr[x].father = y; + } + } + + void cut(const int& x, const int& y) { + split(x, y); + + if (tr[y].lchild == x) { + tr[y].lchild = tr[x].father = 0; + } + } +} lct; + +struct DSU : private std::vector { + DSU(const int& _n) + : std::vector(_n + 1) { + std::iota(begin(), end(), 0); + } + + int find(const int& x) { + return x == at(x) ? x : at(x) = find(at(x)); + } + + bool check(const int& x, const int& y) { + return find(x) == find(y); + } + + void merge(int x, int y) { + x = find(x); + y = find(y); + + if (x != y) at(x) = y; + } +}; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= m; ++i) { + int u, v, w; + + cin >> u >> v >> w; + + edges[i] = {u, v, w}; + } + + std::sort(edges + 1, edges + m + 1, [](const std::tuple& a, const std::tuple& b) { + return std::get<2>(a) < std::get<2>(b); + }); + + DSU dsu(n); + + for (int i = 1; i <= m; i++) { + int u = std::get<0>(edges[i]), + v = std::get<1>(edges[i]), + x = dsu.find(u), + y = dsu.find(v), + w = std::get<2>(edges[i]); + + a[n + i] = w; + + if (x != y) { + dsu.merge(x, y); + lct.link(u, i + n); + lct.link(i + n, v); + res += w; + vis[i] = true; + } + } + + for (int i = 1; i <= m; i++) { + if (vis[i]) continue; + + int u, v, w; + + std::tie(u, v, w) = edges[i]; + auto max = lct.query(u, v); + + if (w > max.first) { + ans = std::min(ans, static_cast(w) - max.first); + } else { + ans = std::min(ans, static_cast(w) - max.second); + } + } + + cout << res + ans << endl; + + return 0; +} diff --git a/BZOJ/1977/data/1.in b/BZOJ/1977/data/1.in new file mode 100644 index 00000000..978694c3 --- /dev/null +++ b/BZOJ/1977/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7b4cf8a5a6f7231a6058ff0feb6dbbefb62a4316e4ae93b6793109bcc4316b73 +size 38049 diff --git a/BZOJ/1977/data/1.out b/BZOJ/1977/data/1.out new file mode 100644 index 00000000..627d8396 --- /dev/null +++ b/BZOJ/1977/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:159d321cd62d70c52cf9c3bce7eeefc7f36d6a2eaa88b26fda88411b992895b3 +size 8 diff --git a/BZOJ/1977/data/10.in b/BZOJ/1977/data/10.in new file mode 100644 index 00000000..c473240a --- /dev/null +++ b/BZOJ/1977/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:07262cad05424a198f715763e29c0408c7c0a98e601128d4b36a6cb22185f4be +size 25257 diff --git a/BZOJ/1977/data/10.out b/BZOJ/1977/data/10.out new file mode 100644 index 00000000..b45dcbd3 --- /dev/null +++ b/BZOJ/1977/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:119b299babb510ceeb0f9ae3e47e7b464ec848e79c4cc2f4694a06c807ed46b9 +size 8 diff --git a/BZOJ/1977/data/2.in b/BZOJ/1977/data/2.in new file mode 100644 index 00000000..6c70811e --- /dev/null +++ b/BZOJ/1977/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ba3bcac55f1ffc45d6bc83c62ec303906bf4888584fcf2618f917a3fc8c0cf4 +size 1698502 diff --git a/BZOJ/1977/data/2.out b/BZOJ/1977/data/2.out new file mode 100644 index 00000000..89da97f7 --- /dev/null +++ b/BZOJ/1977/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c70ad6cd94acad8e4d41a1accc4dd76107835f7642e08e0af4b0b90db0eafb9d +size 10 diff --git a/BZOJ/1977/data/3.in b/BZOJ/1977/data/3.in new file mode 100644 index 00000000..bd7c2d61 --- /dev/null +++ b/BZOJ/1977/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:caa37677465e0268c7dd69244064f41a50ae312bf6bf0cb6d7c7911d8c658404 +size 5459971 diff --git a/BZOJ/1977/data/3.out b/BZOJ/1977/data/3.out new file mode 100644 index 00000000..9c27c2fe --- /dev/null +++ b/BZOJ/1977/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e28b834f422666221c0c4c4d59201b5d9b55dc3d7f7783414385c3344e322ea +size 10 diff --git a/BZOJ/1977/data/4.in b/BZOJ/1977/data/4.in new file mode 100644 index 00000000..4e9cc573 --- /dev/null +++ b/BZOJ/1977/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c5f1e4b1b9d13510f6c614cedbf487532027e74f0523387bdc2ad0435eb640c4 +size 1829879 diff --git a/BZOJ/1977/data/4.out b/BZOJ/1977/data/4.out new file mode 100644 index 00000000..2dde7694 --- /dev/null +++ b/BZOJ/1977/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b39e663f40e2d70c124468ee3e12c3dea8066503f858d2d2fd886a29f64bd035 +size 11 diff --git a/BZOJ/1977/data/5.in b/BZOJ/1977/data/5.in new file mode 100644 index 00000000..b52a4464 --- /dev/null +++ b/BZOJ/1977/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b514ddb46b67bdf7f926e2d4022af1730943d32fa9f3648f52b3886a8af218e +size 12480 diff --git a/BZOJ/1977/data/5.out b/BZOJ/1977/data/5.out new file mode 100644 index 00000000..ebae4c95 --- /dev/null +++ b/BZOJ/1977/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1595a5b67fe15f795b5e694be8dc2788dadbb22cecdb1912d1ad4e1addbc2988 +size 8 diff --git a/BZOJ/1977/data/6.in b/BZOJ/1977/data/6.in new file mode 100644 index 00000000..5e651bdf --- /dev/null +++ b/BZOJ/1977/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6c8e141c9ad90cdeb9acb122bc0891793d45254108fa10d128a707e8fe794dd8 +size 110 diff --git a/BZOJ/1977/data/6.out b/BZOJ/1977/data/6.out new file mode 100644 index 00000000..24c82eb8 --- /dev/null +++ b/BZOJ/1977/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3b2b717b495cea40b7e1adeddc02cb9a68be0e674131fd80bd1f8b159c9dbaff +size 4 diff --git a/BZOJ/1977/data/7.in b/BZOJ/1977/data/7.in new file mode 100644 index 00000000..3368e406 --- /dev/null +++ b/BZOJ/1977/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e52148a47d12b9f7b53a75760f2d99dd55d7e712b6e16dac20aef478c6b8cb36 +size 42143 diff --git a/BZOJ/1977/data/7.out b/BZOJ/1977/data/7.out new file mode 100644 index 00000000..e813dec7 --- /dev/null +++ b/BZOJ/1977/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a71b6900b7810f86e955d379b497e3b74bc010e26cb158ee42a66d20553e9bd8 +size 8 diff --git a/BZOJ/1977/data/8.in b/BZOJ/1977/data/8.in new file mode 100644 index 00000000..7d1bb347 --- /dev/null +++ b/BZOJ/1977/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c1a965bec0f013bdee962c7a7d7fb3686d1e383e4f1b18e4ee16d4a505ee4900 +size 5608615 diff --git a/BZOJ/1977/data/8.out b/BZOJ/1977/data/8.out new file mode 100644 index 00000000..96d9781f --- /dev/null +++ b/BZOJ/1977/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b5005b383d7f9e6554024c80c4f0a8a68525f34b09f9ed48cf796dae0eca3aa7 +size 12 diff --git a/BZOJ/1977/data/9.in b/BZOJ/1977/data/9.in new file mode 100644 index 00000000..2d9f0544 --- /dev/null +++ b/BZOJ/1977/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2053770a79c9d8b08f0c02d58f92779e0473106e4a500b51428b03df7f1ad170 +size 1698624 diff --git a/BZOJ/1977/data/9.out b/BZOJ/1977/data/9.out new file mode 100644 index 00000000..af7370e2 --- /dev/null +++ b/BZOJ/1977/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:35612b155bdaa13548ef34b5f91a43b4d45a839319c23d4f8f4541a5fc31e245 +size 10