UPC-Decayed Bridges

学习犹如逆水行舟,不进则退

Decayed Bridges

题目描述

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.

输入

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

AM BM

输出

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).

题目简述
本来每座桥都按照输入顺序建造桥梁,但是随着时间的增加,这些桥都会一次腐烂,腐烂了之后就不通了,就可能一些岛屿之间不能走了。就问随时间腐烂,会有几个岛屿两两之间,有多少不能通。
解题思路
如果一座桥都不修的话,那么不方便度是(n-1)*n/2,每修一座桥就减去相应值,这个值是链接两者的已链接数量相乘。并且做好标记,直接模拟即可。

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');
    }
}

书山有路勤为径,学海无涯苦作舟。
发布了32 篇原创文章 · 获赞 12 · 访问量 1186

猜你喜欢

转载自blog.csdn.net/qq_35339563/article/details/104582911