HDU-4496-D-市(互いに素セットベース)

D-市

問題の説明

ルクサーは本当に悪い奴です。彼は、彼が出会ったすべてのものを破壊します。
ある日ルクサーは、D-市に行ってきました。D-cityがN D点及びM D-線を有します。各D-ラインは正確に2つのD点を結びます。ルクサーは、すべてのD-ラインを破壊します。D-市の市長は、D-街の多くの接続ブロックはルクサーは、入力の最初のK Dラインを破壊した後に残る方法を知りたいです。
それらは、直接または間接的に相互に接続した場合にのみ場合、2つの点が同一の接続ブロックです。

入力

入力の最初の行は2つの整数を含んでいるNとM.は
その後D-線である2スペースで区切られた整数uおよびvを含むM行それぞれを以下。
制約:
0 <N <= 10000
0 <M <= 100000
0 <= U、V <N.

出力

出力M線、i番目の行は、入力に最初のiのエッジを削除した後の答えです。

サンプル入力

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

サンプル出力

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. 

問題解決のアイデア:

Nウェルノードの1つに理解タイトルは、図エッジをmです。
私はエッジの前に通信要求の数は、削除する図全体をブロックします。
通信要求ブロックが自然にばらばらセットを発生し、そして私の前にエッジの要件ケースを削除することができます。
考慮が逆であってもよい、通信ブロックの数は、ビンのI記事、後方して出力を考慮した後に添加されます。

ACコード:

#include <bits/stdc++.h>
#include <utility>
#include <string>
#define int long long
using namespace std;
const int mod = 1e9+7;
const int N = 2e5+10;
int a[N];
int p[N];

struct node{
    int x,y;
}nodes[N];

int _find(int x)
{
    return p[x] == x ? x : p[x] =  _find(p[x]);
}

signed main()
{
    int n,m;
    while(~scanf("%lld%lld",&n,&m))
    {
        for(int i = 0 ; i < m ; i ++)
            scanf("%lld%lld",&nodes[i].x,&nodes[i].y);
        for(int i = 0 ; i < n ; i ++)
            p[i] = i;
        int ans = n;
        for(int i = m-1 ; i >= 0 ; i --)
        {
            int pa = _find(nodes[i].x);
            int pb = _find(nodes[i].y);
            if( pa != pb )
            {
                p[pa]  = pb;
                a[i] = ans--;
            }
            else
                a[i] = ans;
        }
        for(int i = 0 ; i < m ; i ++)
            printf("%lld\n",a[i]);
    }
    return 0;
}

公開された104元の記事 ウォン称賛7 ビュー4071

おすすめ

転載: blog.csdn.net/qq_43461168/article/details/104127418