zoukankan      html  css  js  c++  java
  • POJ 1611

    #include <stdio.h>
    #include <iostream>
    #define M_500 505
    #define M_30000 30005
    using namespace std;
    
    int pre[M_30000];
    int ans[M_30000];
    int m_1[M_500];
    
    int n;
    int m;
    
    void init();
    int find_pre(int s);
    
    int main()
    {
        //freopen("acm.acm","r",stdin);
        //freopen("out.acm","w",stdout);
        int num_m;
        int i;
        int k;
        int j;
        int code;
        int tem;
        while(cin>>n>>m)
        {
            if(n == 0 && m == 0)
            {
                 break;
            } 
            init();
        //    cout<<"ans[0]"<<ans[0]<<endl;
            for(i = 0; i < m; ++ i)
            {
            //    cout<<"------------"<<i<<endl;
                cin>>k;
                cin>>m_1[i];
        //        cout<<" k "<<k<<endl;
                
                for(j = 1; j < k; ++ j)
                {
                      cin>>code;
                      find_pre(code);
                      find_pre(m_1[i]);
                      if(pre[code] != pre[m_1[i]])
                    {
                           //      if(i == 2)
                          //       {
                          //            cout<<"*************** "<<ans[pre[m_1[i]]]<<endl;
                           //           cout<<ans[pre[code]]<<endl;
                          //            }
                            
                          ans[pre[m_1[i]]] += ans[pre[code]];
                      //    if(i == 2)
                     //     {
                     //           cout<<ans[pre[code]]<<endl;
                      //     cout<<ans[pre[m_1[i]]]<<" @@@@@@@@@@@@@@ "<<pre[m_1[i]]<<endl;
                      //    }
                          pre[pre[code]] = pre[m_1[i]];
                      }
                }
            //    cout<<ans[find_pre(1)]<<endl;
            //    cout<<find_pre(1)<<" ((((((((((((((( "<<endl;
            }
        //    for(i = 0; i < n; ++ i)
        //    {
        //        cout<<ans[i]<<" ";
        //    }
        //    cout<<"///////////////////////////////////////////"<<endl;
        //    cout<<find_pre(0)<<" ((((((((((((((( "<<endl;
            cout<<ans[find_pre(0)] <<endl; 
            
        }
        return 0;
    }
    
    void init()
    {
        int i;
        int j;
        for(i = 0; i < n; ++ i)
        {
            pre[i] = i;
            ans[i] = 1;
        }
    }
    
    int find_pre(int s)
    {
        if(pre[s] == s)
        {
            return pre[s];
        }
        else 
        {
            pre[s] = find_pre(pre[s]);
        }
        return pre[s];
    }

    关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。 

    技术网站地址: vmfor.com

  • 相关阅读:
    我的期末可以加分项
    冲刺
    公司授课管理系统
    挑战赛题终于完成
    Java web 学习
    Java web 学习
    Javaweb 学习
    Base64加密
    选课系统
    Educational Codeforces Round 62题解
  • 原文地址:https://www.cnblogs.com/gavinsp/p/4566474.html
Copyright © 2011-2022 走看看