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

BZOJ 2208: [Jsoi2010]连通数( DFS )的更多相关文章

  1. BZOJ 2208: [Jsoi2010]连通数 tarjan bitset

    2208: [Jsoi2010]连通数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  2. bzoj 2208 [Jsoi2010]连通数

    2208: [Jsoi2010]连通数 Time Limit: 20 Sec  Memory Limit: 512 MB Description Input 输入数据第一行是图顶点的数量,一个正整数N ...

  3. BZOJ.2208.[JSOI2010]连通数(bitset Tarjan 拓扑)

    题目链接 先缩点,对于scc之间贡献即为szscc[i]*szscc[j] 用f[i][j]表示scci是否能到sccj 拓扑排序,每次把now的f或上to的f 用bitset优化 //63888kb ...

  4. bzoj 2208: [Jsoi2010]连通数【tarjan+拓扑+dp】

    我总觉得枚举点bfs也行-- tarjan缩点,记一下每个scc的size,bitset压一下scc里的点,然后按拓扑倒序向上合并到达状态,然后加ans的时候记得乘size #include<i ...

  5. BZOJ 2208 JSOI2010 连通数 Tarjan+拓扑排序

    题目大意:给定一个n个点的有向图,求有多少点对(x,y),使x沿边可到达y 设f[i][j]为从i到j是否可达 首先强联通分量中的随意两个点均可达 于是我们利用Tarjan缩点 缩点之后是一个拓扑图. ...

  6. 2208: [Jsoi2010]连通数

    2208: [Jsoi2010]连通数 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1371  Solved: 557[Submit][Status ...

  7. 【BZOJ2208】[Jsoi2010]连通数 DFS

    [BZOJ2208][Jsoi2010]连通数 Description Input 输入数据第一行是图顶点的数量,一个正整数N. 接下来N行,每行N个字符.第i行第j列的1表示顶点i到j有边,0则表示 ...

  8. 2208: [Jsoi2010]连通数 - BZOJ

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

  9. 【BZOJ】2208 [Jsoi2010]连通数

    [题意]给定n个点的有向图,求可达点对数(互相可达算两对,含自身).n<=2000. [算法]强连通分量(tarjan)+拓扑排序+状态压缩(bitset) [题解]这题可以说非常经典了. 1. ...

随机推荐

  1. linux下各种代理的设置

    http://los-vmm.sc.intel.com/wiki/OpenStack_New_Hire_Guide#Apply_JIRA_account Set up your proxy. The ...

  2. iOS实现文件上传功能模块

    iOS实现文件上传功能,首先要知道的是,上传到服务器的数据格式,一般采用HTTP文件上传协议.如下图 如图所示,只要设置好了HTTP的协议格式,就可以实现文件上传功能. 代码如下: //图片上传模块 ...

  3. rgbdslam_v2安装并使用

    rgbdslam_v2安装并使用 此文档为原创,转载请注明来自CSDN Jasmine_shine的专栏 网址:http://blog.csdn.net/jasmine_shine/article/d ...

  4. c语言函数---M

    书画小说软件 制作更满意的读.更舒心的写.更轻松的公布 最全古典小说网 由本软件公布所得 main()主函数 每一C 程序都必须有一main()函数, 能够依据自己的爱好把它放在程序的某 个地方.有些 ...

  5. Selector、shape详解,注意这两种图像资源都以XML方式存放在drawable不带分辨率的文件夹中

    Selector.shape详解(一) Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> ...

  6. poj2388 高速排序 模板题

    /** \brief poj2388 * * \param date 2014/8/5 * \param state AC * \return memory time * qsort 784K 110 ...

  7. VisualStudio中的代码段

    VS很强大,在这里就不过多说了,在平时码代码时应用代码段会提高我们的编写速度. 举个例子: 比如输入Console.WriteLine (); 传统方法就是一个字母一个字母的输入进去. 如果大家掌握了 ...

  8. WP8.1开发系列之隐藏顶部状态栏

    StatusBar statusbar = StatusBar.GetForCurrentView(); await statusbar.HideAsync(); 只能在后台代码中实现,前台xaml不 ...

  9. Spring学习之Jar包功能介绍(转)

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  10. javascript小练习—点击将DIV变成红色(通过for循环遍历)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...