BZOJ 2208: [Jsoi2010]连通数( DFS )
2015-05-24 10:25
302 查看
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 MBSubmit: 1499 Solved: 611
[Submit][Status][Discuss]
Description
Input
输入数据第一行是图顶点的数量,一个正整数N。 接下来N行,每行N个字符。第i行第j列的1表示顶点i到j有边,0则表示无边。Output
输出一行一个整数,表示该图的连通数。Sample Input
3010
001
100
Sample Output
9HINT
对于100%的数据,N不超过2000。Source
第一轮相关文章推荐
- [BZOJ2208][Jsoi2010]连通数(dfs||tarjan+拓扑序+dp)
- 【bzoj 2208】[Jsoi2010]连通数(dfs||Tarjan算法+拓扑序+dp)
- bzoj 2208: [Jsoi2010]连通数 (dfs|tarjan+bitset+拓扑序)
- 【BZOJ2208】[Jsoi2010]连通数【BFS/DFS】【SCC】
- 【BZOJ2208】[Jsoi2010]连通数 DFS
- Dfs【p4306(bzoj 2208)】 [JSOI2010]连通数
- 【bzoj2208】[Jsoi2010]连通数
- BZOJ 2208 JSOI2010 连通数 Tarjan+拓扑排序
- BZOJ 2208: [Jsoi2010]连通数
- BZOJ2208 [JSOI2010] 连通数
- 【BZOJ】2208 [Jsoi2010]连通数
- [bzoj2208][Jsoi2010]连通数_bitset_传递闭包floyd
- BZOJ 2208 [Jsoi2010]连通数 - Tarjan_SCC/Floyd+bitset优化
- BZOJ 2208: [Jsoi2010]连通数
- BZOJ_2208_[Jsoi2010]连通数_强连通分量+拓扑排序+手写bitset
- [bzoj2208][Jsoi2010]连通数
- bzoj2208 [Jsoi2010]连通数(scc+bitset)
- [tarjan+bitset]BZOJ 2208——[Jsoi2010]连通数
- [BZOJ2208][Jsoi2010]连通数(tarjan+topdp)
- 暴力【bzoj2208】: [Jsoi2010]连通数