Kruskal-重构树 求条
查看原帖
Kruskal-重构树 求条
730195
Little_Cabbage楼主2024/6/15 11:30
#include <bits/stdc++.h>
#define ull unsigned long long
#define int long long
#define ldb long double
#define db double
#define endl '\n'
using namespace std;
namespace OI {
	template <typename T>
	T read() {
		T x = 0, f = 1;
		int ch = getchar();
		while (!isdigit(ch)) {
			if (ch == '-') f = -1;
			ch = getchar();
		}
		while (isdigit(ch)) {
			x = (x << 3) + (x << 1) + (ch ^ 48);
			ch = getchar();
		}
		return x * f;
	}
	template <typename TE>
	void write(TE x) {
		TE sta[35];
		int top = 0;
		do {
			sta[top++] = x % 10, x /= 10;
		} while (x);
		while (top) putchar(sta[--top] + '0');
	}
	string read_with_string() {
		string s = "";
		char ch = getchar();
		while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
			s += ch;
			ch = getchar();
		}
		return s;
	}
	void write_with_string(string s) {
		for (int i = 0; i < s.size(); i++) putchar(s[i]);
	}
}
using namespace OI;
const int N = 1e4 + 10;
int n, m;
struct edge {
	int x, y;
	int val;
	bool operator < (const edge &b) const {
		return val < b.val;
	}
} e[N];
vector<int> g[N];//Kruskal-重构树
int val[N * 2];//重构树的结点权值
int f[N], fa[N];
int Q;
class dsu {
	private:
		int f[N];
	public:
		void init() {
			for (int i = 1; i <= n; i++) f[i] = i;
		}
		int find(int x) {
			return (f[x] == x ? x : f[x] = find(f[x]));
		}
		bool check(int x, int y) {
			int rx = find(x), ry = find(y);
			if (rx == ry) return false;
			return true;
		}
		void add(int x, int y) {
			if (check(x, y)) {
				int rx = find(x), ry = find(y);
				f[rx] = ry;
			}
		}
};
dsu d;
class Debug {
	private:
		int YourVar;
	public:
		void debug_cgs_g() {
			for (int i = 1; i <= n; i++) {
				cout << i << " : \n";
				for (int j = 0; j < g[i].size(); j++)
					cout << g[i][j] << " ";
				cout << endl;
			}
		}
		void debug_cgs_val() {
			cout << "val : \n";
			for (int i = 1; i <= n; i++) cout << i << " ";
			cout << endl;
			for (int i = 1; i <= n; i++) cout << val[i] << " ";
			cout << endl;
		}
		void debug_fa() {
			cout << "fa : \n";
			for (int i = 1; i <= n; i++) cout << i << " ";
			cout << endl;
			for (int i = 1; i <= n; i++) cout << fa[i] << " ";
			cout << endl;
		}
};
Debug debug;
int root;
int dep[N], vis[N];
class LCA {
	private:
		int dp[N][20];
	public:
		void init() {
			for (int i = 1; i <= n; i++) dp[i][0] = fa[i];
			for (int j = 1; j <= log2(n); j++)
				for (int i = 1; i <= n; i++)
					dp[i][j] = dp[dp[i][j - 1]][j - 1];
		}
		int ask(int x, int y) {
			if (dep[x] < dep[y]) swap(x, y);
			for (int i = 0, len = dep[x] - dep[y]; i <= log2(n); i++, len >>= 1) if (len & 1) x = dp[x][i];
			if (x == y) return x;
			for (int i = log2(n); i >= 0; i--) {
				if (dp[x][i] == dp[y][i]) continue;
				x = dp[x][i];
				y = dp[y][i];
			}
			return dp[x][0];
		}
};
LCA lca;
queue<pair<int, int> > q;
void Solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) f[i] = i;
	d.init();
	for (int i = 1; i <= m; i++) {
		cin >> e[i].x >> e[i].y >> e[i].val;
		e[i].val = -e[i].val;//要求求最小值
	}
	sort(e + 1, e + m + 1);
	for (int i = 1; i <= m; i++) {
		int x = e[i].x, y = e[i].y;
		if (d.check(x, y)) {
			d.add(x, y);
			n++;
			val[n] = -e[i].val;
			g[n].push_back(f[x]), g[f[x]].push_back(n), f[x] = n;
			g[n].push_back(f[y]), g[f[y]].push_back(n), f[y] = n;
		}
	}
//	debug.debug_cgs_g();
//	debug.debug_cgs_val();
	root = n;
	q.push(make_pair(root, 1));
	while (!q.empty()) {
		pair<int, int> u = q.front();
		q.pop();
		vis[u.first] = 1;
		dep[u.first] = u.second;
		for (int i = 0; i < g[u.first].size(); i++) {
			int v = g[u.first][i];
			if (!vis[v]) {
				q.push(make_pair(v, u.second + 1));
				fa[v] = u.first;
			}
		}
	}
//	debug.debug_fa();
	lca.init();
	cin >> Q;
	while (Q--) {
		int x, y;
		cin >> x >> y;
		int t = val[lca.ask(x, y)];
		cout << (t ? t : -1) << endl;
	}
}
signed main() {
//	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
//	freopen(".in", "r", stdin);
//	freopen(".ans", "w", stdout);
	int t = 1;
//	cin >> t;
	while (t--) Solve();
	return 0;
}

#1 WA

#2 AC

#3 ~ #7 WA

#8 ~ #21 RE

2024/6/15 11:30
加载中...