当前位置:网站首页>HDU Ice_cream‘s world I (并查集判环)
HDU Ice_cream‘s world I (并查集判环)
2022-04-22 06:18:00 【S atur】
题意:给出一个 n 个点 m 条边的无重边图,试找出其中最多的无重叠环个数。

思路:将并查集的合并函数变形一下就行啦~不用考虑取大环还是小环的问题,只要新加的一条边能形成闭环,必定是新加一个单独环或将原先某个环一分为二,贡献都为1。
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9+7;
const int N = 2e5 + 5;
inline void read(int &x){
char t=getchar();
while(!isdigit(t)) t=getchar();
for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}
int n, m, f[N], ans;
int find(int x)
{
return x == f[x]?x:f[x] = find(f[x]);
}
int join(int x, int y)
{
int a = find(x), b = find(y);
if(a != b){
f[a] = b;
return 0;
}
return 1; //如果根节点一样,就能形成一个环
}
signed main()
{
IOS;
cin >> n >> m;
for(int i = 0; i < n; i ++) f[i] = i;
while(m --){
int u, v; cin >> u >> v;
ans += join(u, v);
}
cout << ans << endl;
return 0;
}
版权声明
本文为[S atur]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Satur9/article/details/117342901
边栏推荐
- 递归找序列集合
- I.Jam-packed (均分/最大最小值) (2021年度训练联盟热身训练赛第五场)
- 快排与归并排序
- (1) Download and installation of SQL Server
- Error: (vlog-2892) Net type of 'i_ yc422' must be explicitly declared.
- C language | bitwise operator
- Precautions for using feign to make remote calls
- [DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted
- The only storage area in the JVM where GC and oom will not occur
- Codeforces Round #778
猜你喜欢

L2-005 集合相似度(set判重)

Win10 modify command line default font

抽象类和抽象方法

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

L2-002 链表去重(测试点1的坑)

L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)

In the process of class loading, the allocation area of class variables is different from that of instance variables

我们常说的栈帧的内部结构是什么样的呢?

Relationship between A5 transceiver signal VOD and pre emphasis adjustment
![Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss
随机推荐
链表难题记录一
When latex uses the template, the caption title of the picture cannot be left aligned
76 · 最长上升子序列
[number theory] congruence (V): multivariate linear congruence equation
L2-001 紧急救援 (最短路Dijkstra的扩展 - 最短路径数&路径最大权值)
重写与重载的定义与区别
Escape analysis in JVM can realize that memory is not allocated on the heap
278 · 绘制填充
(2) Basic configuration of SQL server and connection to SQL server using Navicat
Leetcode - 7 - (nearest common ancestor of binary tree, rotation array, direct of binary tree, next permutation, combined sum)
[number theory] prime number (V): Mason prime number (lucas_lehmer decision method)
Detailed tree array template -- Theory and code implementation
synchronized锁优化的一些机制(锁升级)
LeetCode - 6 - (字符串相乘、下一个更大元素<ⅠⅡⅢ>、k个一组翻转链表)
(4) Character set in SQL Server (collation)
C language | bitwise operator
1232 · 爆破气球的最小箭头数
I.Jam-packed (均分/最大最小值) (2021年度训练联盟热身训练赛第五场)
Hand tearing algorithm -- LRU cache elimination strategy, asked so often
链表习题详解