zoukankan      html  css  js  c++  java
  • poj 2186(tarjan+缩点)

    Popular Cows
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 37083   Accepted: 15104

    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
    缩点之后 出度为0的点的个数就是答案
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstdlib>
      4 #include<cctype>
      5 #include<cmath>
      6 #include<cstring>
      7 #include<map>
      8 #include<queue>
      9 #include<stack>
     10 #include<set>
     11 #include<vector>
     12 #include<algorithm>
     13 #include<string.h>
     14 typedef long long ll;
     15 typedef unsigned long long LL;
     16 using namespace std;
     17 const int INF=0x3f3f3f3f;
     18 const double eps=0.0000000001;
     19 const int N=30000+10;
     20 const ll mod=1e9+7;
     21 int dfn[N];
     22 int low[N];
     23 int vis[N];
     24 int head[N];
     25 int cnt;
     26 stack<int>st;
     27 int belong[N];
     28 struct node{
     29     int to,next;
     30 }edge[N<<1];
     31 int num[N];
     32 int t,tot;
     33 int in[N];
     34 int out[N];
     35 void init(){
     36     memset(head,-1,sizeof(head));
     37     memset(low,0,sizeof(low));
     38     memset(belong,-1,sizeof(belong));
     39     memset(vis,0,sizeof(vis));
     40     memset(in,0,sizeof(in));
     41     memset(out,0,sizeof(out));
     42     memset(num,0,sizeof(num));
     43     tot=t=0;
     44     cnt=0;
     45 }
     46 void add(int u,int v){
     47     edge[tot].to=v;
     48     edge[tot].next=head[u];
     49     head[u]=tot++;
     50 }
     51 void tarjan(int u){
     52     low[u]=dfn[u]=++t;
     53     vis[u]=1;
     54     st.push(u);
     55     for(int i=head[u];i!=-1;i=edge[i].next){
     56         int v=edge[i].to;
     57         if(dfn[v]==0){
     58             tarjan(v);
     59             low[u]=min(low[u],low[v]);
     60         }
     61         else if(vis[v]){
     62             low[u]=min(low[u],dfn[v]);
     63         }
     64     }
     65     if(low[u]==dfn[u]){
     66         int vv;
     67         cnt++;
     68         do{
     69             vv=st.top();
     70             st.pop();
     71             belong[vv]=cnt;
     72             num[cnt]++;
     73             vis[vv]=0;
     74         }while(vv!=u);
     75     }
     76 }
     77 int main(){
     78     int n,m;
     79     while(scanf("%d%d",&n,&m)!=EOF){
     80         init();
     81         for(int i=1;i<=m;i++){
     82             int u,v;
     83             scanf("%d%d",&u,&v);
     84             add(u,v);
     85         }
     86         for(int i=1;i<=n;i++){
     87             if(dfn[i]==0)tarjan(i);
     88         }
     89         for(int i=1;i<=n;i++){
     90             for(int j=head[i];j!=-1;j=edge[j].next){
     91                 int v=edge[j].to;
     92                 if(belong[i]!=belong[v]){
     93                     in[belong[v]]++;
     94                     out[belong[i]]++;
     95                 }
     96             }
     97         }
     98         int flag=0;
     99         int ans=0;
    100         //cout<<cnt<<endl;
    101         for(int i=1;i<=cnt;i++){
    102             if(out[i]==0){
    103                 flag++;ans=ans+num[i];
    104             }
    105         }
    106         if(flag==1){
    107             cout<<ans<<endl;
    108         }
    109         else{
    110             cout<<0<<endl;
    111         }
    112     }
    113 }
  • 相关阅读:
    搭建App主流框架_纯代码搭建(OC)
    UIApplication,UIWindow,UIViewController,UIView(layer)
    插件类
    VIEWCONTROLLER的启动流程
    UIView你知道多少
    分析UIWindow
    创建控制器的3种方式、深入了解view的创建和加载顺序
    UIViewController的生命周期及iOS程序执行顺序
    ViewController加载顺序与self.view
    GCD
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/8604781.html
Copyright © 2011-2022 走看看