国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

poj 2186 Popular Cows 【強連通分量】

2019-11-08 02:29:34
字體:
來源:轉載
供稿:網友

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32368 Accepted: 13202

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 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall

運用強連通分量將圖變成DAG圖,并知道了最后一個分量可能為拓撲終點,dfs反邊搜索看是否達到所有邊。

代碼:

#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;int n,m,cmp[10100];vector<int> V[10100],FV[10100],vs;bool used[10100];void add_edge(int a,int b){    V[a].push_back(b);    FV[b].push_back(a);}void dfs(int v){    used[v]=true;    for (int i=0;i<V[v].size();i++)    {        if (!used[V[v][i]]) dfs(V[v][i]);    }    vs.push_back(v);}void rdfs(int v,int k){    used[v]=true;    cmp[v]=k;    for (int i=0;i<FV[v].size();i++)    {        if (!used[FV[v][i]]) rdfs(FV[v][i],k);    }}int main(){    scanf("%d%d",&n,&m);  //  while (~scanf("%d%d",&n,&m))  //  {        int a,b;      //  for (int i=1;i<=n;i++)      //  {      //      V[i].clear();      //      FV[i].clear();      //  }        vs.clear();        for (int i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            add_edge(a,b);        }        memset(used,0,sizeof(used));        for (int i=1;i<=n;i++)        {            if(!used[i]) dfs(i);        }        memset(used,0,sizeof(used));        int k=1;        for (int i=vs.size()-1;i>=0;i--)        {            if(!used[vs[i]]) rdfs(vs[i],k++);        }        int ans=0;        for (int i=1;i<=n;i++)        {            if (cmp[i]==k-1)            {                m=i;                ans++;            }        }        memset(used,0,sizeof(used));        rdfs(m,0);        for (int i=1;i<=n;i++)        {            if (!used[i])            {                ans=0;                break;            }        }        PRintf("%d/n",ans);    //}    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 全州县| 河北省| 武胜县| 惠安县| 珠海市| 石林| 镇远县| 邵东县| 汨罗市| 青田县| 高青县| 怀集县| 安龙县| 怀安县| 定边县| 聊城市| 长岭县| 台北县| 南丰县| 榕江县| 临夏县| 桐城市| 二连浩特市| 福州市| 黎平县| 万宁市| 鹿泉市| 宜兰县| 广元市| 鲁甸县| 青神县| 普宁市| 太原市| 九寨沟县| 冕宁县| 秦皇岛市| 武义县| 上林县| 定日县| 舟曲县| 贺州市|