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
    评论
【問題概要】 縦 $H$ 行、横 $W$ 列のマスがあります。 上から $i$ 行目、左から $j$ 列目のマスを $(i,j)$ とします。 最初、すべてのマスは白色であり、マス $(i,j)$ は $C_{i,j}$ という文字が書かれています。 あなたは、以下の操作を好きな回数だけ行うことができます。 操作: 黒色を塗られたマス $(i,j)$ を選び、以下のいずれかの操作を行う。 (1) $C_{i,j}$ を $1$ 減らす。 (2) $C_{i,j}$ を $1$ 増やす。 ただし、この操作を行う際には、必ずしも $C_{i,j}$ が $0$ 以上である必要はありません。 操作後、すべてのマスが白色になっている場合、操作回数の最小値を求めてください。 【制約】 ・$1 \leq H,W \leq 50$ ・$0 \leq C_{i,j} \leq 10^{9}$ ・$C_{i,j}$ は整数である。 ・少なくとも $1$ つのマスには文字が書かれている。 【入力】 入力は以下の形式で標準入力から与えられる。 $H$ $W$ $C_{1,1}$ $C_{1,2}$ ... $C_{1,W}$ $C_{2,1}$ $C_{2,2}$ ... $C_{2,W}$ ... $C_{H,1}$ $C_{H,2}$ ... $C_{H,W}$ 【出力】 操作回数の最小値を出力せよ。 【入力例】 3 3 3 1 4 1 5 9 2 6 5 【出力例】 2 【入力例】 3 3 1 1 1 1 1 1 1 1 1 【出力例】 0 【入力例】 4 4 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 【出力例】 2 【入力例】 5 5 0 0 1 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 【出力例】 0 【解法】 まず、全体の合計を求めます。 次に、全体の合計が $0$ の場合、操作回数は $0$ となります。 全体の合計が $1$ 以上の場合、以下の操作を行います。 1. 全体の合計を $2$ で割り、切り捨てた値を $S$ とします。 2. 全体の合計が奇数の場合、$S$ を $1$ 増やします。 3. 以下の操作を繰り返します。 1. 最大値を取るマスを選び、そのマスの値を $2$ 減らします。 2. 上記操作によって、全体の合計が $S$ 以下になる場合、操作回数を出力して終了します。 上記操作によって、全体の合計が $S$ 以下になることが証明できます。 また、上記操作によって操作回数が最小になることが証明できます。 【コード】
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值