1. 程式人生 > 實用技巧 >P1525 關押罪犯 (並查集 / 二分圖)

P1525 關押罪犯 (並查集 / 二分圖)

原題連結:https://www.luogu.com.cn/problem/P1525

題目概括:

給你m對關係,每對關係分別涉及到x,y兩人,矛盾值為w
請你判斷分配x和y到兩個集合中,能否避免衝突
如能避免請輸出0,如果衝突不可避免,請輸出最小的矛盾值

並查集解法

這道題,,讓矛盾值儘可能小,那麼我們可以遵循一個思路,就是”敵人的敵人就是我的朋友“。貪心做法,讓怒氣最大的儘可能不放在一起。於是把怒氣值從大到小排序,然後遍歷,對於兩個人A,B,把A和B的敵人放在一起,B和A的敵人放在一起,對A,B進行查詢,如果他們已經在一棵樹中,直接輸出怒氣值,結束。
因為我們進行了從大到小的排序,大的已經儘可能拆開了,所以當前方案一定是最優的。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4 + 10;
const int N = 20006, M = 100006;
int n, m, fa[N << 1];
struct P {
	int a, b, c;
	bool operator < (const P x) const {
		return c > x.c;
	}
} p[M];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }//壓縮路徑就不用多說了
int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	int n, m; cin >> n >> m;
	for (int i = 1; i <= m; i++) cin >> p[i].a >> p[i].b >> p[i].c;
	sort(p + 1, p + m + 1);
	for (int i = 1; i <= (n << 1); i++) fa[i] = i;
	for (int i = 1; i <= m; i++) {
		int x = find(p[i].a), y = find(p[i].b);
		int nx = find(p[i].a + n), ny = find(p[i].b + n);
		if (x == y) {
			cout << p[i].c << endl;
			return 0;
		}
		fa[x] = ny,fa[y] = nx;//放敵人那邊
	}
	cout << 0 << endl;
}

二分解法:

使用二分判定:

//虛擬碼
void dfs(int x,int color)
    賦值 v[x] <- color
    對於與x相連的每條無向邊(x,y)
        if v[y] == 0 then
           	dfs(y,3 - color)
        else if v[y] == color then
            判斷無向圖不是二分圖,演算法結束

主函式
     for i <- 1 to N
         if v[i] = 0 then dfs(i,1)
         判斷無向圖是否是二分圖

AC程式碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4 + 10;
const int N = 20006, M = 100006;
struct P {
	int x, y, z;
	bool operator < (const P w) const {
		return z > w.z;
	}
} p[M];
int n, m, v[N];
vector<pair<int, int> > e[N];

bool dfs(int x, int color) {
	v[x] = color;
	for (unsigned int i = 0; i < e[x].size(); ++i) {
		int y = e[x][i].first;
		if (v[y]) {
			if (v[y] == color)return false;
		}
		else {
			if (!dfs(y, 3 - color))return false;
		}
	}
	return true;
}

inline bool pd(int now) {
	for (int i = 1; i <= n; i++) e[i].clear();
	for (int i = 1; i <= m; ++i) {
		if (p[i].z <= now)break;
		e[p[i].x].push_back(make_pair(p[i].y, p[i].z));
		e[p[i].y].push_back(make_pair(p[i].x, p[i].z));
	}
	memset(v, 0, sizeof v);
	for (int i = 1; i <= n; ++i)
		if (!v[i] && !dfs(i, 1))return false;
	return true;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; ++i)
		cin >> p[i].x >> p[i].y >> p[i].z;
	sort(p + 1, p + 1 + m);
	int l = 0, r = p[1].z;
	while (l < r) {
		int mid = (l + r) >> 1;
		if (pd(mid)) r = mid;
		else l = mid + 1;
	}
	cout << l << endl;
}