Popular Cows POJ - 2186(强连通分量)
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
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int n, m;
vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt, out[maxn];
stack<int> S;
int vis[maxn]; void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i=; i<G[u].size(); i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v])
lowlink[u] = min(pre[v], lowlink[u]);
}
if(lowlink[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} int main()
{
int u, v;
rd(n), rd(m);
for(int i=; i<m; i++)
{
rd(u), rd(v);
G[u].push_back(v);
}
for(int i=; i<=n; i++)
if(!pre[i]) dfs(i);
// cout << 111 << endl;
if(scc_cnt == )
{
pd(n);
return ;
}
for(int i=; i<=n; i++)
for(int j=; j<G[i].size(); j++)
{
int v = G[i][j];
if(sccno[i] != sccno[v])
out[sccno[i]]++;
}
int res = ;
// cout << scc_cnt << endl;
// for(int i=1; i<=scc_cnt; i++)
// cout << in[i] << endl; int cnt = ;
for(int i=; i<=scc_cnt; i++)
if(out[i] == )
cnt++;
if(cnt != )
return puts(""), ;
for(int i=; i<=n; i++)
if(out[sccno[i]] == ) res++; pd(res); return ;
}
Popular Cows POJ - 2186(强连通分量)的更多相关文章
- poj 2186 强连通分量
poj 2186 强连通分量 传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 33414 Acc ...
- Popular Cows(POJ 2186)
原题如下: Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40746 Accepted: 16 ...
- POJ(2186)强连通分量分解
#include<cstdio> #include<vector> #include<cstring> using namespace std; ; vector& ...
- (连通图 缩点 强联通分支)Popular Cows -- poj --2186
http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
- Popular Cows (POJ No.2186)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- 图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- poj 1904(强连通分量+输入输出外挂)
题目链接:http://poj.org/problem?id=1904 题意:有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚,大臣给出一个匹配表,每个王子都和一个妹子结婚,但是国 ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
随机推荐
- Multiple Threads reading from the same file(转载)
问 I have a xml file that needs to be read from many many times. I am trying to use the Parallel.ForE ...
- 【LeetCode232】 Implement Queue using Stacks★
1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...
- 反射反射,程序员的快乐+反射案例:打印和Excel导出
还是那几句话: 学无止境,精益求精 十年河东,十年河西,莫欺少年穷 学历代表你的过去,能力代表你的现在,学习代表你的将来 看过设计模式的童鞋都知道:反射反射,程序员的快乐!今天我们就利用反射来制作打印 ...
- CYJian的水题大赛
实在没忍住就去打比赛了然后一耗就是一天 最后Rank19还是挺好的(要不是乐多赛不然炸飞),这是唯一一套在Luogu上号称水题大赛的而实际上真的是水题大赛的比赛 好了我们开始看题 T1 八百标兵奔北坡 ...
- 面试4——java进程和线程相关知识
1.线程和进程的概念.并行和并发的概念
- Codeforces 987E Petr and Permutations(数组的置换与复原 、结论)
题目连接: Petr and Permutations 题意:给出一个1到n的序列,Petr打乱了3n次,Um_nik打乱了7n+1次,现在给出被打乱后的序列,求是谁打乱的. 题解:因为给出了一个3* ...
- HNOI2019 多边形 polygon
HNOI2019 多边形 polygon https://www.luogu.org/problemnew/show/P5288 这题镪啊... 首先堆结论: 显然终止状态一定是所有边都连向n了 根据 ...
- Bluedroid 函数分析:BTA_GATTC_Open
进行GATT 通信,首先要打开GATT 的通道.下面我们分析BTA_GATTC_Open 这个函数: 这个函数在bta_gattc_api.c 文件中定义,这个是一个接口文件,里面没有做真正的open ...
- Mybatis教程-实战看这一篇就够了
转自:https://blog.csdn.net/hellozpc/article/details/80878563 1.从JDBC谈起 1.1.使用IDEA创建maven工程 1.2.引入mysql ...
- Linux下FastDFS分布式存储-总结及部署记录
一.分布式文件系统介绍分布式文件系统:Distributed file system, DFS,又叫做网络文件系统:Network File System.一种允许文件通过网络在多台主机上分享的文件系 ...