D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 1315    Accepted Submission(s): 496

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
Hint
The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first.
The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together.
But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block.
Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
 
Source
 
解题报告
给定一个图,N个点,M条边,求按顺序删边后剩下的多少联通块。
一般我们都是用并查集来加边,删边也能够用并查集,删除第1条边等于加上m-1条边。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 100000+10
#define N 10000+10
using namespace std;
int n,m;
struct node
{
int u,v;
} edge[M];
int B[N],ans[M];
int fine(int x)
{
if(B[x]!=x)
B[x]=fine(B[x]);
return B[x];
}
int main()
{
int i;
while(~scanf("%d%d",&n,&m))
{
memset(ans,0,sizeof(ans));
memset(edge,0,sizeof(edge));
for(i=0;i<n;i++)
B[i]=i;
for(i=0; i<m; i++)
{
scanf("%d%d",&edge[i].u,&edge[i].v);
}
int sum=n;
for(i=m-1;i>=0;i--)
{
ans[i]=sum;
int xx=fine(edge[i].u);
int yy=fine(edge[i].v);
if(xx!=yy)
{
sum--;
B[xx]=yy;
}
}
for(i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}

HDU4496_D-City(并查集删边/逆向)的更多相关文章

  1. ZOJ 3261 - Connections in Galaxy War ,并查集删边

    In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ...

  2. HDU——2473Junk-Mail Filter(并查集删点)

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  3. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  4. HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)

    题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...

  5. hdu 4496 并查集 逆向 并查集删边

    貌似某大犇说过 正难则反,,, 题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中, 那么此时联通快个数少一,否则不变 #i ...

  6. nyoj_1022:合纵连横(并查集删点)

    题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ...

  7. ZOJ - 3261 Connections in Galaxy War(并查集删边)

    https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ...

  8. nyoj 1022:合纵连横(并查集删点)

    题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ...

  9. Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边

    #include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...

随机推荐

  1. Android状态栏颜色修改

    android状态栏颜色修改   状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的.   5.x环境下 方式一,状态栏将显示为纯净的颜色,没有渐变效果 /** * 状 ...

  2. swift调用相机和相册

    简单实现swift调用相机和相册的功能,分享代码与学习swift的童鞋共同进步 import UIKit class ViewController: UIViewController,UIImageP ...

  3. Android自定义控件(一)——开关控件

    Google 在 API 14 开始才新增了Switch 控件. 因此,我们可以选择自己封装一个Switch . 效果如图: View主要代码: public class SwitchView ext ...

  4. CodeSmith使用总结--读取一个表试试

    我感觉CodeSmith对于我的最大用途是不用我手动创建每个表的Model.BLL和DAL了,那些繁琐的工作真的让我很无语. CodeSmith要读取数据库中的表就要先连接数据库. 新建一个数据库连接 ...

  5. 修改EF的默认约定模型的方式

    EF默认使用约定的模型,但是有时类不遵从约定,我们需要能够执行进一步的配置.对此有两种方法:数据注释和Fluent API 注释 class User { [Key] public int Key{ ...

  6. S2SH简单介绍和理解

    struts2简介 Struts2是由WebWork基础上发展起来的,与struts1比较,选用struts2的理由是:①Struts1要求Action类继承一个抽象基类,而Struts2Action ...

  7. iOS_SN_BlueTooth (二)iOS 连接外设的代码实现

    原文:http://www.cocoachina.com/ios/20150917/13456.html?utm_source=tuicool&utm_medium=referral 上一篇文 ...

  8. iOS在MRC工程环境下下使用ARC的方法

  9. ifstream中文路径问题分析

    最近维护一个项目,遇到了ifstream在中文路径下打开文件失败的bug,我搜索了一下,最后整理成下文以后日后查阅. 一.问题重现 vs2008下创建一个简单win32工程. 使用ANSI编码方式:项 ...

  10. jar打包命令

    jar查看jar命令的使用 1. jar cvf 生成jar包的完整名称 要生成的class文件所在目录以及名称 jar -cvf aa.jar *.* 2.将jar包放到环境变量中,可以不用把cla ...