hdu2417并查集
来源:互联网 发布:ubuntu怎么配置ip 编辑:程序博客网 时间:2024/06/10 23:33
//delete point,transform to,using other point replace it
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;const int max_n=2*1e6;int n,m,tot;int vis[max_n];struct node{ int fa,rep;}e[max_n];void init(){ for(int i=0;i<n;i++) { e[i].rep=i; e[i].fa=i; } tot=n-1;}int find(int x){ if(e[x].fa==x)return x; return e[x].fa=find(e[x].fa);}void Union(int fa,int fb){ e[fb].fa=fa;}void Delete(int x){ e[x].rep=++tot; e[tot].fa=tot;}int main(){ int cas=0; while(cin>>n>>m) { if(n==0 && m==0)break; init(); for(int i=1;i<=m;i++) { char s[10]; scanf("%s",s); if(s[0]=='M') { int a,b; scanf("%d%d",&a,&b); int fa=find(e[a].rep),fb=find(e[b].rep); if(fa!=fb)Union(fa,fb); } else { int a; scanf("%d",&a); Delete(a); } } memset(vis,0,sizeof(vis)); int ans=0; for(int i=0;i<n;i++) { int cur=find(e[i].rep); if(!vis[cur]) { vis[cur]=1; ans++; } } printf("Case #%d: %d\n",++cas,ans); } return 0;}
0 0
- hdu2417并查集
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 并查集
- 并查集
- 所谓并查集
- UVA - 10534Wavio Sequence(LIS)
- usb serial驱动流程分析
- leetcode Best Time to Buy and Sell Stock III
- 在ROS的PPPOE IP变的时候自动修改DNS记录,实现DDNS
- codechef August Challenge 2014 第五个题目
- hdu2417并查集
- 一个带动画的页面底部的TabBar的实现
- 异常处理表
- 桥接模式处理下机计算金额问题
- WDF驱动模型
- Android事件处理
- 对微信的一点研究
- 经典SQL例题
- 基于ARM平台分析Linux系统调用过程