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)至少要加多少条边,才能使得从任何一个顶点出发,都 ...
随机推荐
- url解读
我刚刚学习的时候,我抓到包不知道哪个是协议.哪个是是服务器地址.哪个是端口号...不知道有没有老铁遇到跟我一样的. 接口:http://172.168.12.0:8888/old/login.do 解 ...
- ipfs01
IPFS音乐播放器 IPFS相关 IPFS第一次亲密接触 什么是IPFS IPFS对比HTTP/FTP等协议的优势 IPFS应用场景 ipfs入门 官网地址:https://ipfs.io 下载安 ...
- Java 访问权限控制 小结
总所周知,Java提供了访问权限修饰词,以供类库开发人员向客户端程序员指明哪些是可用的,哪些是不可用的. 访问权限控制的等级,从最大权限到最小权限依次为:public.protected.包访问权限( ...
- fork开源代码后如何基于某个tag建立自己的branch
应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...
- 【SSH】——封装参数不确定的分页查询
[前言] 在BS中,分页技术的应用相当频繁.说到分页,简单的分页就很好实现了,如果在分页的基础上再加上业务逻辑,这就使得分页的技术更加的灵活了. [简单分页] 我们先看一种简单的分页,为了做到复用,我 ...
- redis集群如何清理前缀相同的key
最近经常收到redis集群告警,每天收到50多封邮件,实在不胜其烦,内存不够用,原因是有一些无用的key(约3000万)占用内存(具体不说了).这部分内存不能被释放. 原来的定期清理脚本的逻辑: 打开 ...
- regex & form validation & phone
regex & form validation https://regexper.com/ https://gitlab.com/javallone/regexper-static https ...
- DDD-领域驱动设计
识别领域事件 DDD战术篇:领域模型的应用 DDD战略篇:架构设计的响应力 DDD实战篇:分层架构的代码结构
- Linux设置快捷命令
vi ~/.bashrc 在.bashrc目录中,添加 alias 设置 例如 cdtools='cd ~/GIT/tools' 对于一条比较长的命令,如显示系统运行时长 cat /proc/upti ...
- 【转】Win7装不上Office2010 提示MSXML 6.10.1129.0
转自:http://zhidao.baidu.com/link?url=aZPbpBu0Fb7rc8HCb_NuonuZ4ET_BB8_NgZ96tCpB9dyuUyWVwMl78MLa7rh-rfx ...