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. 图解SQL的inner join(join)、left join、right join、full outer join、union、union all的区别

    对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...

  2. iOS - 网络语线程(OC)

    1. 检测网络状态 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the vi ...

  3. 时间类型(DataTime)赋空值

    暂时只发现这一个方法 如果直接Datetime time=DBNull.Value;会报null与DataTime没有隐式转换 SqlCommand cmd = SqlCommand(conn); / ...

  4. 20150224—ASP.NET基础

    一.如何使用VS2012创建ASP.NET的项目. 文件-新建-网站 出现以下对话框,选择ASP.NET的空网站(注意,左侧使用的模板是Visual C#) 选择好存放位置,名字之后 点击确定.这样就 ...

  5. 10款免费CSS编辑器应对于Linux和Ubuntu

    您是否在使用Linux和Ubuntu的,不知道在哪里可以找到一些优秀且免费的CSS编辑器用于Linux和Ubuntu的?如果你的答案是肯定的,然后停止幻想,开始浏览这个帖子里,我们展示了前10名,并免 ...

  6. 洛谷 P1195 口袋的天空

    题目背景 小杉坐在教室里,透过口袋一样的窗户看口袋一样的天空. 有很多云飘在那里,看起来很漂亮,小杉想摘下那样美的几朵云,做成棉花糖. 题目描述 给你云朵的个数N,再给你M个关系,表示哪些云朵可以连在 ...

  7. Oracle 创建用户授权

    权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...

  8. Fedora 20 创建桌面快捷方式

    创建desktop文件 sudo touch /usr/share/applications/sublime.desktop 添加内容 [Desktop Entry] Encoding=UTF-8 N ...

  9. 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    今天给Centos通过rpm -Uvh装了个epel的扩展后,执行yum就开始报错: Error: Cannot retrieve metalink for repository: epel. Ple ...

  10. .Net中的Socket通讯

    .NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...