zoukankan      html  css  js  c++  java
  • 1388:家谱(gen)

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1388

    方法一:普通模拟,结构体写法

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 struct node
     6 {
     7     string name;
     8     int father;
     9 };
    10 node a[50009];
    11 int findx(int i)
    12 {
    13     if(a[i].father!=i)a[i].father=findx(a[i].father);
    14     return a[i].father;
    15 }
    16 void unionn(int r,int t)
    17 {
    18     int q=findx(t);
    19     if(r!=q)a[q].father=r;
    20 }
    21 int main()
    22 {
    23     char c;
    24     int tot=0;
    25     int father;
    26     string s;
    27     do
    28     {
    29         cin>>c;
    30         if(c=='$')break;
    31         cin>>s;
    32         int r=-1;
    33         for(int i=1;i<=tot;i++)
    34             if(a[i].name==s)
    35             {
    36                 r=i;
    37                 break;\找到就可以跳出;
    38             }    
    39         if(r==-1)
    40         {
    41             r=++tot;
    42             a[r].father=r;
    43             a[r].name=s;
    44         }    
    45         if(c=='#')
    46         {
    47             father=a[r].father;
    48             continue;
    49         }    
    50         if(c=='+')
    51         {
    52             unionn(father,r);
    53             continue;
    54             }    
    55         if(c=='?')
    56             cout<<s<<" "<<a[findx(r)].name<<endl;
    57     }while(1);
    58     return 0;
    59 }

    方法二:使用STL  MAP

     1     #include<iostream>
     2     #include<cstdio>
     3     #include<algorithm>
     4     #include<string>
     5     #include<map>
     6      
     7     using namespace std;
     8     map<string, string>p;
     9     string findth(string x)
    10     {
    11         if (p[x] == x) return x;
    12         return p[x] = findth(p[x]);
    13     }
    14      
    15     void unionn(string x, string y)
    16     {
    17         string xx = findth(x);
    18         //string yy = findth(y);
    19         if (xx != y) p[y] = xx;
    20     }
    21     int main()
    22     {
    23         string s;
    24         string t;
    25         string t1;
    26         string t2;
    27         while (cin >> s, s != "$") {
    28             if (s[0] == '#') {
    29                 t = s.substr(1);
    30                 if (p[t] == "") p[t] = t;
    31             }
    32             else if (s[0] == '+') {
    33                 t1 = s.substr(1);
    34                 unionn(t, t1);
    35             }
    36             else if (s[0] == '?') {
    37                 t2 = s.substr(1);
    38                 findth(t2);//找到祖先
    39                 cout << t2 << " " << p[t2] << endl;
    40             }
    41         }
    42         return 0;
    43     }
  • 相关阅读:
    Understanding about Baire Category Theorem
    Isometric embedding of metric space
    Convergence theorems for measurable functions
    Mindmap for "Principles of boundary element methods"
    Various formulations of Maxwell equations
    Existence and uniqueness theorems for variational problems
    Kernels and image sets for an operator and its dual
    [loj6498]农民
    [luogu3781]切树游戏
    [atAGC051B]Three Coins
  • 原文地址:https://www.cnblogs.com/tflsnoi/p/14250572.html
Copyright © 2011-2022 走看看