zoukankan      html  css  js  c++  java
  • hdu--4496--并查集

    好久没写并查集了 还好这题蛮水的~

    从前到后的删除 那我就先将所有的边给记录下来 然后从后往前的添加边 就是同一个意思了.离线处理吧相当于 一次性全部输出

    有点逆序的味道~~

    -----但我闻到了风中 苦涩的味道 哈哈~~

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 int n;
     6 const int size = 10100;
     7 int father[size] , ans[size*10] , from[size*10] , to[size*10];
     8 
     9 int find( int x )
    10 {
    11     return father[x] == -1 ? x : father[x] = find( father[x] );
    12 }
    13 
    14 int main()
    15 {
    16     int m , sum , x , y;
    17     while( ~scanf("%d%d",&n,&m) )
    18     {
    19         sum = n;
    20         memset( father , -1 , sizeof(father) );
    21         for( int i = 0 ; i<m ; i++ )
    22         {
    23             scanf("%d%d",&from[i],&to[i]);
    24         }
    25         for( int i =  m-1 ; i>=0 ; i-- )
    26         {
    27             ans[i] = sum;
    28             x = find( from[i] );
    29             y = find( to[i] );
    30             if( x!=y )
    31             {
    32                 -- sum;
    33                 father[x] = y;
    34             }    
    35         }
    36         for( int i = 0 ; i<m ; i++ )
    37         {
    38             printf("%d
    ",ans[i]);
    39         }
    40     }
    41     return 0;
    42 }
    View Code

    today:

      在拥挤的人群中

      感到光荣

    just follow your heart
  • 相关阅读:
    oracle查看所有角色
    jQuery 异步提交表单实例解析
    oracle查看用户系统权限
    js中日期操作大全
    oracle 查询用户下所有表
    JS语法字典
    JS定时器例子讲解
    开源软件
    rpm的使用
    lvs+keepalived和haproxy+heartbeat区别
  • 原文地址:https://www.cnblogs.com/radical/p/4085040.html
Copyright © 2011-2022 走看看