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。

看到这题然后马上打了一个tarjan

然后对每一个强连通分量dfs,A了之后感觉有点奇怪,这个复杂度是多少来着,我好像算不出来,果断百度题解

然后大囧。。。。。。怎么好像正解是tarjan+拓扑排序+状态压缩,只搜到了一个和我一样的做法

然后我想到这样做其实可以随随便便卡掉,还是n三方,于是又打了一遍正解,加个拓扑和状态压缩

 const
maxn=;
var
first,c,sum,dfn,low,z:array[..maxn*]of longint;
next,last:array[..maxn*maxn*]of longint;
flag:array[..maxn*]of boolean;
f:array[..maxn,..maxn]of boolean;
n,cnt,tot,ans,time,s:longint; procedure insert(x,y:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
end; procedure dfs(x:longint);
var
i:longint;
begin
inc(time);
dfn[x]:=time;
low[x]:=time;
inc(s);
z[s]:=x;
flag[x]:=true;
i:=first[x];
while i<> do
begin
if dfn[last[i]]= then
begin
dfs(last[i]);
if low[last[i]]<low[x] then low[x]:=low[last[i]];
end
else
if flag[last[i]] and (low[last[i]]<low[x]) then low[x]:=low[last[i]];
i:=next[i];
end;
if low[x]=dfn[x] then
begin
inc(cnt);
while z[s+]<>x do
begin
inc(sum[cnt]);
c[z[s]]:=cnt;
flag[z[s]]:=false;
dec(s);
end;
end;
end; procedure init;
var
i,j:longint;
cc:char;
begin
readln(n);
for i:= to n do
begin
for j:= to n do
begin
read(cc);
if cc='' then insert(i,j);
end;
readln;
end;
for i:= to n do
if dfn[i]= then dfs(i);
for i:= to n do
begin
j:=first[i];
while j<> do
begin
if f[c[i],c[last[j]]]=false then
begin
insert(n+c[i],n+c[last[j]]);
f[c[i],c[last[j]]]:=true;
end;
j:=next[j];
end;
end;
end; function dfs2(x:longint):longint;
var
i:longint;
begin
dfs2:=sum[x-n];
flag[x]:=true;
i:=first[x];
while i<> do
begin
if flag[last[i]]=false then inc(dfs2,dfs2(last[i]));
i:=next[i];
end;
end; procedure work;
var
i,j:longint;
begin
for i:= to cnt do
begin
for j:= to cnt do
flag[j+n]:=false;
inc(ans,sum[i]*dfs2(i+n));
end;
writeln(ans);
end; begin
init;
work;
end.
 const
maxn=;
var
first,c,sum,dfn,low,z,d:array[..maxn*]of longint;
next,last:array[..maxn*maxn*]of longint;
flag:array[..maxn*]of boolean;
ff:array[..maxn,..maxn]of boolean;
n,cnt,tot,ans,time,s:longint; procedure insert(x,y:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
end; procedure dfs(x:longint);
var
i:longint;
begin
inc(time);
dfn[x]:=time;
low[x]:=time;
inc(s);
z[s]:=x;
flag[x]:=true;
i:=first[x];
while i<> do
begin
if dfn[last[i]]= then
begin
dfs(last[i]);
if low[last[i]]<low[x] then low[x]:=low[last[i]];
end
else
if flag[last[i]] and (low[last[i]]<low[x]) then low[x]:=low[last[i]];
i:=next[i];
end;
if low[x]=dfn[x] then
begin
inc(cnt);
while z[s+]<>x do
begin
inc(sum[cnt]);
c[z[s]]:=cnt;
flag[z[s]]:=false;
dec(s);
end;
end;
end; procedure init;
var
i,j:longint;
cc:char;
begin
readln(n);
for i:= to n do
begin
for j:= to n do
begin
read(cc);
if cc='' then insert(i,j);
end;
readln;
end;
for i:= to n do
if dfn[i]= then dfs(i);
for i:= to n do
begin
j:=first[i];
while j<> do
begin
if (ff[c[i],c[last[j]]]=false) and (c[i]<>c[last[j]]) then
begin
insert(n+c[i],n+c[last[j]]);
inc(d[c[last[j]]]);
ff[c[i],c[last[j]]]:=true;
end;
j:=next[j];
end;
end;
end; var
q:array[..maxn]of longint;
f:array[..maxn,..]of longint;
l,r:longint; procedure work;
var
i,j,k,tmp:longint;
begin
l:=;
r:=;
for i:= to cnt do
if d[i]= then
begin
inc(r);
q[r]:=i;
end;
while l<=r do
begin
j:=first[q[l]+n];
while j<> do
begin
dec(d[last[j]-n]);
if d[last[j]-n]= then
begin
inc(r);
q[r]:=last[j]-n;
end;
j:=next[j];
end;
inc(l);
end;
for i:=r downto do
begin
f[q[i],q[i] div ]:=<<(q[i]mod );
j:=first[q[i]+n];
while j<> do
begin
for k:= to cnt div do
f[q[i],k]:=f[q[i],k]or f[last[j]-n,k];
j:=next[j];
end;
end;
for i:= to cnt do
begin
tmp:=;
for j:= to cnt do
if f[i,j div ] and (<<(j mod ))> then inc(tmp,sum[j]);
inc(ans,tmp*sum[i]);
end;
writeln(ans);
end; begin
init;
work;
end.

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

  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]连通数( DFS )

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

  3. bzoj 2208 [Jsoi2010]连通数

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

  4. 2208: [Jsoi2010]连通数

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

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

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

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

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

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

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

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

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

  9. bzoj2208:[Jsoi2010]连通数

    http://blog.csdn.net/u013598409/article/details/47037499 里面似乎有生成数据的... //我本来的想法是tarjan缩点之后然后将图遍历一遍就可 ...

随机推荐

  1. 我的博客模板(线框图wireframe)

    不久前看到一篇介绍定制网页浏览的方法,当时就想着,我把我的博客页也修改下,在手机浏览的时候,也能漂亮的显示出来.以后写的文章的话,也可以分享的微信朋友圈里面和朋友分享. 具体步骤参考:http://w ...

  2. Ruby on Raisl应用(一):在Rails上配置Mongoid+Mongodb

    一. 概述 最近考虑用ruby on rails 搭建一套Blog系统,前端考虑用Bootstrap,数据库用Mongodb.由于之前没有相关应用经验.先记录下整个项目过程. 现有资源: Mac 笔记 ...

  3. UI2_UIGesture

    // // ViewController.h // UI2_UIGesture // // Created by zhangxueming on 15/7/9. // Copyright (c) 20 ...

  4. UI5_HomeWorkCompanyViewController

    // // ItemCompany.h // UI5_HomeWork // // Created by zhangxueming on 15/7/3. // Copyright (c) 2015年 ...

  5. node笔记——gulp修改静态文件的名字

    cmd小技巧: 1.换到下级或同等级目录 D: 2.换到上级目录 cd.. node 包管理器小技巧[以gulp为例] npm install --save-dev gulp gulp-concat ...

  6. startup.bat闪退---tomcat环境变量配置中遇到的问题

    常用的方法: 1.在已解压的tomcat的bin文件夹下找到startup.bat,右击->编辑.在文件头加入下面两行: SET JAVA_HOME=D:\Java\jdk1.6.0_10   ...

  7. Android实现Http协议案例

    在Android开发中,使用Http协议实现网络之间的通信是随处可见的,使用http方式主要采用2中请求方式即get和post两种方式. 一.使用get方式: HttpGet httpGet = ne ...

  8. COM包装(COM Wrappers)

    为了实现传统的COM程序与.NET程序之间的相互调用,.NET提供了两个包装类:运行时可调用包装(runtime callable wrapper,RCW)和COM可调用包装(COM callable ...

  9. English Learning

    EnglishPod 百度云盘:http://pan.baidu.com/s/1eQUJquA

  10. 《搭建更新DNS集群服务》RHEL6

    DNS服务器的更新: 一听就知道不止一台的DNS服务器,要是一台也用不着更新对吧?一般都是DNS集群. 一台DNS更新了,添加一条数据,下面的都要跟着它变. 主DNS服务器的配置 首先先配置DNS服务 ...