poj 2186(tarjan+缩点)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37083 | Accepted: 15104 |
Description
popular, even if this is not explicitly specified by an ordered pair
in the input. Your task is to compute the number of cows that are
considered popular by every other cow.
Input
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
int dfn[N];
int low[N];
int vis[N];
int head[N];
int cnt;
stack<int>st;
int belong[N];
struct node{
int to,next;
}edge[N<<];
int num[N];
int t,tot;
int in[N];
int out[N];
void init(){
memset(head,-,sizeof(head));
memset(low,,sizeof(low));
memset(belong,-,sizeof(belong));
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(num,,sizeof(num));
tot=t=;
cnt=;
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void tarjan(int u){
low[u]=dfn[u]=++t;
vis[u]=;
st.push(u);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(dfn[v]==){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
int vv;
cnt++;
do{
vv=st.top();
st.pop();
belong[vv]=cnt;
num[cnt]++;
vis[vv]=;
}while(vv!=u);
}
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;i++){
if(dfn[i]==)tarjan(i);
}
for(int i=;i<=n;i++){
for(int j=head[i];j!=-;j=edge[j].next){
int v=edge[j].to;
if(belong[i]!=belong[v]){
in[belong[v]]++;
out[belong[i]]++;
}
}
}
int flag=;
int ans=;
//cout<<cnt<<endl;
for(int i=;i<=cnt;i++){
if(out[i]==){
flag++;ans=ans+num[i];
}
}
if(flag==){
cout<<ans<<endl;
}
else{
cout<<<<endl;
}
}
}
poj 2186(tarjan+缩点)的更多相关文章
- POJ 2186 tarjan+缩点 基础题
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 37111 Accepted: 15124 De ...
- POJ 2762 tarjan缩点+并查集+度数
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15494 ...
- poj 2186 (强连通缩点)
题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...
- POJ 2672 Tarjan + 缩点 + 拓扑思想
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17383 ...
- POJ 3694 (tarjan缩点+LCA+并查集)
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...
- poj 2762(tarjan缩点+判断是否是单链)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19234 ...
- poj 2186 强连通+缩点
题意:有一群牛,求被所有牛都认可的牛的个数 每个连通分量建一个缩点,出度为零的缩点包含的点的个数即为要求值 如果有多个出度为零的,直接输出零,否则输出那唯一一个出度为零的缩点包含的点的个数 #incl ...
- poj 2186 tarjan求强连通分量
蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...
- Countries in War (POJ 3114) Tarjan缩点+最短路
题目大意: 在一个有向图中,每两点间通信需要一定的时间,但同一个强连通分量里传递信息不用时间,给两点u,v求他们最小的通信时间. 解题过程: 1.首先把强连通分量缩点,然后遍历每一条边来更新两个强 ...
随机推荐
- 零基础入门学习Python(27)--集合:在我的世界里,你就是唯一
知识点 集合:set set和dict类似,也是一组key的集合,但不存储value.由于key不能重复,所以,在set中,没有重复的key. 集合中的元素的三个特征: 1)确定性(元素必须可hash ...
- GROUP函数
GROUP_ID 首先我们看看官方的解释: 大意是GROUP_ID用于区分相同分组标准的分组统计结果. 解释起来比较抽象,下面我们来看看具体的案例. 例1:单一分组 SQL> select gr ...
- Python爬虫例子(笔记,不适合参考,愿意看的可以看看)
话不多说,直接上代码: import re import csv #爬虫的一个小例子,爬的是百度贴吧(网页版)某个帖子的各个楼层的用户名,发言内容和发言时间(使用到了正则表达式) source3.tx ...
- Notepad++ 连接远程 FTP 进行文件编辑
一.下载安装 Notepad++ 1.下载 Notepad++ : https://pan.baidu.com/s/1o7VrS4y 密码 : ck8a 2.安装 Notepad++ 2.1.勾选所有 ...
- (dede)织梦系统二次开发笔记
(dede)织梦系统二次开发记录 --soulsjie 一.模板常用文件说明 模板文件都在文件夹templets下,我们以默认模板(default)为例,对模板文件结构进行分析: 首页模板文件目录 \ ...
- [HDU2136] Largest prime factor(素数筛)
传送门 题意 给出若干个数n(n<=1000000),求每个n的最大质因子的排名. 质数的排名:如果素数p是第k小的素数,那么p的排名就是k. 思路 乍一看不知道怎么搞. 其实可以想想我们怎么筛 ...
- Ubuntu 16.04升级4.7.0内核后导致Compiz奔溃,问题:compiz[4852]: segfault at 48 ip 00007f88cae087f0 sp 00007ffce354c268 error 4 in libscale.so
由于硬件的驱动支持问题,升级4.7.0的内核主要是为了能使用Intel HD Graphics 630驱动,但是也出现了相关问题,比如Compiz的特效导致桌面上如果有多个相同程序启动,然后再次点击时 ...
- Spring Boot多数据源连接8小时后断开的问题解决(MySQL)
这个问题涉及的方面很多,需要一步步去排查,可能环境有问题,数据库有问题,但是网上最多的应该是如下的方式去解决. 以单个数据源为主,多个数据源基本方法一致. 1.MySQL 5版本之前可以通过在URL后 ...
- SHARP AR-2048D/2348D
http://www.sharp.cn/printer/AR-2048D%7C2348D/support/download.html
- MongoDB小结23 - 索引简介
MongoDB中的索引,可以看作是书的目录. 想象一下给你一本没有目录的书,然后让你去查询指定内容,我只想说,我不是电脑,我很蛋疼! 让你翻没有目录的书,就跟让电脑查询没有索引的集合一样,从头查询到尾 ...