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. android在桌面弹出一个窗口

    android在桌面弹出一个窗口 遇到了这种需求,要和iPhone一样的效果. 下面是简单实现功能,优化和美化部分,有时间慢慢搞. 方法应该有不少吧,我用的是弹出一个activity,将这个activ ...

  2. Centos6.8下安装oracle_11gr2版主要过程

    安装前准备 下载oracle版本 地址:http://docs.oracle.com/cd/E21901_01/index.html ,下载2个文件分别是 linux.x64_11gR2_databa ...

  3. openstack 采用conductor的原因

    供参考. Tan0同学给我的解释: 两个原因 一个是为了isolation做准备 因为升级主要就是升DB的schema 如果让compute直接读写DB,那每次升级都得升compute 现在隔离开之后 ...

  4. #include <thread>

    1 detach 脱离当前主线程,自由执行,乱序; 2 join() 等待模式,执行完再执行下一个 3 std::this_thread::get_id() 获取当前线程编号 4 std::threa ...

  5. javascript数组去重算法-----4(另一种写法__2)

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

  6. favicon

    <link rel="icon" href="Images/wangyi.ico" type="text/plain" />

  7. mac pro 设置wifi热点

    系统偏好设置-共享-网络共享(Internet共享)共享来源选择你的网络(非wifi), 以下复选框选wifi,wifi设置里能够设置wifi名字password,注意不能有中文. 设置完,inter ...

  8. 提高你的Java代码质量吧:使用构造函数协助描述枚举项

    一.分析 一般来说,我们经常使用的枚举项只有一个属性,即排序号,其默认值是从0.1.2... ....但是除了排序号外,枚举还有一个(或多个)属性. 二.场景 比如,可以通过枚举构造函数声明业务值,定 ...

  9. oracle tns

    oracle tns 是oracle提供的服务名,设置方法,oracle安装根目录---product----版本选择11.2.0----client1---NETWORK---ADMIN---tns ...

  10. C#Excel导出导入

    using System; using System.Collections.Generic; using NPOI; using NPOI.HPSF; using NPOI.HSSF; using ...