HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496
题意:
给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数。
思路:
离线处理删边,从后往前处理变成加边,用并查集维护连通块个数。其实这题和BZOJ 1015差不多。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int n, m, tot, num;
int p[], ans[], head[]; struct node
{
int u,v;
}query[]; struct edge
{
int v,next;
}e[]; void addEdge(int u, int v)
{
e[tot].v = v;
e[tot].next = head[u];
head[u] = tot++;
} int finds(int x)
{
return p[x]==x?x:p[x]=finds(p[x]);
} void update(int u)
{
for(int i=head[u];i!=-;i=e[i].next)
{
int v = e[i].v;
int x = finds(u);
int y = finds(v);
if(x!=y) {p[x]=y;num--;}
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
tot = ;
memset(head,-,sizeof(head));
for(int i=;i<n;i++) p[i]=i;
for(int i=;i<=m;i++)
scanf("%d%d",&query[i].u,&query[i].v); num = n;
ans[m] = n;
for(int i=m;i>;i--)
{
int u = query[i].u;
int v = query[i].v;
addEdge(u,v);
addEdge(v,u);
update(u);
update(v);
ans[i-]=num;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
}
return ;
}
HDU 4496 D-City(逆向并查集)的更多相关文章
- HDU - 4496 City 逆向并查集
思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...
- HDU_4496_逆向并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4496 逆向并查集,先读取,然后从后向前join每次保存答案即可. #include<iostream> ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- zoj 3261 逆向并查集+离线处理
题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- 逆向并查集 hrbust 1913
#include<iostream> //由于拆除并查集的方法太难或者没有#include<cstdio> //可以先将所有没有拆的桥连接 再逆向操作 断开变成连接 反向输出# ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
随机推荐
- flask模板应用-自定义错误页面
自定义错误页面 当程序返回错误响应时,会渲染一个默认的错误页面,我们可以注册错误处理函数来处理错误页面 错误处理函数和视图函数很相似,返回值将作为响应的主题,因此我们先要创建错误页面的模板文件.为了和 ...
- USMART 组件移植到STM32
USMART是由ALIENTEK开发的一个串口调试助手组件,通过它可以通过串口调试助手,调用程序里面的任何函数并执行,单个函数最多支持10个输入参数,并支持函数返回值显示. USMART支持的参数类型 ...
- Codeforce 287A - IQ Test (模拟)
In the city of Ultima Thule job applicants are often offered an IQ test. The test is as follows: the ...
- 判断是移动端还是PC端
// 判断是移动端还是PC端 $http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USE ...
- c#md5加密的简单用法
using System.Security.Cryptography; //MD5 md5 = MD5.Create(); MD5 md5 = new MD5CryptoServiceProvider ...
- 使用jwt
jwt:json web token 使用jwt的好处1,无状态2解耦3,更适合于移动端Android,ios4,性能更好5,能够不考虑csrf攻击 ①,客户端请求 Date date = new D ...
- Oracle误删除数据恢复
select * from tablename as of timestamp to_timestamp('2018-05-04 13:30:00','yyyy-MM-dd hh24:mi:ss') ...
- LogUtil工具
package com.develop.web.util; import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logg ...
- springMVC之一(页面<--->控制器 互相传值,转发和重定向)
#页面--->控制器1.request:不建议使用2.使用属性传值(建议使用)@RequestParam("name") String username3.使用Bean对象传 ...
- jsoi r2d1t3的50分
#include<bits/stdc++.h> using namespace std; int n,r,x,y; double ans; double dis(int x,int y){ ...