题意:求强联通分量

Tarjan算法

 #include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<map>
#define clc(a,b) memset(a,b,sizeof(a))
typedef long double ld;
typedef long long ll;
const int N = ;
const double eps=1e-;
const int inf=-;
const int maxn=1e5+;
const int num=;
const double Pi=acos(-);
using namespace std; struct gragh
{
int to;
int next;
} V[num]; bool instack[num]= {false};
int low[num]= {},DFN[num]= {},Stap[num]= {},Belong[num]= {};
int answer=;
int Dindex,stop,Bcnt;
int head[num];
int edge; void add(int a,int b)
{
V[edge].to=b;
V[edge].next=head[a];
head[a]=edge++; }
void tarjan(int u)
{
int v;
DFN[u]=low[u]=++Dindex;
instack[u]=true;
Stap[stop++]=u;
for(int i=head[u]; i!=-; i=V[i].next)
{
v=V[i].to;
if(!DFN[v])
{
tarjan(v);
if(low[v]<low[u])
low[u]=low[v];
}
else if(instack[v]&&DFN[v]<low[u])
low[u]=DFN[v];
}
if(DFN[u]==low[u])
{
int sum=;
Bcnt++;
do
{
v=Stap[--stop];
instack[v]=false;
Belong[v]=Bcnt;
sum++;
}
while(v!=u);
cout<<sum<<endl;
if(sum!=)
answer+=(sum*(sum-))/;
}
}
void solve(int N)
{
stop=Bcnt=Dindex=;
// clc(DFN,0);
for(int i=; i<=N; i++)
if(!DFN[i])
tarjan(i);
} int main()
{
int n,m;
int a,b;
cin>>n>>m;
clc(head,-);
edge=;
for(int i=; i<m; i++)
{
cin>>a>>b;
add(a,b);
}
solve(n);
cout<<answer<<endl;
return ;
}

CCF 认证4的更多相关文章

  1. CCF认证历年试题

    CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...

  2. 小明种苹果(续)第十七次CCF认证

    小明种苹果(续)第十七次CCF认证 题目 原题链接 ](http://118.190.20.162/view.page?gpid=T93) 很高心,在现在CCF CSP可以下载自己当时的答卷了,也就是 ...

  3. CCF认证(1)

    #include <iostream> #include <windows.h> using namespace std; typedef struct letter{ int ...

  4. CCF 认证

    题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...

  5. CCF认证考试——折点计数

    描述:简单题 #include<iostream> using namespace std; int main() { ], n, count = ; cin >> n; ; ...

  6. CCF认证之——相反数

    这道题目非常简单! #include<iostream> using namespace std; int main() { ],n,count=; cin >> n; ; i ...

  7. ccf认证 201709-4 通信网络 java实现

    试题编号:                                                               201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...

  8. ccf认证模拟题之三---最大的矩形

    问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...

  9. CCF认证201712-2游戏

    问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1号小朋 ...

随机推荐

  1. protobuf 向前兼容向后兼容

    http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/ 不错的protobuf.. protobuf的编码方式: ...

  2. Javascript中Array.prototype.map()详解

    map 方法会给原数组中的每个元素都按顺序调用一次 callback 函数.callback 每次执行后的返回值组合起来形成一个新数组. callback 函数只会在有值的索引上被调用:那些从来没被赋 ...

  3. Python SyntaxError: Non-ASCII character '\xe5'

    error: SyntaxError: Non-ASCII character '\xe5' in file D:\worklife\workshop\myCrawler\src\mainDriver ...

  4. Hex string convert to integer with stringstream

    #include <sstream>#include <iostream>int main() { unsigned int x; std::stringstream ss; ...

  5. php采集远程文章简单类

    <?php /** * 采集类 * @author Milkcy * @copyright (C) 2012-2015 TCCMS.COM * @lastmodify 2012-07-10 14 ...

  6. ASC #1

    开始套题训练,第一套ASC题目,记住不放过每一题,多独立思考. Problem A ZOJ 2313 Chinese Girls' Amusement 循环节 题意:给定n,为圆环长度,求k < ...

  7. CodeForces152C——Pocket Book(排列组合问题)

    Pocket Book DescriptionOne day little Vasya found mom's pocket book. The book had n names of her fri ...

  8. python脚本工具 - 3 目录遍历

    遍历系统中某一目录下的所有文件名 #! /usr/bin/python # coding:utf-8 import os def dirList(path): filelist = os.listdi ...

  9. android系统平台显示驱动开发简要:Samsung LCD接口篇『三』

    平台信息: 内核:linux3.4.39系统:android4.4 平台:S5P4418(cortex a9) 作者:瘋耔(欢迎转载,请注明作者) 欢迎指正错误,共同学习.共同进步!! 关注博主新浪博 ...

  10. 正确认识Android的内存管理机制,合理关闭进程 (一)

    随着大家收货后会有很多乐粉晒内存,为啦方便大家,在网上搜集了一些相关Andriod管理的相关机制合理管理内存,整理下发个贴. 首先要知道Android系统是基于Linux 2.6内核开发的开源操作系统 ...