BZOJ2208: [Jsoi2010]連通數

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   for   

2208: [Jsoi2010]連通數Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 1235  Solved: 488
[Submit][Status]
Description

Input

輸入資料第一行是圖頂點的數量,一個正整數N。 接下來N行,每行N個字元。第i行第j列的1表示頂點i到j有邊,0則表示無邊。

Output

輸出一行一個整數,表示該圖的連通數。

Sample Input3
010
001
100
Sample Output9HINT

 

對於100%的資料,N不超過2000。

 

Source

第一輪

題解:看著正解好麻煩,敲了個暴力,結果就過了!過了。。。我還想如果不過的話,就先tarjan一下,在dfs。。。從每個點開始dfs出它能到達的的點ans++代碼:
 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #define inf 100000000012 #define maxn 2000+10013 #define maxm 4000000+100014 #define eps 1e-1015 #define ll long long16 using namespace std;17 int n,ans,tot,head[maxn],next[maxm],go[maxm];18 bool v[maxn];19 inline int read()20 {21     int x=0,f=1;char ch=getchar();22     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}23     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}24     return x*f;25 }26 inline void dfs(int x)27 {28     v[x]=1;ans++;29     for(int i=head[x];i;i=next[i])if(!v[go[i]])dfs(go[i]);30 }31 int main()32 {33     freopen("input.txt","r",stdin);34     freopen("output.txt","w",stdout);35     n=read();char ch;36     for(int i=1;i<=n;i++)37     {38      for(int j=1;j<=n;j++)39      {40          ch= ;while(ch<0||ch>1)ch=getchar();41          if(ch==1)42          {43             go[++tot]=j;next[tot]=head[i];head[i]=tot;44          }45      }46     }  47     ans=0;48     for(int i=1;i<=n;i++)49     {50         memset(v,0,sizeof(v));51         dfs(i);52     }53     printf("%d\n",ans);54 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.