还是挺简单的tarjan。

判断时可能重复,直接bitset搞定。

首先tarjan缩点,每个scc的内部肯定能互相到达,更一下,而且一个scc里的各个点的贡献肯定是一样的,topsort,更新答案就可以了,用bitset的count成上scc大小即可。

据说数据很水,大约O(n^3)可过,但是我看了看,没缩点,直接dfs,细节比较多,略烦,上述算法比较长,但是脑量比较小。还是比较好的,tarjan和topsort打得熟就15min搞定。

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<map>
using namespace std;
struct EDGE{
int ed,nex;
}edge[],edgec[];
int num,numc,ans,first[],firstc[];
int n,ord,sccnum,top;
int sta[],dfn[],low[],bl[],du[];
char ch[];
bool ins[];
vector<int>scc[];
bitset<>s[];
void add(int st,int ed){
// cout<<"st="<<st<<" ed="<<ed<<endl;
edge[++num].ed=ed;
edge[num].nex=first[st];
first[st]=num;
}
void addc(int st,int ed){
// cout<<"Stc="<<st<<" edc="<<ed<<endl;
edgec[++numc].ed=ed;
edgec[numc].nex=firstc[st];
firstc[st]=numc;
}
void tarjan(int x){
dfn[x]=low[x]=++ord;
sta[++top]=x;ins[x]=;
for(int i=first[x];i;i=edge[i].nex){
int y=edge[i].ed;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}else if(ins[y])
low[x]=min(low[x],dfn[y]);
}if(dfn[x]==low[x]){
sccnum++;int p;
do{
p=sta[top--];ins[p]=;
bl[p]=sccnum;scc[sccnum].push_back(p);
}while(p!=x);
}
}
void topsort(){
queue<int>q;
for(int i=;i<=sccnum;i++)
if(du[i]==) q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
// cout<<x<<" ";
for(int i=firstc[x];i;i=edgec[i].nex){
int y=edgec[i].ed;
du[y]--;
s[y]|=s[x];
if(du[y]==)
q.push(y);
}
}
}
int main(){
// freopen("a,in","r",stdin);
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",ch+);
for(int j=;j<=n;j++)
if(ch[j]-'')
add(i,j);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
for(int i=;i<=n;i++)
for(int j=first[i];j;j=edge[j].nex){
int y=edge[j].ed;
if(bl[i]==bl[y]) continue;
addc(bl[i],bl[y]);
du[bl[y]]++;
}
for(int i=;i<=sccnum;i++){
for(int j=;j<scc[i].size();j++){
int y=scc[i][j];
s[i][y]=;
}
}
topsort();
for(int i=;i<=sccnum;i++)
ans+=s[i].count()*scc[i].size();
printf("%d\n",ans);
return ;
}

最新文章

  1. chose.jquery 联动
  2. Shader实例:NGUI图集中的UISprite正确使用Shader的方法
  3. yum
  4. 初学python之安装Jupyter notebook
  5. 经典网页设计:20个华丽的 iPhone 应用程序演示网站
  6. php 过滤英文标点符号 过滤中文标点符号
  7. hdu1247 Hat’s Words
  8. wordpress源码解析-目录结构-文件调用关系(1)
  9. C语言中的指针和内存泄漏
  10. 每一个程序员需要了解的10个Linux命令
  11. C# 使用ffmpeg.exe进行音频转换完整demo
  12. 在js脚本里计算多个小数的加法问题
  13. power desinger 学习笔记&lt;八&gt;
  14. iOS开发系列之远程控制事件
  15. Google推出iOS功能性UI测试框架EarlGrey
  16. 第52周四ApplicationContext
  17. LNMP : 502 Bad Gateway 解决小记,真正的原因
  18. Unity For Android Cardboard App ( 1 ):基础入门
  19. 【推荐】开源项目minapp-重新定义微信小程序的开发
  20. Java学习资源整理(超级全面)

热门文章

  1. [转载]Flex的文件规则
  2. Vue中的key到底有什么用?
  3. [CSS] w3c 盒模型 和 IE 盒模型
  4. element-ui 表单自定义日期输入校验
  5. jQuery EasyUI中DataGird动态生成列的方法
  6. List&lt;int&gt;转化为逗号链接的字符串
  7. vue+elementui搭建后台管理界面
  8. jQuery获取表单全部数据
  9. 【XDOJ】坑爹的杜神
  10. openGL中的gl,glu,glut