CCF 认证4
题意:求强联通分量
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的更多相关文章
- CCF认证历年试题
CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...
- 小明种苹果(续)第十七次CCF认证
小明种苹果(续)第十七次CCF认证 题目 原题链接 ](http://118.190.20.162/view.page?gpid=T93) 很高心,在现在CCF CSP可以下载自己当时的答卷了,也就是 ...
- CCF认证(1)
#include <iostream> #include <windows.h> using namespace std; typedef struct letter{ int ...
- CCF 认证
题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...
- CCF认证考试——折点计数
描述:简单题 #include<iostream> using namespace std; int main() { ], n, count = ; cin >> n; ; ...
- CCF认证之——相反数
这道题目非常简单! #include<iostream> using namespace std; int main() { ],n,count=; cin >> n; ; i ...
- ccf认证 201709-4 通信网络 java实现
试题编号: 201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...
- ccf认证模拟题之三---最大的矩形
问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...
- CCF认证201712-2游戏
问题描述 有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向. 游戏开始,从1号小朋 ...
随机推荐
- Android支付接入(四):联通VAC计费
原地址:http://blog.csdn.net/simdanfeg/article/details/9012031 注意事项: 1.联通支付是不需要自己标识软硬计费点的,当平台申请计费点的时候会提交 ...
- 线索二叉树Threaded binary tree
摘要 按照某种遍历方式对二叉树进行遍历,可以把二叉树中所有结点排序为一个线性序列.在该序列中,除第一个结点外每个结点有且仅有一个直接前驱结点:除最后一个结点外每一个结点有且仅有一个直接后继结点.这 ...
- 学点PYTHON基础的东东--数据结构,算法,设计模式---观察者模式
按照小明明的设计模式抄抄看看.. http://dongweiming.github.io/python-observer.html # 这个是观察者基类 class Subject(object): ...
- linux的定时任务crontab
每隔一分钟执行以下语句: #打印当前时间: date "+%Y-%m-%d %T" 保存为/usr/test/test.sh 查看系统中当前用户有多少个定时任务: crontab ...
- cocos2d-x 锚点,位置==》动手实验记录 多动手... :)
总结: 1:cocos2d-x的位置由锚点和Position位置 共同决定的.2: cocos2d-x,当位置不设置或者为零的时候, 子节点的锚点位置永远位于父节点的左小角的地方3:我们的自己的游戏编 ...
- [itint5]根据前序后序遍历统计二叉树
http://www.itint5.com/oj/#28 这题有意思.一开始还想不清楚,看了解释,很棒. 这个题目的特殊之处是所有节点的值都是不一样的. 所以递归过程可以大大简化. 先看两种遍历的性质 ...
- DAO是什么技术
DAO是Data Access Object数据访问接口,数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. 在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应 ...
- Android:控件的对象修改控件的值
TextView private TextView textView; ... textView =(TextView)findViewById(R.id.textView); textView.se ...
- ripple
ripple模拟器非常好用,chrome上的插件
- mmap
http://www.360doc.com/content/11/0830/10/1964482_144428042.shtml