Problem Description

Luxer is a really bad guy. He destroys everything he met. 
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. 
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.

 

Input

First line of the input contains two integers N and M. 
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. 
Constraints: 
0 < N <= 10000 
0 < M <= 100000 
0 <= u, v < N. 

 

Output

Output M lines, the ith line is the answer after deleting the first i edges in the input.

 逆向思维的重要性!
#include<cstdio>
#include<iostream>
#include<string.h>
#include<stack>
#define maxn 10005
using namespace std;
int
pre[maxn];
int
num;
void
init()
{

for
(int i=;i<maxn;i++) pre[i]=i;
}

int
find(int x)
{

if
(pre[x]==x) return x;
else return
pre[x]=find(pre[x]);
}

void
merge(int x,int y)
{

x=find(x);
y=find(y);
if
(x!=y)
{

num--;
pre[y]=x;
}
}

int
main()
{

cin.sync_with_stdio(false);
int
n,m;
while
(cin>>n>>m)
{

stack<int> fuck,fuck2;
int
ret[m];
while
(m--)
{

int
x,y;
cin>>x>>y;
fuck.push(x),fuck.push(y);
}

init();
num=n;
int
rett=;
while
(!fuck.empty())
{

int
yy=fuck.top();
fuck.pop();
int
xx=fuck.top();
fuck.pop();
ret[rett++]=num;
merge(xx,yy);
}

for
(int i=rett-;i>=;i--) cout<<ret[i]<<endl;
}

return
;
}

最新文章

  1. Snort 安装 配置 - Archlinux
  2. block,inline,inline-block的区别
  3. Codeforces Round #281 (Div. 2) D. Vasya and Chess 水
  4. cocos2d-x 详解之 CCLayer(触摸事件)
  5. map的例子
  6. oracle进制转换
  7. Java设计模式---组合模式
  8. Material风格的Quick组件,妈妈再也不用担心我的界面不好看了
  9. linux win7双系统
  10. 浅谈mapreduce程序部署
  11. NOIP2001-普及组复赛-第二题-最大公约数和最小公倍数问题
  12. USACO Section 1.2 Transformations 解题报告
  13. [ext4]06 磁盘布局 - 特殊inode
  14. ELK学习总结(3-1)elk的基本查询
  15. tensorflow从入门到放弃-0
  16. Jmeter post请求传参问题
  17. 树中的路径和 Sum of Distances in Tree
  18. Android Studio 常见问题及解决方法
  19. 搭建Hexo博客(三)—换电脑继续写Hexo博客
  20. Linux 查看进程的线程数

热门文章

  1. OpenJudge计算概论-买房子
  2. ISO/IEC 9899:2011 条款5——5.2.1 字符集
  3. Qt坐标系以及自定义可移动控件
  4. Mac更新npm和node版本
  5. Mac下载工具软件提示损坏
  6. Asp.net C# 遍历Excel中的表格名称
  7. ELK之elasticsearch插件导致filebeat没有上传日志至elasticsearch解决办法
  8. iOS-浅谈iOS中三种生成随机数方法
  9. laravel-excel 表格 文档翻译笔记
  10. PPM / PGM / PBM 图像文件格式