Homework Introduction

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, fa[N];

int find(int x) { //寻找x所在集合的祖先
	if (x == fa[x])
		return x;
	return fa[x] = find(fa[x]);
}

void merge(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy) {
		fa[fx] = fy;
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		fa[i] = i;
	}
	for (int i = 1; i <= m; i++) {
		int pos, x, y;
		cin >> pos >> x >> y;
		if (pos == 1) {
			merge(x, y);
		} else {
			int fx = find(x), fy = find(y);
			if (fx == fy) {
				cout << "Y" << endl;
			} else
				cout << "N" << endl;
		}
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int c, T, n, m, fa[N << 1], val[N], ok[N], p[N], q[N], siz[N * 2];

int find(int x) {
	if (fa[x] == x)
		return x;
	else
		return fa[x] = find(fa[x]);
}

void merge(int x, int y) {
	int fx = find(x);
	int fy = find(y);
	if (fx != fy) {
		fa[fx] = fy;
		siz[fy] += siz[fx];
	}
}

int main() {
	cin >> c >> T;
	while (T--) {
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			p[i] = i;
			ok[i] = 0;
			val[i] = 0;
			q[i] = 1;

		}
		for (int i = 1; i <= 2 * n; i++) {
			fa[i] = i;
			if (i <= n)
				siz[i] = 1;
			else
				siz[i] = 0;
		}
		for (int i = 1; i <= m; i++) {
			char opt;
			int x, y;
			cin >> opt >> x;
			if (opt == 'T') {
				val[x] = 1;
				ok[x] = 1;
			} else if (opt == 'F') {
				val[x] = -1;
				ok[x] = 1;
			} else if (opt == 'U') {
				val[x] = 0;
				ok[x] = 1;
			} else if (opt == '+') {
				cin >> y;
				if (ok[y]) {
					val[x] = val[y];
					ok[x] = 1;
				} else {
					ok[x] = 0;
					p[x] = p[y];
					q[x] = q[y];
				}
			} else if (opt == '-') {
				cin >> y;
				if (ok[y]) {
					val[x] = -val[y];
					ok[x] = 1;
				} else {
					ok[x] = 0;
					p[x] = p[y];
					q[x] = -q[y];
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			if (ok[i]) {
				continue;
			}
			int x = p[i];
			if (q[x] == 1) {
				merge(i, x);
				merge(i + n, x + n);
			} else {
				merge(i, x + n);
				merge(i + n, x);
			}
		}
		int res = 0;
		for (int i = 1; i <= n; i++) {
			if (find(i) == find(i + n)) {
				res += siz[find(i)];
				siz[find(i)] = 0;
			} else if (ok[i] == 1 && val[i] == 0) {
				res += siz[find(i)];
				siz[find(i)] = 0;
				res += siz[find(i + n)];
				siz[find(i + n)] = 0;
			}
		}
		cout << res << endl;
	}
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5, M = 5e4 + 5;

struct edge {
	int u, v, w;
} p[M];

struct node {
	int v, w;
};
vector<node>e[N];
int n, m, fa[N], dep[N], f[N][25], g[N][25], q;

void dfs(int x, int father, int deep, int w) {
	dep[x] = deep;
	f[x][0] = father;
	g[x][0] = w;
	for (int i = 1; i <= 21; i++) {
		f[x][i] = f[f[x][i - 1]][i - 1];
		g[x][i] = min(g[f[x][i - 1]][i - 1], g[x][i - 1]);
	}
	for (int i = 0; i < e[x].size(); i++) {
		int v = e[x][i].v;
		int w = e[x][i].w;
		if (v == father)
			continue;
		dfs(v, x, deep + 1, w);
	}
}

int lca(int x, int y) {
	if (dep[x] > dep[y])
		swap(x, y);
	int res = 1e9;
	for (int j = 21; j >= 0; j--) {
		if (dep[f[y][j]] >= dep[x]) {
			res = min(res, g[y][j]);
			y = f[y][j];
		}
	}
//	cout << x << " " << y << " " << res << endl;
	if (x == y)
		return res;
	for (int j = 21; j >= 0; j--) {
		if (f[y][j] != f[x][j]) {
			res = min(res, min(g[y][j], g[x][j]));
			y = f[y][j];
			x = f[x][j];
		}
	}
	return min(res, min(g[x][0], g[y][0]));
}

bool cmp(edge a, edge b) {
	return a.w > b.w;
}

int find(int x) {
	if (x == fa[x])
		return x;
	else
		return fa[x] = find(fa[x]);
}

void merge(int x, int y) {
	int fx = find(x), fy = find(y);
	if (fx != fy)
		fa[fx] = fy;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		fa[i] = i;
	for (int i = 1; i <= m; i++) {
		cin >> p[i].u >> p[i].v >> p[i].w;
	}
	sort(p + 1, p + m + 1, cmp);
	for (int i = 1; i <= m; i++) {
		int u = p[i].u, v = p[i].v, w = p[i].w;
		if (find(u) != find(v)) {
			e[u].push_back({v, w});
			e[v].push_back({u, w});
			merge(u, v);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (dep[i] == 0) {
			dfs(i, 0, 1, 0);
		}
	}
	cin >> q;
	while (q--) {
		int x, y;
		cin >> x >> y;
		if (find(x) != find(y))
			cout << -1 << endl;
		else {
			cout << lca(x, y) << endl;
		}
	}
	return 0;
}

Problem

Please claim the assignment to see the problems.
Status
Live...
Problem
26
Open Since
2025-8-10 13:30
Deadline
2025-8-31 23:59
Extension
24 hour(s)