zoukankan      html  css  js  c++  java
  • hdu2473

    hdu2473
    并查集的删除操作
    建立虚点,删除它就断掉了它在原图中的所有关系,而成为独立节点,而且它只能被删除一次,而且删除之后还能进行操作,采用映射(虚点)的方法,建立虚点并把删除之后的操作挪到虚点上来。啊,初始化确实有问题,有可能多次删除的,所以要按操作的数量来,比如全部删除等等数据,哭了

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<ctime>
     7 #include<set>
     8 #include<map>
     9 #include<stack>
    10 #include<cstring>
    11 #define inf 2147483647
    12 #define ls rt<<1
    13 #define rs rt<<1|1
    14 #define lson ls,nl,mid,l,r
    15 #define rson rs,mid+1,nr,l,r
    16 #define N 2000010
    17 #define For(i,a,b) for(int i=a;i<=b;i++)
    18 #define p(a) putchar(a)
    19 #define g() getchar()
    20 
    21 using namespace std;
    22 int n,m;
    23 char c;
    24 int d[N],id[N];
    25 int x,y;
    26 int cnt;
    27 int tot;
    28 set<int>s;
    29 void in(int &x){
    30     int y=1;
    31     char c=g();x=0;
    32     while(c<'0'||c>'9'){
    33         if(c=='-')y=-1;
    34         c=g();
    35     }
    36     while(c<='9'&&c>='0'){
    37         x=(x<<1)+(x<<3)+c-'0';c=g();
    38     }
    39     x*=y;
    40 }
    41 void o(int x){
    42     if(x<0){
    43         p('-');
    44         x=-x;
    45     }
    46     if(x>9)o(x/10);
    47     p(x%10+'0');
    48 }
    49 
    50 int find(int x){
    51     if(d[x]==x)return x;
    52     return d[x]=find(d[x]);
    53 }
    54 
    55 int main(){
    56     while(cin>>n>>m&&(n+m)){
    57         For(i,0,N)
    58             d[i]=i;
    59         For(i,0,n-1)
    60             id[i]=i;
    61         cnt=n;
    62         while(m--){
    63             cin>>c;
    64             if(c=='M'){
    65                 in(x);in(y);
    66                 int t1=find(id[x]);
    67                 int t2=find(id[y]);
    68                 if(t1!=t2)
    69                     d[t1]=t2;
    70             }
    71             if(c=='S'){
    72                 in(x);
    73                 id[x]=cnt++;
    74             }
    75         }
    76         s.clear();
    77         For(i,0,n-1)
    78             s.insert(find(id[i]));
    79         cout<<"Case #"<<++tot<<": "<<s.size()<<endl;
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    File Size(4.12)
    Ownership of New Files and Directories(4.6)
    Changing User IDs and Group IDs & How saved setuserid works (8.11)
    Windows 下硬盘安装linux 系统
    access Function(4.7)
    File Access Permissions(4.5)
    link, unlink, remove, and rename Functions(4.15)
    文件特殊权限: SUID, SGID, SBIT
    ASP.NET与DreamweaverMx结合
    sqlserver的几个函数要记录
  • 原文地址:https://www.cnblogs.com/war1111/p/10453045.html
Copyright © 2011-2022 走看看