UPC-Decayed Bridges

Learning is like rowing upstream

Decayed Bridges

Title Description

There are N islands and M bridges.
The i-th bridge connects the Ai-th and Bi-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 M-th bridge.
Let the inconvenience be the number of pairs of islands (a,b) (a<b) such that we are no longer able to travel between the a-th and b-th islands using some of the bridges remaining.
For each i (1≤i≤M), find the inconvenience just after the i-th bridge collapses.
Constraints
All values in input are integers.
·2≤N≤105
·1≤M≤105
·1≤Ai<Bi≤N
·All pairs (Ai,Bi) are distinct.
·The inconvenience is initially 0.

Entry

Input is given from Standard Input in the following format:
N M
A1 B1
A2 B2

AM BM

Export

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

Sample Output

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

Sample Input

0
0
4
5
6

Hint

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

Topic Brief
Originally each bridge are building bridges in the input order, but the increase over time, these bridges will be a rotten, rotten after not pass, could not walk between some of the islands. Asked decay over time, there will be several twenty-two between islands, the number can not pass.
Problem-solving ideas
If you do not repair a bridge, then the degree is not convenient (n-1) * n / 2, each repair is a bridge minus the corresponding value, which is the link between the twoHas multiplied the number of links. And well marked, you can direct simulation.

Time to AC

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<queue>
#include <stack>
#include<string>
#include<utility>
#include<math.h>
#include<stdio.h>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
    ll c = getchar(), Nig = 1, x = 0;
    while (!isdigit(c) && c != '-')c = getchar();
    if (c == '-')Nig = -1, c = getchar();
    while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
    return Nig * x;
}
inline void out(ll a)
{
    if (a < 0)putchar('-'), a = -a;
    if (a >= 10)out(a / 10);
    putchar(a % 10 + '0');
}
#define read read()
int fa[100005];
int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
    int fx = find(x), fy = find(y);
    if (fx != fy)fa[fx] = fy;
}
ll save[100005];
ll num[100005];
ll ans[100005];
struct node {
    int a; int b;
}point[100005];
int main()
{
    for (int i = 0; i < 100005; i++)
    {
        fa[i] = i;
        num[i] = 1;
    }
    int q = 0;
    ll n = read, t = read;
    ll tot = (n - 1) * n / 2;
    for (int i = 0; i < t; i++) {
        point[i].a = read;
        point[i].b = read;
    }
    for (int i = t - 1; i >= 0; i--)
    {
        ans[q++] = tot;
        int ta = point[i].a, tb = point[i].b;
        if (find(ta) == find(tb))continue;
        tot -= num[find(ta)] * num[find(tb)];
        num[find(tb)] += num[find(ta)];
        num[find(ta)] = 0;
        merge(ta, tb);
    }
    for (int i = q - 1; i >= 0; i--)
    {
        out(ans[i]);
        putchar('\n');
    }
}

There is a road ground for the tracks, Boundless Learning bitter for the boat.
Published 32 original articles · won praise 12 · views 1186

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104582911