HDU4496 D-City【并查集】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=4496

题目大意:

给出一张图,按照给定的边的顺序逐个删除。问每删除一条边后图的连通块数是多少。

思路:

逆向并查集求联通块数。假设一开始的时候所有点都不连通。从给定边逆着的顺序,即从最后

一条边开始添加。如果新添加的边连通了两个连通分量,则连通块数就减一,否则不改变。将

每次加边后的连通块数存起来。最后输出出来。

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

struct EdgeNode
{
    int u;
    int v;
}Edges[1000010];
int Father[100010],Ans[100010];

int Find(int x)
{
    if(Father[x] != x)
        Father[x] = Find(Father[x]);
    return Father[x];
}

int main()
{
    int N,M;
    while(~scanf("%d%d",&N,&M))
    {
        for(int i = 0; i < M; ++i)
            scanf("%d%d",&Edges[i].u,&Edges[i].v);
        for(int i = 0; i < N; ++i)
            Father[i] = i;
        int j = 0;
        int Num = N;
        for(int i = M-1; i >= 0; --i)
        {
            int u = Edges[i].u;
            int v = Edges[i].v;
            u = Find(u);
            v = Find(v);
            if(u != v)
            {
                Father[u] = v;
                Ans[j++] = Num;
                Num--;
            }
            else
                Ans[j++] = Num;
        }
        for(int i = j-1; i >= 0; --i)
            printf("%d\n",Ans[i]);
    }

    return 0;
}
时间: 2024-08-08 01:30:12

HDU4496 D-City【并查集】的相关文章

hdu4496 D-City(反向并查集)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496 D-City 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

HDU4496 D-City并查集逆向推理

D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 3035    Accepted Submission(s): 1074 Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D

L2-013. 红色警报 (并查集)

战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报. 输入格式: 输入在第一行给出两个整数N(0 < N <=500)和M(<=5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数.随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔.在城市信息之后给出被攻

hdu-2874 Connections between cities(lca+tarjan+并查集)

题目链接: Connections between cities Time Limit: 10000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description After World War X, a lot of cities have been seriously damaged, and we need to rebuild those cities. However, s

Travel(HDU 5441 2015长春区域赛 带权并查集)

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2404    Accepted Submission(s): 842 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is tr

杭电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

UVA - 10596 - Morning Walk (欧拉回路!并查集判断回路)

UVA - 10596 Morning Walk Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem H Morning Walk Time Limit 3 Seconds Kamal is a Motashota guy. He has got a new job in Chittagong . So, he has moved to Ch

POJ1703--Find them, Catch them(种类并查集)

Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, t

hdu 5441 并查集

Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 2331    Accepted Submission(s): 804 Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t