POJ2186 (强连通分量缩点后出度为0的分量内点个数)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 27820 | Accepted: 11208 |
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
/*
ID: LinKArftc
PROG: 2186.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ;
int n, m; struct Edge {
int v, next;
} edge[maxm]; int head[maxn], tot; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot ++;
} int dfn[maxn], low[maxn], belong[maxn];//每个点属于哪个连通分量
int scc;//连通分量总数,下标 1~scc
int Time;
int ins[maxn];
stack <int> s;
vector <int> vec[maxn];//记录每个连通分量内的点 void tarjan(int u) {
int v;
low[u] = dfn[u] = ++ Time;
s.push(u);
ins[u] = true;
for (int i = head[u]; i + ; i = edge[i].next) {
v = edge[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (ins[v]) low[u] = min(low[u], dfn[v]);//写出low[u] = min(low[u], low[v]);也可以
}
if (low[u] == dfn[u]) {
scc ++;
do {
v = s.top();
s.pop();
ins[v] = false;
belong[v] = scc;
vec[scc].push_back(v);
} while (u != v);
}
} int outdeg[maxn]; int main() {
//input;
while (~scanf("%d %d", &n, &m)) {
init();
int u, v;
for (int i = ; i <= m; i ++) {
scanf("%d %d", &u, &v);
addedge(u, v);
}
while (!s.empty()) s.pop();
for (int i = ; i <= maxn; i ++) vec[i].clear();
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
Time = ; scc = ;
for (int i = ; i <= n; i ++) {
if (!dfn[i]) tarjan(i);
}
memset(outdeg, , sizeof(outdeg));
for (int u = ; u <= n; u ++) {
for (int i = head[u]; i + ; i = edge[i].next) {
int v = edge[i].v;
if (belong[u] == belong[v]) continue;
outdeg[belong[u]] ++;
}
}
int ans = ;
bool has = false;
bool flag = false;
int ii;
for (int i = ; i <= scc; i ++) {
if (outdeg[i] == ) {
ii = i;
if (has) {
flag = false;
break;
} else {
has = true;
flag = true;
}
}
}
if (flag) printf("%d\n", vec[ii].size());
else printf("0\n");
}
return ;
}
POJ2186 (强连通分量缩点后出度为0的分量内点个数)的更多相关文章
- POJ1236 (强连通分量缩点求入度为0和出度为0的分量个数)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13804 Accepted: 55 ...
- POJ1236 强连通 (缩点后度数的应用)
题意: 一些学校有一个发送消息的体系,现在给你一些可以直接发送消息的一些关系(单向)然后有两个问题 (1) 问你至少向多少个学校发送消息可以让所有的学校都得到消息 (2) 问至少加多少条边 ...
- poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】
Redundant Paths Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11047 Accepted: 4725 ...
- Reachability from the Capital CodeForces - 999E(强连通分量 缩点 入度为0的点)
题意: 问至少加几条边 能使点s可以到达所有的点 解析: 无向图的连通分量意义就是 在这个连通分量里 没两个点之间至少有一条可以相互到达的路径 所以 我们符合这种关系的点放在一起, 由s向这些点的任 ...
- 【poj2553】The Bottom of a Graph(强连通分量缩点)
题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...
- POJ-2186 Popular Cows,tarjan缩点找出度为0的点。
Popular Cows 题意:一只牛崇拜另外一只牛,这种崇拜关系可以传导.A->B,B->C =>A->C.现在给出所有的关系问你有多少牛被其他所有的牛都崇拜. 思路:就是一 ...
- ZOJ3795 Grouping(强连通分量+缩点+记忆化搜索)
题目给一张有向图,要把点分组,问最少要几个组使得同组内的任意两点不连通. 首先考虑找出强连通分量缩点后形成DAG,强连通分量内的点肯定各自一组,两个强连通分量的拓扑序能确定的也得各自一组. 能在同一组 ...
- Tarjan求强连通分量 缩点
强连通分量的定义: 在一张有向图中,如果两个点u,v之间能相互到达则称这两个点u,v是强连通的,在这个基础上如果有向图G中的任意两个顶点都强连通,那么称图G是一个强连通图.有向非强连通图的极大强连通子 ...
- 【强连通分量缩点】poj 1236 Network of Schools
poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
随机推荐
- python自动化之BDD框架之lettuce初识问题集
最近在学习虫师老师编写的python自动化的书.其中讲到了BDD结构lettuce入门一章. 因为是小白,按部就班地进行操作,先不谈执行操作如何,先来讲讲遇到的几个坑,和怎么解决的: 第一坑:pyth ...
- 孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6
孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 不过由于对python-docx模 ...
- PHP实现字节数Byte转换为KB、MB、GB、TB
function getFilesize($num) { $p = 0; $format = 'bytes'; if( $num > 0 && $num < 1024 ) ...
- 稳定匹配 - Stable Matching
这篇文章将会对稳定匹配算法进行介绍及Python代码的实现,第一部分会针对稳定匹配的Gale-Shapley算法进行解析,第二部分就是用Python对该算法进行实现. 一.稳定匹配算法原理 1.1 介 ...
- mysql语法总结
增: 删: 改: 查: 索引: 建: alter table: sql一些常用的经典语句,最后是select as的用法
- iOS-显示日期的转换,今天,昨天,前天
+ (NSString *)stringWithDate:(NSDate *)date{ // 1.获得年月日 NSCalendar *calendar = [NSCalendar currentCa ...
- js定时器实现图片轮播
效果展示如下: setInterval(moverleft,3000);定时器设置为3秒,而且实现图片下方的小圆点序号跟图片对应,点击小圆点也能切换图片. 代码如下: <!DOCTYPE htm ...
- 接触到的一些数据结构: LIST_ENTRY, TAILQ
双链表: LIST_ENTRY: typedef struct _LIST_ENTRY { struct _LIST_ENTRY *Flink; follow: next entry, header ...
- c++ 中反正单词用到了resize()
resize(n) 调整容器的长度大小,使其能容纳n个元素.如果n小于容器的当前的size,则删除多出来的元素.否则,添加采用值初始化的元素. 题目如下: 151. Reverse Words in ...
- java enum naming rules & Pascal case, Camel case, Uppercase
java enum naming rules Constant & all Capital Case https://stackoverflow.com/questions/3069743/c ...