zoukankan      html  css  js  c++  java
  • POJ2186(强连通分量分解)

    Popular Cows

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 35035   Accepted: 14278

    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

     
    题意:求从其他所有顶点都可以到达的顶点数目。
    思路:所求顶点数目即为拓扑序最后的强连通分量中的顶点数目,检查其他点是否都可以到达该强连通分量。
      1 //2017-08-20
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <iostream>
      5 #include <algorithm>
      6 #include <vector>
      7 
      8 using namespace std;
      9 
     10 const int N = 10010;
     11 vector<int> G[N];//邻接表存图
     12 vector<int> rG[N];//存反向图
     13 vector<int> vs;//后序遍历顺序的顶点列表
     14 bool vis[N];
     15 int cmp[N];//所属强连通分量的拓扑序
     16 
     17 void add_edge(int u, int v){
     18     G[u].push_back(v);
     19     rG[v].push_back(u);
     20 }
     21 
     22 //input: u 顶点
     23 //output: vs 后序遍历顺序的顶点列表
     24 void dfs(int u){
     25     vis[u] = true;
     26     for(int i = 0; i < G[u].size(); i++){
     27         int v = G[u][i];
     28         if(!vis[v])
     29               dfs(v);
     30     }
     31     vs.push_back(u);
     32 }
     33 
     34 //input: u 顶点编号; k 拓扑序号
     35 //output: cmp[] 强连通分量拓扑序
     36 void rdfs(int u, int k){
     37     vis[u] = true;
     38     cmp[u] = k;
     39     for(int i = 0; i < rG[u].size(); i++){
     40         int v = rG[u][i];
     41         if(!vis[v])
     42               rdfs(v, k);
     43     }
     44 }
     45 
     46 //Strongly Connected Component 强连通分量
     47 //input: n 顶点个数
     48 //output: k 强连通分量数;
     49 int scc(int n){
     50     memset(vis, 0, sizeof(vis));
     51     vs.clear();
     52     for(int u = 0; u < n; u++)
     53         if(!vis[u])
     54               dfs(u);
     55     int k = 0;
     56     memset(vis, 0, sizeof(vis));
     57     for(int i = vs.size()-1; i >= 0; i--)
     58           if(!vis[vs[i]])
     59               rdfs(vs[i], k++);
     60     return k;
     61 }
     62 
     63 void solve(int n){
     64     int k = scc(n);
     65     int u = 0, ans = 0;
     66     for(int v = 0; v < n; v++){
     67         if(cmp[v] == k-1){
     68             u = v;
     69             ans++;
     70         }
     71     }
     72     memset(vis, 0, sizeof(vis));
     73     rdfs(u, 0);
     74     for(int i = 0; i < n; i++){
     75         if(!vis[i]){
     76             ans = 0;
     77             break;
     78         }
     79     }
     80     printf("%d
    ", ans);
     81 }
     82 
     83 int main()
     84 {
     85     int n, m;
     86     while(scanf("%d%d", &n, &m)!=EOF){
     87         int u, v;
     88         for(int i = 0; i < n; i++){
     89             G[i].clear();
     90             rG[i].clear();
     91         }
     92         while(m--){
     93             scanf("%d%d", &u, &v);
     94             u--; v--;
     95             add_edge(u, v);
     96         }
     97         solve(n);
     98     }
     99 
    100     return 0;
    101 }
  • 相关阅读:
    oracle与DB2
    oracle ORA-01427: 单行子查询返回多个行
    mysql开发总结
    mysql show profile基本详解
    mysql批量插入数据
    mysql索引详解
    mysql性能调优
    MySQL优化
    mysql主从调优
    mysql主从复制
  • 原文地址:https://www.cnblogs.com/Penn000/p/7399829.html
Copyright © 2011-2022 走看看