2208: [Jsoi2010]连通数
来源:互联网 发布:阿里云账号不记得了 编辑:程序博客网 时间:2024/06/05 07:15
2208: [Jsoi2010]连通数
Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2403 Solved: 1021
[Submit][Status][Discuss]
Description
Input
输入数据第一行是图顶点的数量,一个正整数N。 接下来N行,每行N个字符。第i行第j列的1表示顶点i到j有边,0则表示无边。
Output
输出一行一个整数,表示该图的连通数。
Sample Input
3
010
001
100
010
001
100
Sample Output
9
HINT
对于100%的数据,N不超过2000。
Source
第一轮
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<queue>#include<set>#include<map>#include<stack>#include<bitset>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 2020;int n,ans,scc,dfs_clock,dfn[maxn],low[maxn],bel[maxn],du[maxn],siz[maxn];char ch[maxn];vector <int> v[maxn];vector <int> v2[maxn];queue <int> Q;stack <int> s;bitset <maxn> B[maxn];void Dfs(int x){dfn[x] = low[x] = ++dfs_clock; s.push(x);for (int i = 0; i < v[x].size(); i++){int to = v[x][i];if (!dfn[to]) Dfs(to),low[x] = min(low[x],low[to]);else if (!bel[to]) low[x] = min(low[x],dfn[to]);}if (low[x] == dfn[x]){++scc;for (;;){int tp = s.top(); s.pop();bel[tp] = scc; ++siz[scc];B[scc][tp] = 1;if (tp == x) break;}}}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n;for (int i = 1; i <= n; i++){scanf("%s",ch + 1);for (int j = 1; j <= n; j++)if (ch[j] == '1') v[i].push_back(j);}for (int i = 1; i <= n; i++)if (!dfn[i]) Dfs(i);for (int i = 1; i <= n; i++)for (int j = 0; j < v[i].size(); j++){int x = bel[i],y = bel[v[i][j]];if (x == y) continue;++du[x]; v2[y].push_back(x);}for (int i = 1; i <= scc; i++)if (!du[i]) Q.push(i);while (!Q.empty()){int k = Q.front(); Q.pop();ans += siz[k]*B[k].count();for (int i = 0; i < v2[k].size(); i++){int to = v2[k][i]; --du[to];B[to] |= B[k];if (!du[to]) Q.push(to);}}cout << ans;return 0;}
0 0
- 2208: [Jsoi2010]连通数
- 2208: [Jsoi2010]连通数
- 2208: [Jsoi2010]连通数
- bzoj 2208: [Jsoi2010]连通数
- 【BZOJ 2208】 [Jsoi2010]连通数
- bzoj 2208: [Jsoi2010]连通数
- bzoj 2208: [Jsoi2010]连通数
- BZOJ 2208: [Jsoi2010]连通数
- BZOJ 2208 [Jsoi2010]连通数
- BZOJ 2208: [Jsoi2010]连通数
- BZOJ 2208 [Jsoi2010]连通数
- 2208: [Jsoi2010]连通数(tarjan + 状态压缩)
- 2208: [Jsoi2010]连通数(Trajan+bitset)
- bzoj 2208 [Jsoi2010]连通数 bitset
- BZOJ2208: [Jsoi2010]连通数
- BZOJ2208: [Jsoi2010]连通数
- bzoj2208 [Jsoi2010]连通数
- bzoj2208: [Jsoi2010]连通数
- 文章标题 POJ 3253 : Fence Repair(哈夫曼)
- 多个Activity之间切换原理介绍
- 总结30
- MiniTwitterSimulate 模拟登陆界面
- 用数组写队列
- 2208: [Jsoi2010]连通数
- 四种单击事件
- 块设备驱动程序
- SQLite数据库存储
- JSP Cookie中文问题s
- 字符传的操作
- Cross Validation for Ensemble Learning(Stacking!!)
- Java实现堆排序
- 堆和栈详解