Hdu 4496 D-City
时间:2019-06-14
本文章向大家介绍Hdu 4496 D-City,主要包括Hdu 4496 D-City使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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.
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.
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
解题思路
反向并查集求联通块数;
逆向思维,假设一开始每个点都不连通的,从给定的边中逆序读入数据,相当于给初始化的图增加边,如果有两个联通分量联通了则现在的连通分量块数等于上一个连通分量的块数 - 1;
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 const int maxn = 100100; 5 int n, m; 6 int f[maxn], a[maxn], b[maxn], sum[maxn]; 7 void init(){ 8 for(int i = 0; i < n; i++) f[i] = i; 9 } 10 int getf(int v){ 11 if(f[v] == v) return v; 12 else return f[v] = getf(f[v]); 13 } 14 int main(){ 15 while(~scanf("%d %d", &n, &m)){ 16 init(); 17 for(int i = 1; i <= m; i++){ 18 scanf("%d %d", &a[i], &b[i]); 19 } 20 sum[m] = n; 21 for(int i = m; i >= 1; i--){ 22 int t1 = getf(a[i]), t2 = getf(b[i]); 23 if(t1 != t2){ 24 f[t2] = t1; 25 sum[i - 1] = sum[i] - 1; 26 } 27 else{ 28 sum[i - 1] = sum[i]; 29 } 30 } 31 for(int i = 1; i <= m; i++){ 32 printf("%d\n", sum[i]); 33 } 34 } 35 return 0; 36 }
原文地址:https://www.cnblogs.com/zoom1109/p/11025180.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- mysql explain用法和结果的含义
- 超详细整理!Pandas实用手册(PART I)
- NLP简报(Issue#9)
- 这些pandas技巧你还不会吗 | Pandas实用手册(PART II)
- 芝麻街跨界NLP,没有一个ERNIE是无辜的
- python实现多层感知机
- 数据分析必备!Pandas实用手册(PART III)
- python实现决策树
- NLP简报(Issue#10)
- Transformer温故知新
- 【作者解读】ERNIE-GEN : 原来你是这样的生成预训练框架!
- Python中那些低调有趣的模块
- 01背包问题讲解(动态规划)
- python实现随机森林
- 在mysql中order by是怎样工作的?