HDU 4496 D-City (并查集)
2024-10-10 20:15:44
题意:给定一个图,问你每次删除一条边后有几个连通块。
析:水题,就是并查集的运用,倒着推。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int p[maxn];
int e1[maxn*10], e2[maxn*10];
int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
int ans[maxn*10]; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i) p[i] = i; for(int i = 0; i < m; ++i)
scanf("%d %d", &e1[i], &e2[i]); int cnt = n;
for(int i = m-1; i >= 0; --i){
ans[i] = cnt;
int x = Find(e1[i]);
int y = Find(e2[i]);
if(x != y){
p[y] = x;
--cnt;
}
} for(int i = 0; i < m; ++i)
printf("%d\n", ans[i]);
}
return 0;
}
最新文章
- ExecuteScalar()
- 开始使用DOJO(翻译)
- ThreadStatic应用(Identity补完)
- 学习Linux第二天
- jquery自动焦点图
- 全球AI界最值得关注的十位科学家
- samba常用命令
- [置顶] 关于UBUNTU 12.04, 在THINKPAD E430C上WIFI连接不上的问题
- 【HEOI 2018】Day2 T2 林克卡特树
- 18.11 ROM、RAM、DRAM、SRAM和FLASH区别
- 156. Binary Tree Upside Down反转二叉树
- jQuery插件slider实现图片轮播
- python全栈开发day36-IO多路复用
- django ORM聚合函数
- Oracle 从共享池删除指定SQL的执行计划
- 【转】重装win7后,所有USB接口无法使用(鼠标、键盘、U盘)
- [杂谈]ACM启程
- 【Hive三】Hive理论
- 深入ff and ffbase
- 时间动态协同过滤(TimeSVD++)