poj_2524_题解
来源:互联网 发布:唱歌测音软件 编辑:程序博客网 时间:2024/05/17 01:44
并查集
#include<iostream>#include<algorithm>using namespace std;int parent[50001];int find(int* p,int r){while(p[r]!=r)r=p[r];return r;}int main(){int n,m;int c=0;while(1){cin>>n>>m;if(n==0&&m==0)break;else if(m==0){c++;cout<<"Case "<<c<<": "<<n<<endl;}else{c++;for(int i=0;i<n;i++){parent[i]=i;}int ans=n;for(int i=0;i<m;i++){int x;int y;cin>>x>>y;int a=find(parent,x); int b=find(parent,y);if(a>b){parent[a]=b;ans--;}else if(a<b){parent[b]=a;ans--;}} cout<<"Case "<<c<<": "<<ans<<endl;}} return 0;}
阅读全文
0 0
- poj_2524_题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解
- 题解~~~~
- 题解。。。。
- 题解
- 题解
- 1002 题解
- pku1001题解
- 隧道尽头的灯
- XSS挑战之旅 解题记录
- 面向过程和面向对象demo
- 正则表达式
- 每日一诗词 —— 定风波·莫听穿林打叶声
- poj_2524_题解
- 基于微软案例数据挖掘之Microsoft 神经网络分析算法
- Git撤销修改场景及对应指令详解
- Maven 隐式依赖包冲突导致程序报错
- Asp.net+Vue2构建简单记账WebApp之一(设计)
- 不同文件中类外定义demo
- Codeforces 570D Tree Requests
- HTTP协议简介
- 大数据预科班作业5~6