首页 > TAG信息列表 > P1536

P1536 村村通 并查集

P1536 村村通本题要求统计连通分块的个数,同一个连通分块可以用并查集进行处理 /* 村村通 */ #include<iostream> using namespace std; const int Max=1010; int pre[Max]; int n,m,cnt; void make() { for (int i=1;i<=n;i++) { pre[i]=i; } } int find(int x) { int r=x

P1536 村村通 题解

题目传送门 C++代码 #include <bits/stdc++.h> using namespace std; int n;//城镇数目 int m;//道路数目 int x, y; //城镇的编号 const int N = 1010; int fa[N]; //并查集数组 //要深入理解这个递归并压缩的过程 int find(int x) { if (fa[x] != x) fa[x] = find(fa[

[并查集] lg-P1536. 村村通(并查集+模板题)

文章目录 1. 题目来源2. 题目解析 1. 题目来源 链接:P1536 村村通 2. 题目解析 并查集模板题,维护连通块,连通块个数减一即为需要建设的道路个数。 代码: // https://www.luogu.com.cn/problem/P1536 #include <iostream> #include <cstdio> #include <cstring> #include <

Luogu P1536 村村通

Luogu P1536 村村通 很简单的并查集。 记住\(道路数=区块数-1\)。 #include<bits/stdc++.h> #define N 1010 using namespace std; int n,m,ans; int fa[N]; int Find(int x) { return fa[x]==x?x:fa[x]=Find(fa[x]); } void Merge(int x,int y) { fa[Find(y)]=Find(x

P1536村村通

这是一个并查集的题,被洛谷评为提高—。 拿到这个题便看出了这是一个裸的并查集,于是就写了一个模板,结果发现连输入都输不进去,一看竟然是多组数据,,然后看到N==0结束,于是便加了一层while。之后提交发现RE了,于是想到shh大佬曾经说RE就是递归写错了,于是仔细检查,发现真的写错了,,然后AC。 1.

P1536 村村通

原题链接 https://www.luogu.org/problemnew/show/P1536 昨天刚学的并查集,今天正好练习一下,于是就找到了这个题 看起来好像很简单,尤其是你明白了思路之后,完全就和板子题没啥区别嘛 话是这么说,但是思路我一开始也没想到,只知道要用并查集和生成树的知识,知道看到了题解里的思路才恍然