题意:给定一个图,问你每次删除一条边后有几个连通块。

析:水题,就是并查集的运用,倒着推。

代码如下:

#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;
}

最新文章

  1. ExecuteScalar()
  2. 开始使用DOJO(翻译)
  3. ThreadStatic应用(Identity补完)
  4. 学习Linux第二天
  5. jquery自动焦点图
  6. 全球AI界最值得关注的十位科学家
  7. samba常用命令
  8. [置顶] 关于UBUNTU 12.04, 在THINKPAD E430C上WIFI连接不上的问题
  9. 【HEOI 2018】Day2 T2 林克卡特树
  10. 18.11 ROM、RAM、DRAM、SRAM和FLASH区别
  11. 156. Binary Tree Upside Down反转二叉树
  12. jQuery插件slider实现图片轮播
  13. python全栈开发day36-IO多路复用
  14. django ORM聚合函数
  15. Oracle 从共享池删除指定SQL的执行计划
  16. 【转】重装win7后,所有USB接口无法使用(鼠标、键盘、U盘)
  17. [杂谈]ACM启程
  18. 【Hive三】Hive理论
  19. 深入ff and ffbase
  20. 时间动态协同过滤(TimeSVD++)

热门文章

  1. 隐藏 php apache 的版本号
  2. ORACLE impdp 导入数据
  3. notepad 行替换使用指南
  4. Java核心技术II读书笔记(二)
  5. Android堆栈分析
  6. 手动编译Spring4.2源码,以及把源码导入myEclipse中
  7. dubbo-admin在jdk 1.8上部署出错问题
  8. 构建通过 Database.com 提供技术支持的 PhoneGap 应用程序
  9. 康乐不风流之爱解题的pde灌水王张祖锦
  10. Linux服务部署