• poj 2524 Ubiquitous Religions


    并查集

     1 #include<iostream>
    2 #include<cstdio>
    3 using namespace std;
    4
    5 int f[50001];//,r[30001];
    6 int m,n;
    7 void Init()
    8 {
    9 for(int i=1;i<=m;++i) f[i]=i;
    10 //memset(r,0,sizeof(r));
    11 }
    12
    13 int father(int k)
    14 {
    15 if(k !=f[k])
    16 return father(f[k]);
    17 return f[k];
    18 }
    19
    20 void unionset(int k,int t)
    21 {
    22 int i=father(k);
    23 int j=father(t);
    24 f[j]=i;
    25 /*if(r[i]<r[j])
    26 f[i]=j;
    27 else
    28 {
    29 f[j]=i;
    30 if(r[i]==r[j])
    31 r[i]++;
    32 }*/
    33
    34 }
    35 void readData()
    36 {
    37 int i,j,k,t,num;
    38 num=1;
    39 while(cin>>m>>n)
    40 {
    41 if(m==0 && n==0) break;
    42 Init();//
    43 for(int i=1;i<=n;++i)
    44 {
    45 scanf("%d%d",&k,&t);
    46 if(father(k)!=father(t))
    47 {
    48 unionset(k,t);
    49 }
    50 }
    51 int ans=0;
    52 for(i=1;i<=m;++i)
    53 {
    54 if(i==f[i]) ans++;
    55 }
    56 printf("Case %d: %d\n",num++,ans);
    57 }
    58 }
    59 int main()
    60 {
    61 //Init();
    62 //freopen("a.txt","r",stdin);
    63 readData();
    64 return 0;
    65 }
  • 相关阅读:
    请求headers处理
    requests模块
    urllib3
    urllib
    百度AI搜索引擎
    CSS层叠样式表--使用
    学习requests_html
    聚合新闻头条
    爬虫工程师的月薪如何?
    Linux日志系统
  • 原文地址:https://www.cnblogs.com/redlight/p/2429863.html
Copyright © 2020-2023  润新知