POJ2186

poj2186 popular cows

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

题意:10000头奶牛,50000对仰慕关系,且仰慕关系可以传递,问所有牛都仰慕的对象有多少?

————————————————————————————————————————————————

强连通分量中相互仰慕,所点后各点形成有向无环图,而其中如果有一个点(强连通分量)出度为0,那么这个强连通分量包含的点数就是答案;若果有多个点出度为0,则没有所有牛都仰慕的牛,答案0。

————————————————————————————————————————————————

  1 //poj2186 popular cows
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<stack>
  7
  8 using namespace std;
  9 const int maxn=10010;
 10 const int maxm=50010;
 11 int n,m;
 12 struct edge
 13 {
 14     int u,v,next;
 15 }e[maxm],ee[maxm];
 16 int head[maxn],js,headd[maxn],jss;
 17 int dfsn[maxn],low[maxn],belong[maxn];
 18 bool ins[maxn];
 19 int sshu;
 20 int chudu[maxn],ss[maxn];
 21 stack<int>st;
 22 int visx;
 23 void init()
 24 {
 25     memset(head,0,sizeof(head));
 26     memset(e,0,sizeof(e));
 27     js=0;
 28     memset(headd,0,sizeof(headd));
 29     memset(ee,0,sizeof(ee));
 30     jss=0;
 31     memset(dfsn,-1,sizeof(dfsn));
 32     memset(low,-1,sizeof(low));
 33     sshu=0;
 34     memset(ss,0,sizeof(ss));
 35     while(!st.empty())st.pop();
 36     visx=0;
 37     memset(ins,0,sizeof(ins));
 38     memset(chudu,0,sizeof(chudu));
 39     memset(belong,0,sizeof(belong));
 40 }
 41 void addage(int u,int v,edge e[],int head[],int &js)
 42 {
 43     e[++js].u=u;e[js].v=v;
 44     e[js].next=head[u];head[u]=js;
 45 }
 46
 47 void tarjan(int u)
 48 {
 49     dfsn[u]=low[u]=++visx;
 50     st.push(u);
 51     ins[u]=1;
 52     for(int i=head[u];i;i=e[i].next)
 53     {
 54         int v=e[i].v;
 55         if(dfsn[v]==-1)
 56         {
 57             tarjan(v);
 58             low[u]=min(low[u],low[v]);
 59         }
 60         else if(ins[v] && low[u]>dfsn[v])low[u]=dfsn[v];
 61     }
 62     if(low[u]==dfsn[u])
 63     {
 64         sshu++;
 65         int tp;
 66         do
 67         {
 68             tp=st.top();
 69             st.pop();
 70             ins[tp]=0;
 71             ss[sshu]++;
 72             belong[tp]=sshu;
 73         }while(u!=tp);
 74     }
 75 }
 76 int main()
 77 {
 78     while(scanf("%d%d",&n,&m)==2)
 79     {
 80         init();
 81         for(int i=0,u,v;i<m;i++)
 82         {
 83             scanf("%d%d",&u,&v);
 84             addage(u,v,e,head,js);
 85         }
 86         for(int i=1;i<=n;i++)
 87             if(dfsn[i]==-1)tarjan(i);
 88         for(int i=1;i<=m;i++)
 89         {
 90             int u=e[i].u,v=e[i].v;
 91             if(belong[u]!=belong[v])
 92             {
 93                 addage(belong[u],belong[v],ee,headd,jss);
 94                 chudu[belong[u]]++;
 95             }
 96         }
 97         int count=0,mn;
 98         for(int i=1;i<=sshu;i++)
 99             if(chudu[i]==0)count++,mn=i;
100         if(count==1)printf("%d\n",ss[mn]);
101         else printf("0\n");
102     }
103     return 0;
104 }

时间: 2024-08-09 10:33:25

POJ2186的相关文章

poj2186 Popular Cows --- 强连通

给一个有向图,问有多少结点是其他所有结点都可以到达的. 等价于,在一个有向无环图上,找出度为0 的结点,如果出度为0的结点只有一个,那么这个就是答案,如果大于1个,则答案是0. 这题有环,所以先缩点.求唯一出度为0的强连通分量. #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<iostream> #define inf 0x3f3f3f3f

强连通分量tarjan缩点——POJ2186 Popular Cows

这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定意味着v可达u.    相互可达则属于同一个强连通分量    (Strongly Connected Component, SCC) §有向图和它的转置的强连通分量相同 §所有SCC构成一个DAG(有向无环图) dfn[u]为节点u搜索的次序编号(时间戳),即首次访问u的时间 low[u]为u或u的

强连通分量+poj2186

强连通分量:两个点能够互相连通. 算法分解:第一步.正向dfs全部顶点,并后序遍历 第二步,将边反向,从最大边dfs,构成强连通分量 标号最大的节点属于DAG头部,cmp存一个强连通分量的拓扑序. poj2186 解就是拓扑后的最后一个强连通分量 #include<cstdio> #include<algorithm> #include<vector> #include<iostream> #include<cstring> #include&l

最近切的两题SCC的tarjan POJ1236 POJ2186

两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书 POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点) //poj1236 #include<iostream> #include<cstdio> #include<string.h> #define maxn 6000 int now=0,next[maxn],head[maxn],point[maxn],num=0,dfn

POJ2186 Popular Cows 【强连通分量Kosaraju】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

POJ2186 Popular Cows

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 popula

POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 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 &l

POJ2186 Popular Cows【Kosaraju】【强连通分量】

Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24266Accepted: 9954 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 <= 5

POJ2186 Popular Cows ,有向图, Tarjan算法

题意: 给定一个有向图,求有多少个顶点是由任何顶点出发都可达的. 顶点数<= 10,000,边数 <= 50,000 定理: 有向无环图中唯一出度为0的点,一定可以由任何点出发均可达 (由于无环,所以从任何点出发往前走,必然终止于一个出度为0的点) 1. 求出所有强连通分量 2. 每个强连通分量缩成一点,则形成一个有向无环图DAG. 3. DAG上面如果有唯一的出度为0的点,则该点能被所有的点可达.那么该点所代表的连通分量上的所有的原图中的点,都能被原图中的所有点可达,则该连通分量的点数,就是