【BZOJ 4883】[Lydsy2017年5月月赛] 棋盘上的守卫

相关链接

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4883
官方题解:http://oi.cyo.ng/wp-content/uploads/2017/05/ludsy_may_contest_solution.pdf

解题报告

把行和列看成点,格子看成边
那么一个$n$个点的连通块需要$n$条边
于是用$Kruskal$做一个基环树森林就可以了
时间复杂度:$O(nm \log nm)$

Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 300000;

struct Edge{
	int u, v, c;
	bool operator < (const Edge &ee) const {
		return c < ee.c;
	}
}e[N];
int n, m, tot, cir[N], fa[N];

inline int read() {
	char c=getchar(); int f=1,ret=0;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret * f;
}

int find(int x) {
	return x == fa[x]? x: fa[x] = find(fa[x]);
}

int main() {
#ifdef DBG
	freopen("11input.in", "r", stdin);
#endif
	n = read(); m = read();
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			e[++tot].c = read();
			e[tot].u = i;
			e[tot].v = n + j;
		}
	}
	sort(e + 1, e + 1 + tot);
	for (int i = 1; i <= n + m; i++) {
		fa[i] = i;
	}
	LL ans = 0;
	for (int i = 1; i <= tot; i++) {
		int u = e[i].u, v = e[i].v;
		int fu = find(u), fv = find(v);
		if (cir[fu] && cir[fv]) {
			continue;
		} else if (fu != fv) {
			fa[fv] = fu;
			cir[fu] |= cir[fv];
		} else if (!cir[fu]) {
			cir[fu] = 1;
		}
		ans += e[i].c;
	}
	printf("%lld\n", ans);
	return 0;
}

16 thoughts to “【BZOJ 4883】[Lydsy2017年5月月赛] 棋盘上的守卫”

  1. 407949 570854Howdy! Would you mind if I share your blog with my twitter group? Theres a lot of individuals that I feel would genuinely enjoy your content. Please let me know. Thanks 677831

  2. 685590 794652Oh my goodness! an wonderful write-up dude. A lot of thanks Even so My business is experiencing trouble with ur rss . Do not know why Struggle to sign up to it. Can there be everybody finding identical rss dilemma? Anyone who knows kindly respond. Thnkx 457872

  3. 179564 151360Basically a smiling visitor here to share the love (:, btw outstanding style . “Audacity, a lot more audacity and always audacity.” by Georges Jacques Danton. 547651

  4. 877245 993355An fascinating discussion may be worth comment. I believe you need to write on this topic, it may well surely be a taboo subject but normally men and women are not enough to dicuss on such topics. To a higher. Cheers 434632

  5. 123977 381250Empathetic for your monstrous inspect, in addition Im just seriously excellent as an alternative to Zune, and consequently optimism them, together with the extremely very good critical reviews some other players have documented, will let you determine whether it does not take right choice for you. 601779

  6. 902261 105723Thanks for the info provided! I was obtaining for this information for a long time, but I wasnt able to uncover a reliable source. 813695

  7. Very efficiently written information. It will be beneficial to anyone who employess it, including yours truly :). Keep up the good work – i will definitely read more posts.

Leave a Reply

Your email address will not be published. Required fields are marked *