杭电4496--D-City(***并查集***)

D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2649    Accepted Submission(s): 922

Problem Description

Luxer is a really bad guy. He destroys everything he met.
One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.

Input

First line of the input contains two integers N and M.
Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
Constraints:
0 < N <= 10000
0 < M <= 100000
0 <= u, v < N.

Output

Output M lines, the ith line is the answer after deleting the first i edges in the input.

Sample Input

5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4

Sample Output

1 1 1 2 2 2 2 3 4 5

Hint

The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there‘s only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.

Source

2013 ACM-ICPC吉林通化全国邀请赛——题目重现

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  5498 5497 5496 5495 5494

求联通块数量 ,  反向思维, 很神奇。

#include <cstdio>
#include <cstring>
#include <iostream>
#define N 10001
#define M 100001
using  namespace std;
int n, m;
int a[M], b[M], num[M], father[N];
void Init()
{
    num[m-1] = n;
    for(int i = 0; i < n; i++)
        father[i] = i;
}
int Find(int a)
{
    if(a == father[a])
        return a;
    else
        return father[a] = Find(father[a]);
}
void Mercy(int a, int b, int c)
{
    int Q = Find(a);
    int P = Find(b);
    if(Q != P)
    {
        father[Q] = P;
        num[c-1] = num[c] - 1;
    }
    else
        num[c-1] = num[c];
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        Init();
        for(int i = 0; i < m; i++)
            scanf("%d%d", &a[i], &b[i]);
        for(int i = m-1; i >= 0; i--)   //反向 ;
            Mercy(a[i], b[i], i);
        for(int i = 0; i < m; i++)
            printf("%d\n", num[i]);
    }
    return 0;
}
时间: 2024-08-14 00:16:58

杭电4496--D-City(***并查集***)的相关文章

杭电1232(图—并查集)

畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30798    Accepted Submission(s): 16193 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相

杭电3635-Dragon Balls(并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4582    Accepted Submission(s): 1747 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

杭电ACM1269——迷宫城堡~~并查集

这题,可以用并查集来解决.需要用到两个数组来判断从i到j以及j到i的是否属于同一个并查集. 下面的是AC的代码: #include <iostream> #include <cstdio> using namespace std; int par[2][10005]; int n, m; int finds(int x, int i) { if(x == par[i][x]) return x; else return par[i][x] = finds(par[i][x], i)

杭电3172--Virtual Friends(并查集+map)

Virtual Friends Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6844    Accepted Submission(s): 1952 Problem Description These days, you can do all sorts of things online. For example, you can u

油菜花王国——杭电校赛(并查集)

油菜花王国 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1609    Accepted Submission(s): 411 Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. 出城不

More is better(杭电1856)(并查集)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 15434    Accepted Submission(s): 5667 Problem Description Mr Wang wants some boys to help him with a project. Because the projec

杭电 5326 Work (并查集求子结点为k的结点数)

Description It’s an interesting experience to move from ICPC to work, end my college life and start a brand new journey in company. As is known to all, every stuff in a company has a title, everyone except the boss has a direct leader, and all the re

杭电 4707 pet(并查集求元素大于k的集合)

Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three da

杭电 oj3047~带权并查集

Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2947    Accepted Submission(s): 1132 Problem Description In 12th Zhejiang College Students Games 2007, there was a new stadium built

HDU 4496 D-City (并查集)

题意:给你n个点m条边,问删除前i条边后有多少个连通分块. 思路:从后往前操作,从后往前添加i条边等于添加完m条边后删掉前m-i条边,可知刚开始没有边,所以sum[m]=n; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 100010