C. Learning Languages 求聯通塊的個數

來源:互聯網
上載者:User

標籤:

                          C. Learning Languages

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue>10 #include <set>11 #include <map>12 #include <list>13 #include <iomanip>14 #include <cstdlib>15 #include <sstream>16 using namespace std;17 typedef long long LL;18 const int INF=0x5fffffff;19 const double EXP=1e-6;20 const int MS=105;21 const int mod=9973;22 23 int lang[MS][MS];24 int con[MS][MS];25 int flag[MS];26 int zero,n,m;27 28 /*29 將n個聯通塊串連起來需要n-1條邊。30 問題轉化為求聯通塊的個數。  注意空集的時候ans==定點個數31 */32 33 int main()34 {35     memset(lang,0,sizeof(lang));36     memset(con,0,sizeof(con));37     memset(con,0,sizeof(con));38     zero=0;39     scanf("%d%d",&n,&m);40     int cnt,t;41     for(int i=1;i<=n;i++)42     {43         scanf("%d",&cnt);44         if(cnt>0)45             zero=1;46         for(int j=0;j<cnt;j++)47         {48             scanf("%d",&t);49             lang[i][t]=1;50         }51     }52     for(int k=1;k<=m;k++)53     {54         for(int i=1;i<=n;i++)55             for(int j=1;j<=n;j++)56                 if(lang[i][k]&&lang[j][k])57                     con[i][j]=1;58     }59     for(int i=1;i<=n;i++)60         con[i][i]=1;61     for(int k=1;k<=n;k++)62         for(int i=1;i<=n;i++)63             for(int j=1;j<=n;j++)64                 if(con[i][k]&&con[k][j])65                     con[i][j]=1;66     int ans=0;67     for(int i=1;i<=n;i++)68     {69         if(!flag[i])70         {71             ans++;72             for(int j=1;j<=n;j++)73                 if(con[i][j])74                     flag[j]=1;75         }76     }77     if(zero)78         printf("%d\n",ans-zero);79     else80         printf("%d\n",ans);81     return 0;82 }

 

C. Learning Languages 求聯通塊的個數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.