AtCoder Beginner Contest 120 D - Decayed Bridges(并查集)
题目链接:https://atcoder.jp/contests/abc120/tasks/abc120_d
题意 先给m条边,然后按顺序慢慢删掉边,求每一次删掉之后有多少对(i,j)不连通(我应该解释对了吧)
删边这个过程就可以从反方向进行,相当于从m到1慢慢加边
然后就把连通的用并查集存起来,另用一个s数组来存每个点和他连通的有几个点
不连通的就减去s[i]*s[j]就好了
初始的是C(n,2) 没边的时候所有点都不连通嘛。
代码如下
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std; const int maxn = 1e5 + ;
pair<ll, ll> p[maxn];
ll par[maxn];
ll s[maxn];
ll sum;
ll ans[maxn];
ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); } int main() {
ll n, m;
scanf("%lld%lld", &n, &m);
for (int i = ; i <= n; i++) {
par[i] = i;
s[i] = ;
}
for (int i = ; i < m; i++) {
scanf("%lld%lld", &p[i].first, &p[i].second);
}
sum = 1LL * n * (n - ) / ;
for (int i = m - ; i >= ; i--) {
ans[i] = sum;
int x = find(p[i].first), y = find(p[i].second);
if (x != y) {
par[y] = x;
sum -= s[x] * s[y];
s[x] += s[y];
}
}
for (int i = ; i < m; i++) printf("%lld\n", ans[i]);
return ;
}
AtCoder Beginner Contest 120 D - Decayed Bridges(并查集)的更多相关文章
- AtCoder Beginner Contest 120 题解
题目链接:https://atcoder.jp/contests/abc120 A Favorite Sound 分析:答案为. 代码: #include <iostream> using ...
- AtCoder Beginner Contest 120 解题报告
为啥最近都没有arc啊... A - Favorite Sound #include <algorithm> #include <iostream> #include < ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
随机推荐
- Guava集合-BiMap
在本篇文章中我们将介绍Guava集合中的BiMap这个接口. com.google.common.collect Interface BiMap<K,V> BiMap接口的父接口是Map& ...
- 用户登陆,退出等基本Action
用户登陆页面user_login.jsp对应action为login.do: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...
- webAppbuilder微件使用教程3 地理处理微件
webAppbuilder微件使用教程 --微件使用进阶地理处理微件 By 李远祥 地理处理是GIS解决问题的关键部分,也是其灵魂所在.由于WebAppBuilder框架的限制,用户如果想要非常灵活的 ...
- 最简单的struts应用
博客园 1.搭建一个简单的Struts2应用 具体为一下几个步骤: 1.引入Struts 2工程所需运行库文件. 2.创建并配置web.xml文件 3.创建一个Action类 4.创建并配置strut ...
- JS中的Map和Set实现映射对象
使用iterable内置的forEach方法 var a = ['A', 'B', 'C']; a.forEach(function (element, index, array) { // elem ...
- ExcelConvert
public static class ExcelConvert { #region - 由数字转换为Excel中的列字母 - public static int ToIndex(string col ...
- USB2.0学习笔记连载(十八):keil实现寄存器的配置及相关函数讲解(二)
其实之前也有提及过,Cypress公司提供的官方文件和应用手册真的可以解决很多问题.做的也很人性化,操作也及其简单,几乎只要在 TD_int()里面配置一些常用的参数即可,其他都可以不用操作. 作为一 ...
- golang xorm应用
github.com/go-xorm/xorm xorm库 http://www.xorm.io/docs/ 手册 xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便.xo ...
- js 正则表达式 整数或小数
非零开头的整数或小数 /^[1-9][0-9]*([.][0-9]+)?$/ 非零开头的整数或两位小数 /^[1-9][0-9]*([.][0-9]{1,2})?$/ /^[1-9][0-9]*([. ...
- [心平气和读经典]The TCP/IP Guide(000)
The TCP/IP Guide [Page 39] The TCP/IP Guide: Introduction and "Guide to The Guide" | 第1章 概 ...