树链剖分0pts求调
查看原帖
树链剖分0pts求调
674147
vanueber楼主2024/10/10 11:50
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <ctime>
#include <random>

using namespace std;

const int INF = 0x3f3f3f3f;

inline int read()
{
	int w = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		w = (w << 1) + (w << 3) + (ch ^ 48);
		ch = getchar();
	}
	return w * f;
}

inline void write(int x)
{
	if (x < 0)
	{
		putchar('-');
		x = -x;
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}

const int maxn = 1e6 + 5, maxm = 3e6 + 5;
int n, m;
struct node
{
	int v, w;
};
vector<node> G[maxn];
struct edge
{
	int u, v, w;
} e[maxm];
bool vis[maxm];
bool operator <(const edge &a, const edge &b)
{
	return a.w < b.w;
}

int fa[maxn];
void init(int n)
{
	for (int i = 1; i <= n; ++i) fa[i] = i;
}
int find(int x)
{
	if (fa[x] == x) return fa[x];
	return fa[x] = find(fa[x]);
}
void join(int s1, int s2)
{
	int pa = find(s1), pb = find(s2);
	if (pa != pb) fa[pb] = pa;
}

int cnt = 0, ans = 0;
void kruskal()
{
	init(n);
	sort(e + 1, e + m + 1);
	for (int i = 1; i <= m; ++i)
	{
		int u = e[i].u, v = e[i].v, w = e[i].w;
		if (find(u) == find(v)) continue;
		else
		{
			vis[i] = 1;
			join(u, v);
			G[u].push_back({v, w});
			G[v].push_back({u, w});
			++cnt;
			ans += w;
			if (cnt == n - 1) break;
		}
	}
}

int dep[maxn], f[maxn], hson[maxn], sz[maxn], w[maxn];
void dfs1(int u, int fa, int depth)
{
	sz[u] = 1;
	dep[u] = depth;
	int hson_sz = -1;
	for (node k : G[u])
	{
		int v = k.v;
		if (v == fa) continue;
		f[v] = u;
		dfs1(v, u, depth + 1);
		sz[u] += sz[v];
		w[v] = k.w;
		if (hson_sz < sz[v])
		{
			hson_sz = sz[v];
			hson[u] = v;
		}
	}
}
int top[maxn], dfn[maxn], num, d[maxn];
void dfs2(int u, int fa)
{
	top[u] = fa, dfn[u] = ++num, d[num] = w[u];
	if (hson[u] == 0) return;
	dfs2(hson[u], fa);
	for (node k : G[u])
	{
		int v = k.v;
		if (v == f[u] || v == hson[u]) continue;
		dfs2(v, v);
	}
}

struct seg
{
	int l, r;
	int maxx, smax;
} tr[maxn << 2];
void print(seg x)
{
	printf("$%d %d %d %d\n", x.l, x.r, x.maxx, x.smax);
}
int ls(int p)
{
	return p << 1;
}
int rs(int p)
{
	return p << 1 | 1;
}
void pushup(seg &p, seg l, seg r)
{
	p.maxx = max(l.maxx, r.maxx);
	p.smax = max(p.maxx == l.maxx ? l.smax : l.maxx, p.maxx == r.maxx ? r.smax : r.maxx);
}
void pushup(int p)
{
	pushup(tr[p], tr[ls(p)], tr[rs(p)]);
}
void build(int p, int l, int r)
{
	tr[p].l = l, tr[p].r = r;
	tr[p].maxx = 0, tr[p].smax = -1;
	if (l == r)
	{
		tr[p].maxx = d[l];
		tr[p].smax = -1;
		return;
	}
	int mid = (l + r) >> 1;
	build(ls(p), l, mid);
	build(rs(p), mid + 1, r);
	pushup(p);
}
seg query(int p, int l, int r)
{
	if (l > tr[p].l || tr[p].r > r) return seg{0, 0, 0, -1};
	if (l <= tr[p].l && tr[p].r <= r)
	{
		return tr[p];
	}
	int mid = (tr[p].l + tr[p].r) >> 1;
	if (r <= mid)
	{
		return query(ls(p), l, r);
	}
	if (mid < l)
	{
		return query(rs(p), l, r);
	}
	seg res = {0, 0, 0, -1};
	pushup(res, query(ls(p), l, r), query(rs(p), l, r));
	return res;
}
pair<int, int> query_path(int u, int v)
{
	seg res = {0, 0, 0, -1};
	while (top[u] != top[v])
	{
		if (dep[top[u]] < dep[top[v]])
		{
			swap(u, v);
		}
		pushup(res, res, query(1, dfn[top[u]], dfn[u]));
		u = f[top[u]];

	}
	if (dep[u] < dep[v]) swap(u, v);
	seg s = query(1, dfn[v] + 1, dfn[u]);
	pushup(res, res, s);
	return make_pair(res.maxx, res.smax);
}

int main()
{
#ifndef ONLINE_JUDGE
#define LOCAL
//	freopen("in.txt", "r", stdin);
#endif
	n = read(), m = read();
	for (int i = 1, u, v, w; i <= m; ++i)
	{
		u = read(), v = read(), w = read();
		e[i] = (edge)
		{
			u, v, w
		};
	}
	kruskal();
	dfs1(1, 0, 1);
	dfs2(1, 1);
	build(1, 1, n);
	int tot = 1e9;
//	cout<<ans<<endl;
	for (int i = 1; i <= m; ++i)
	{
		if (!vis[i])
		{
			int u = e[i].u, v = e[i].v;
			pair<int, int> ed = query_path(u, v);
			int w = e[i].w;
			if (w != ed.first)
			{
				tot = min(tot, ans - ed.first + w);
			}
			else if (ed.second >= 0)
			{
				tot = min(tot, ans - ed.second + w);
			}
		}
	}
	cout << tot << endl;
#ifdef LOCAL
	fprintf(stderr, "%f\n", 1.0 * clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}
/*
4 6
1 2 1
1 3 3
1 4 10
1 2 5
2 2 2
1 1 4

*/
2024/10/10 11:50
加载中...