atcoder abc120 D

D - Decayed Bridges


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

There are NN islands and MM bridges.

The ii-th bridge connects the AiAi-th and BiBi-th islands bidirectionally.

Initially, we can travel between any two islands using some of these bridges.

However, the results of a survey show that these bridges will all collapse because of aging, in the order from the first bridge to the MM-th bridge.

Let the inconvenience be the number of pairs of islands (a,b)(a,b) (a<ba<b) such that we are no longer able to travel between the aa-th and bb-th islands using some of the bridges remaining.

For each ii (1≤i≤M)(1≤i≤M), find the inconvenience just after the ii-th bridge collapses.

Constraints

  • All values in input are integers.
  • 2≤N≤1052≤N≤105
  • 1≤M≤1051≤M≤105
  • 1≤Ai<Bi≤N1≤Ai<Bi≤N
  • All pairs (Ai,Bi)(Ai,Bi) are distinct.
  • The inconvenience is initially 00.

Input

Input is given from Standard Input in the following format:

NN MM
A1A1 B1B1
A2A2 B2B2
⋮⋮
AMAM BMBM

Output

In the order i=1,2,...,Mi=1,2,...,M, print the inconvenience just after the ii-th bridge collapses. Note that the answer may not fit into a 3232-bit integer type.


Sample Input 1 Copy

Copy

4 5
1 2
3 4
1 3
2 3
1 4

Sample Output 1 Copy

Copy

0
0
4
5
6

For example, when the first to third bridges have collapsed, the inconvenience is 44 since we can no longer travel between the pairs (1,2),(1,3),(2,4)(1,2),(1,3),(2,4) and (3,4)(3,4).


Sample Input 2 Copy

Copy

6 5
2 3
1 2
5 6
3 4
4 5

Sample Output 2 Copy

Copy

8
9
12
14
15

Sample Input 3 Copy

Copy

2 1
1 2

Sample Output 3 Copy

Copy

1

并查集倒着处理即可

#include <bits/stdc++.h>
#include <time.h>
#define first fi
#define second se


using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) {
    return a < -eps ? -1 : a > eps;
}
inline int cmp(db a,db b) {
    return sign(a - b);
}
ll mul(ll a,ll b,ll c) {
    ll res = 1;
    while(b) {
        if(b & 1) res *= a,res %= c;
        a *= a,a %= c,b >>= 1;
    }
    return res;
}
ll phi(ll x) {
    ll res = x;
    for(ll i = 2; i * i <= x; i++) {
        if(x % i == 0) res = x / i * (i - 1);
        while(x % i == 0) x /= i;
    }
    if(x > 1) res = res / x  * (x - 1);
    return res;
}
ll c,n,k,x,y;
string str;
ll a[maxn],b[maxn],r[maxn],fa[maxn];
ll Find(int x) {
    if(x == fa[x]) return fa[x];
//    int i = fa[x];
    fa[x] = Find(fa[x]);
//    r[x] += r[i];
    return fa[x];
}
int main() {
    int m;
    ios::sync_with_stdio(false);
    while(cin >> n >> m) {
        for(int i = 1; i <= n; i++) fa[i] = i,r[i] = 1;
        for(int i = 1; i <= m; i++) {
            cin >> a[i] >> b[i];
        }
        vector<ll>anss;
        ll ans = (n - 1) * (n)  / 2;
        for(int i = m; i >= 1; i--) {
                anss.push_back(ans);
            int X = Find(a[i]);
            int Y = Find(b[i]);
            if(X != Y) {
                fa[Y] = X;
                ans -= r[X] * r[Y];
                r[X] += r[Y];
            }
        }
        reverse(anss.begin(),anss.end());
        for(auto d:anss){
            cout << d <<endl;
        }

    }
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值