zoukankan      html  css  js  c++  java
  • BNUOJ 33895 D-City

    D-City

    Time Limit: 1000ms
    Memory Limit: 65535KB
    This problem will be judged on HDU. Original ID: 4496
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:并查集,逆向求解。。。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define INF 0x3f3f3f3f
    15 using namespace std;
    16 const int maxn = 10010;
    17 int uf[maxn],n,m;
    18 int ans[maxn*10];
    19 int a[maxn*10],b[maxn*10];
    20 int Find(int x){
    21     if(x != uf[x])
    22         uf[x] = Find(uf[x]);
    23     return uf[x];
    24 }
    25 int main(){
    26     int i,j,k;
    27     while(~scanf("%d %d",&n,&m)){
    28         for(i = 0; i <= n; i++)
    29             uf[i] = i;
    30         for(i = 1; i <= m; i++){
    31             scanf("%d %d",a+i,b+i);
    32         }
    33         ans[m] = n;
    34         for(i = m; i; i--){
    35             int tx= Find(a[i]);
    36             int ty = Find(b[i]);
    37             uf[tx] = ty;
    38             if(tx != ty){
    39                 ans[i-1] = ans[i]-1;
    40             }else ans[i-1] = ans[i];
    41         }
    42         for(i = 1; i <= m; i++){
    43             printf("%d
    ",ans[i]);
    44         }
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    C# 防止同一个账号多次登录(cache方法)
    前端--关于Canvas
    前端--关于客户端javascript
    前端--关于javascript函数
    前端--关于javascript对象
    前端--关于javascript基础
    前端--关于背景、浮动和定位
    前端--关于CSS盒模型
    前端--关于CSS文本
    前端--关于css选择器
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3925418.html
Copyright © 2011-2022 走看看