您的位置:首页 > 其它

POJ 1611

2015-06-10 16:35 267 查看
#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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: