D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3665    Accepted Submission(s): 1306

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.
 

Sample Input

5 10
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4
 

Sample Output

1
1
1
2
2
2
2
3
4
5
 
 并查集,题目问每破坏一条边有多上连通块,可以倒推把边连起来,每次查询有多少个集合
 //2016.8.9
#include<iostream>
#include<cstdio>
#include<stack> using namespace std; int fa[], arr[][], cnt; int getf(int i)
{
if(fa[i]==i)return i;
return fa[i] = getf(fa[i]);
} int init(int n)
{
for(int i = ; i < n; i++)
fa[i] = i;
} int Merge(int a, int b)
{
int af = getf(a);
int bf = getf(b);
if(af!=bf){
fa[bf] = af;
cnt--;
}
} int main()
{
int n, m, u, v;
while(cin>>n>>m)
{
init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d", &arr[i][], &arr[i][]);
}
stack<int> s;
cnt = n;
for(int i = m-; i >= ; i--)
{
s.push(cnt);
Merge(arr[i][], arr[i][]);
}
while(!s.empty())
{
int ans = s.top();
cout<<ans<<endl;
s.pop();
}
} return ;
}

最新文章

  1. Spark的精简安装步骤---陈楠心血总结
  2. PHP计算一年有多少周,每周开始日期和结束日期
  3. cuda fft 计算
  4. 动手实现自己的 STL 容器 《1》---- vector
  5. UVa 112 - Tree Summing(树的各路径求和,递归)
  6. ASP.NET MVC 学习第三天
  7. CodeForces 705B Spider Man (水题)
  8. 一段jquery代码,保存
  9. 随心所欲~我也做个集合遍历器吧(自己的foreach,委托的威力)
  10. ANT 操控 ORACLE数据库实践
  11. KoaHub.js可借助 Babel 编译稳定运行在 Node.js 环境上
  12. Unreal Engine 4 Radiant UI 入门教程(零)在场景中摆放网页
  13. JavaScript--我发现!原来你是这样的JS(1)
  14. 函数的非固定参数-Day3
  15. opensuse安装pycurl失败记录
  16. static与final的区别
  17. python 自动发现Java端口
  18. Windows服务器时间不同步问题
  19. 使用VLC推送TS流(纯图版)
  20. Couldn&#39;t find an AF_INET address for

热门文章

  1. Contaminated Milk
  2. [转帖]完美解决NVIDIA最新显卡驱动无法安装。(修改教程篇,各机型都可以)
  3. Hibernate---第一个helloworld程序 (XML版本, annotation版本)
  4. javascript 中 arguments.callee属性
  5. implemented loader.php
  6. CodeForces 618C CodeForces 618C
  7. ios中操作技巧
  8. 神经网络 Neuroph - Java Neural Network Platform Neuroph
  9. Delphi获取文件的大小(实际&amp;物理)
  10. mysql show命令