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. AAM(Active Appearance Model)算法介绍

    前面介绍ASM算法(http://blog.csdn.net/carson2005/article/details/8194317)的时候,笔者提到,ASM是基于统计形状模型的基础上进行的,而AAM则 ...

  2. 用消息在Win32控制台程序多线程间进行通讯

      #include <stdio.h> #include <windows.h> //#include <iostream> //#include <pro ...

  3. 用SNMP协议实现系统信息监控--CentOS

    (1)  安装SNMP客户端以及服务端 安装内容为三项:net-snmp  net-snmp-devel   net-snmp-utils 安装命令:yum install net-snmp net- ...

  4. iOS~~MD5加密

    // 一般加密 +(NSString *)md5String:(NSString *)str { const char *password=[str UTF8String]; unsigned cha ...

  5. CSS书写规范、顺序和命名规则

    写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考   这些是参考了国外一些文章以及我的个 ...

  6. 视差滚动(Parallax Scrolling)效果的原理与实现

    视差滚动(Parallax Scrolling)效果的原理与实现1.视差滚动效果的主要特点:    1)直观的设计,快速的响应速度,更合适运用于单页面    2)差异滚动 分层视差    页面上很多的 ...

  7. apache 出现Index of /的页面解决

    在apache安装文件中找到http.conf配置文件,打开找到 Options Indexes FollowSymLinks 这行,在Indexes前加一个-(减号),也就是该完之后是这样: Opt ...

  8. PHP第二课笔记

    ★Php的基本概念 快速入门案例 test.php <html> <body> //<?php  ?>是运行在服务端 <?php   echo 'hello' ...

  9. Nmap官网中众多文档如何查看

    打开Nmap(nmap.org)官网后,会看多个关于文档的链接,熟悉之后会发现有三类,Reference Guide,Books,Docs.通过熟悉知道Doc是文档的入口,且下面是对Doc页面的翻译, ...

  10. [linux] linux知识积累(不断更新中…)

    一.vim知识 f(find)命令也可以用于移动,fx将找到光标后第一个为x的字符,3fd将找到第三个为d的字符. D 删除当前字符至行尾.D=d$ :split或new 打开一个新窗口,光标停在顶层 ...