HDU 4496 并查集
来源:互联网 发布:get it和got it 编辑:程序博客网 时间:2024/06/06 02:47
点击打开链接
题意:给了一个图,问你依次将边删除后,图还剩下多少个联通的块
思路:就是问你有多少个块嘛,可以用并查集来判断是不是在一个块内,然后我们将顺序反过来判断一下就可以了,水题~~~
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=100010;int f[maxn],u[100010],v[100010],ans[100010];int find1(int x){ if(x!=f[x]) f[x]=find1(f[x]); return f[x];}void unite(int a,int b){ int aa=find1(a); int bb=find1(b); if(aa==bb) return ; f[aa]=bb;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=-1){ for(int i=0;i<=n;i++) f[i]=i; for(int i=0;i<m;i++) scanf("%d%d",&u[i],&v[i]); int sum=0; for(int i=m-1;i>=0;i--){ ans[i]=n-sum; if(find1(u[i])!=find1(v[i])){ unite(u[i],v[i]); sum++; } } for(int i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- hdu 4496 并查集
- hdu 4496(并查集)
- HDU 4496 并查集
- hdu-4496-并查集
- [并查集] HDU 4496
- HDU 4496 D-City(并查集)
- hdu 4496 D-City 并查集
- HDU 4496 D-CITY(并查集)
- 【并查集】-HDU-4496-D-City
- HDU 4496 D-City(并查集)
- hdu 4496 D-City(并查集)
- hdu 4496 并查集的变形
- 【HDU】4496 D-City 并查集
- HDU-4496-逆向并查集
- hdu 4496 D-City 并查集
- hdu 4496 D-City(并查集)
- 并查集--HDU - 4496 D-City
- hdu 4496 D-City 并查集
- GIT - 基本概念(对象,索引,提交)
- 【CodeForces】#621A - Wet Shark and Odd and Even(水)
- Spring定时任务之Quartz
- 如何将Qt窗口的内容转化为图片?
- MIPS多周期CPU设计之使用时钟上升沿触发PC模块的实现方案
- HDU 4496 并查集
- Android自动化测试-从入门到入门(3)Espresso入门
- delphi工作笔记利用indy10发送邮件
- Sqlite3常用的插入方法及性能测试
- [JZOJ3401]【GDOI2014模拟】Pty爬山
- TCP学习~~
- 【codeforces】-#621-AWet Shark and Odd and Even(n个数最大偶数和)
- PullToRefreshListView
- mysql存储过程----三种定义变量方式