標籤: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