Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27820   Accepted: 11208

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
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

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

 
题意:A喜欢B,B喜欢C,则A喜欢C,求被所有人喜欢的人的个数
思路:将每个强连通分量缩点,然后求每个点的出度,如果有多个无出度的点则无解,否则答案就是出度为0的强连通分量内点的个数
 
/*
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 ;
}

最新文章

  1. 用DOM4J解析XML文件案例
  2. dojo 加载Json数据
  3. MYSQL企业常用架构与调优经验分享
  4. EntityFramework6 快速入门教程
  5. Cheatsheet: 2015 01.01~ 01.31
  6. JDK中的Timer和TimerTask详解(zhuan)
  7. WinDbg配置与下载 (转载)
  8. VB VS2003获取当前进程用户登录
  9. c++类的基础
  10. 基于JDK1.8的ConcurrentHashMap分析
  11. scrapy分布式爬虫scrapy_redis二篇
  12. C# 操作Word书签(二)——插入图片、表格到书签;读取、替换书签
  13. Python Tkinter 简单使用
  14. 【转】33 个 2017 年必须了解的 iOS 开源库
  15. Eclipse添加JDK,JRE切换
  16. ***LANMP镜像手册(Apache&amp;Nginx)-lanmp-oneinstack
  17. MySQL 基础 DDL和DML
  18. 面试题:JS中map的陷阱
  19. learning ddr mode register MR1
  20. oracle锁表查询

热门文章

  1. coia阻止事件上浮
  2. laravel跨域问题
  3. C语言运算符(注意事项)
  4. 洛谷P1379八数码难题
  5. popen和system问题
  6. Browser-Solidity的本地安装及使用介绍
  7. Week2 Teamework from Z.XML 软件分析与用户需求调查(三)必应助手体验评测
  8. JAVA课程设计 俄罗斯方块
  9. Java相关配置合集
  10. elementUI默认样式修改不成功的问题