题意:求强联通分量

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. 贪心算法——将正整数变为1

    题目链接http://toutiao.com/a6320936270101528833/ 为避免链接失效,再粘贴一下题目内容: 给你一个数n,有3种操作: 1.这个数加1 2.这个数减1 3.如果这个 ...

  2. UIScrollView的坑--UINavigationController Push后位置变化

    今天在使用UIScrollView的时候遇到了一个问题,记录一下.如果这个记录有幸被您搜索到,或许对您有些帮助. 今天有这样一个需求: 在一个由导航条控制的页面中.需要显示一些信息,目前已经有10多行 ...

  3. asp.net中@ Import 命令的使用

    @ Import  将命名空间显式导入到 ASP.NET 应用程序文件(如网页.用户控件.母版页或 Global.asax 文件)中,同时使导入的命名空间的所有类和接口可用于文件.导入的命名空间可以是 ...

  4. ACM题集以及各种总结大全!

    ACM题集以及各种总结大全! 虽然退役了,但是整理一下,供小弟小妹们以后切题方便一些,但由于近来考试太多,顾退役总结延迟一段时间再写!先写一下各种分类和题集,欢迎各位大牛路过指正. 一.ACM入门 关 ...

  5. cf 320B

    数据量小  dfs水过 #include <iostream> #include <cstdio> #include <cstring> using namespa ...

  6. hdu 4664 Triangulation 博弈论

    看到这题时,当时还不会做,也没搞懂sg函数,于是狠狠的钻研了下博弈论,渐渐的知道了sg函数…… 现在在来做这题就很容易了,1A 打表容易发现在80左右的时候就出现循环节了 代码如下: #include ...

  7. 2.Adding a Controller

    MVC stands for model-view-controller.  MVC is a pattern for developing applications that are well ar ...

  8. jquery layout学习

    1.官网:http://layout.jquery-dev.com/index.cfm 2.博客园:http://www.cnblogs.com/chen-fan/articles/2044556.h ...

  9. http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html

    http://jingyan.baidu.com/article/a3761b2b66fe141577f9aa51.html

  10. [itint5]下一个排列

    http://www.itint5.com/oj/#6 首先,试验的时候要拿5个来试,3,4个都太少了.好久没做所以方法也忘了,是先从后往前找到第一个不合顺序的,然后在后面找到比这个大的最小的来交换, ...