BZOJ 2208: [Jsoi2010]连通数( DFS )

n只有2000,直接DFS就可以过了...

--------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cctype>
 
#define rep( i, n ) for( int i = 0; i < n; ++i )
#define clr( x, c ) memset( x, c, sizeof( x ) )
 
using namespace std;
 
const int maxn = 2000 + 5;
 
struct edge {
int to;
edge* next;
};
 
edge* pt;
edge* head[ maxn ];
edge EDGE[ maxn * maxn ];
 
void init() {
pt = EDGE;
clr( head, 0 );
}
 
inline void add_edge( int u, int v ) {
pt -> to = v;
pt -> next = head[ u ];
head[ u ] = pt++;
}
 
bool vis[ maxn ];
int ans = 0;
 
void dfs( int x ) {
ans++;
vis[ x ] = true;
for( edge* e = head[ x ]; e; e = e -> next ) if( ! vis[ e -> to ] )
   dfs( e -> to );
}
 
int main() {
init();
int n;
cin >> n;
rep( i, n )
   rep( j, n ) {
    char c = getchar();
    while( ! isdigit( c ) ) c = getchar();
    if( c == '1' ) add_edge( i, j );
   }
rep( i, n ) {
clr( vis, 0 );
dfs( i );
}
cout << ans << "\n";
return 0;
}

  

--------------------------------------------------------------------------

2208: [Jsoi2010]连通数

Time Limit: 20 Sec  Memory Limit: 512 MB
Submit: 1499  Solved: 611
[Submit][Status][Discuss]

Description

Input

输入数据第一行是图顶点的数量,一个正整数N。 接下来N行,每行N个字符。第i行第j列的1表示顶点i到j有边,0则表示无边。

Output

输出一行一个整数,表示该图的连通数。

Sample Input

3
010
001
100

Sample Output

9

HINT

对于100%的数据,N不超过2000。

Source

 

posted @ 2015-05-24 10:25  JSZX11556  阅读(182)  评论(0编辑  收藏  举报