2208: [Jsoi2010]连通数

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=2208

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。

题意

题解

先缩点,变成一个有向无环图之后,再直接跑dp就好了

可以用bitset做

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200051
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** vector<int> Q[maxn];
char s[];
int dfn[],low[],_clock=;
int sta[],top;
bool in_sta[];
int changed[],scc,num[];
bitset<> have[];
void tarjan(int x)
{
dfn[x]=low[x]=++_clock;
sta[++top]=x;
in_sta[x]=;
for(int i=;i<Q[x].size();i++)
{
int v = Q[x][i];
if(!dfn[v])
tarjan(v),low[x]=min(low[x],low[v]);
else if(in_sta[v])
low[x]=min(low[x],dfn[v]);
}
if(dfn[x]==low[x])
{
int temp;
++scc;
do{
temp = sta[top--];
in_sta[temp]=;
changed[temp]=scc;
++num[scc];
}while(temp!=x);
}
}
int main()
{
int n=read();
for(int i=;i<=n;i++)
{
scanf("%s",s+);
for(int j=;j<=n;j++)
{
if(s[j]=='')
Q[i].push_back(j);
}
}
for(int i=;i<=n;i++)
if(!dfn[i])tarjan(i);
for(int i=;i<=n;i++)
have[changed[i]][i]=;
int ans=;
for(int i=;i<=scc;i++)
{
ans+=num[i]*num[i];
bitset<>temp;
for(int x = ;x<=n;x++)
{
if(changed[x]==i)
{
for(int j=;j<Q[x].size();j++)
{
int v = Q[x][j];
if(changed[v]!=i)
temp|=have[changed[v]];
}
}
}
ans+=num[i]*temp.count();
have[i]|=temp; }
printf("%d\n",ans);
}

最新文章

  1. 利用Python进行数据分析(13) pandas基础: 数据重塑/轴向旋转
  2. vim编辑器,管道,输入输出重定向
  3. Json转换为对象
  4. Qt之窗口动画(下坠、抖动、透明度)
  5. 解决uploadify多图片上传部分图片丢失,且不提示任何错误的问题
  6. php实现在线下载程序安装包功能
  7. Linux网络编程10&mdash;&mdash;使用UDP实现五子棋对战
  8. JS内存泄漏 和Chrome 内存分析工具简介(摘)
  9. JS中 submit提交与Form表单里的onsubmit的调用问题?
  10. vsftp FTP服务器外网访问设置
  11. 【树状数组】BZOJ3132 上帝造题的七分钟
  12. Java高级篇(一)——线程
  13. Unity3D 粒子系统 属性
  14. 第11章 AOF持久化
  15. Django多表查询练习题
  16. Python 关联关系
  17. css 规则中两个类连在一起是什么意思?
  18. Python源码文件中带有中文时,输出乱码
  19. jQuery复选框全选和全选取消
  20. Alpha冲刺——day8

热门文章

  1. Struts2的OGNL标签详解
  2. 锋利的jQuery读书笔记---jQuery中操作DOM
  3. WPF如何在同一个区域依次叠加显示多张图片呢?
  4. POJ 1083 Moving Tables
  5. PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案
  6. Webservice 调用方式整理
  7. 试验笔记 - Eclipse的.class反编译插件
  8. MFC记住上次路径---遇到的一些问题
  9. Winxp下搭建SVN服务器
  10. 关于C++ vector的拷贝