poj 3180 The Cow Prom(tarjan+缩点 easy)

Description

The N (2 <= N <= 10,000) cows are so excited: it‘s prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. 

Only cows can perform the Round Dance which requires a set of ropes and a circular stock tank. To begin, the cows line up around a circular stock tank and number themselves in clockwise order consecutively from 1..N. Each cow faces the tank so she can see the other dancers. 

They then acquire a total of M (2 <= M <= 50,000) ropes all of which are distributed to the cows who hold them in their hooves. Each cow hopes to be given one or more ropes to hold in both her left and right hooves; some cows might be disappointed. 

For the Round Dance to succeed for any given cow (say, Bessie), the ropes that she holds must be configured just right. To know if Bessie‘s dance is successful, one must examine the set of cows holding the other ends of her ropes (if she has any), along with the cows holding the other ends of any ropes they hold, etc. When Bessie dances clockwise around the tank, she must instantly pull all the other cows in her group around clockwise, too. Likewise,
if she dances the other way, she must instantly pull the entire group counterclockwise (anti-clockwise in British English). 

Of course, if the ropes are not properly distributed then a set of cows might not form a proper dance group and thus can not succeed at the Round Dance. One way this happens is when only one rope connects two cows. One cow could pull the other in one direction, but could not pull the other direction (since pushing ropes is well-known to be fruitless). Note that the cows must Dance in lock-step: a dangling cow (perhaps with just one rope) that is eventually pulled along disqualifies a group from properly performing the Round Dance since she is not immediately pulled into lockstep with the rest. 

Given the ropes and their distribution to cows, how many groups of cows can properly perform the Round Dance? Note that a set of ropes and cows might wrap many times around the stock tank.

Input

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

Lines 2..M+1: Each line contains two space-separated integers A and B that describe a rope from cow A to cow B in the clockwise direction.

Output

Line 1: A single line with a single integer that is the number of groups successfully dancing the Round Dance.

Sample Input

5 4
2 4
3 5
1 2
4 1

Sample Output

1

Hint

Explanation of the sample: 

ASCII art for Round Dancing is challenging. Nevertheless, here is a representation of the cows around the stock tank:
       _1___

      /****
   5 /****** 2

  / /**TANK**|

  \ \********/

   \ \******/  3

    \ 4____/  /

     \_______/
Cows 1, 2, and 4 are properly connected and form a complete Round Dance group. Cows 3 and 5 don‘t have the second rope they‘d need to be able to pull both ways, thus they can not properly perform the Round Dance.

Source

USACO 2006 January Silver

用tarjan求出强连通分量后,用个map数组统计每个强连通分量的个数,要求强连通分量的子元素大于1(即至少两个)才行

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<stack>
  5 #include<vector>
  6 #include<map>
  7 using namespace std;
  8 #define N 100006
  9 int n,m;
 10 int tot;
 11
 12 int head[N];
 13 int vis[N];
 14 int tt;
 15 int scc;
 16 stack<int>s;
 17 int dfn[N],low[N];
 18 int col[N];
 19
 20 struct Node
 21 {
 22     int from;
 23     int to;
 24     int next;
 25 }edge[N<<6];
 26 void init()
 27 {
 28     tot=0;
 29     scc=0;
 30     tt=0;
 31     memset(head,-1,sizeof(head));
 32     memset(dfn,-1,sizeof(dfn));
 33     memset(low,0,sizeof(low));
 34     memset(vis,0,sizeof(vis));
 35     memset(col,0,sizeof(col));
 36 }
 37 void add(int s,int u)//邻接矩阵函数
 38 {
 39     edge[tot].from=s;
 40     edge[tot].to=u;
 41     edge[tot].next=head[s];
 42     head[s]=tot++;
 43 }
 44 void tarjan(int u)//tarjan算法找出图中的所有强连通分支
 45 {
 46     dfn[u] = low[u]= ++tt;
 47     vis[u]=1;
 48     s.push(u);
 49     int cnt=0;
 50     for(int i=head[u];i!=-1;i=edge[i].next)
 51     {
 52         int v=edge[i].to;
 53         if(dfn[v]==-1)
 54         {
 55         //    sum++;
 56             tarjan(v);
 57             low[u]=min(low[u],low[v]);
 58         }
 59         else if(vis[v]==1)
 60           low[u]=min(low[u],dfn[v]);
 61     }
 62     if(dfn[u]==low[u])
 63     {
 64         int x;
 65         scc++;
 66         do{
 67             x=s.top();
 68             s.pop();
 69             col[x]=scc;
 70             vis[x]=0;
 71         }while(x!=u);
 72     }
 73 }
 74 int main()
 75 {
 76     while(scanf("%d%d",&n,&m)==2)
 77     {
 78         init();
 79         for(int i=0;i<m;i++){
 80             int x,y;
 81             scanf("%d%d",&x,&y);
 82             add(x,y);
 83         }
 84
 85         for(int i=1;i<=n;i++)
 86         {
 87             if(dfn[i]==-1)
 88             {
 89                 tarjan(i);
 90             }
 91         }
 92        //printf("%d\n",scc);
 93        map<int,int> mp;
 94        for(int i=1;i<=n;i++){
 95             mp[col[i]]++;
 96        }
 97        int ans=0;
 98        for(int i=1;i<=scc;i++){
 99            if(mp[i]>1) ans++;
100        }
101        printf("%d\n",ans);
102     }
103     return 0;
104 }

时间: 2024-10-10 16:58:01

poj 3180 The Cow Prom(tarjan+缩点 easy)的相关文章

POJ 3180 The cow Prom Tarjan基础题

题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stack> 6 #define M 50010 7 #define N 10010 8 using namespace std; 9 int n,m,u,v,head[

poj 3180 The Cow Prom 强连通分量

水题,直接贴代码. //poj 3180 //sep9 #include <iostream> #include <stack> using namespace std; const int maxN=10024; const int maxM=50048; int sum[maxN]; int head[maxN],dfn[maxN],low[maxN],ins[maxN],belong[maxN]; stack<int> s; struct Edge{ int v,

POJ 3180 The Cow Prom

传送门:http://poj.org/problem?id=3180 解题思路: #include <cstdio> #include <algorithm> #include <cstring> #include <map> #include <iostream> using namespace std; const int MAXN=10010; const int MAXM=50010; struct Edge{ int to; int n

POJ 3180 The Cow Prom(强联通)

题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好,顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.为了跳这种圆舞,她们找了M(2≤M≤50000)条绳索.若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛.有的奶牛可能握有很多绳索,也有的奶牛可能一

POJ-3180 The Cow Prom(tarjan求强连通分量)

题目链接:http://poj.org/problem?id=3180 题目大意:求一个有向图的强连通分量 算法:求强连通分量首选tarjan算法 这里简单说一下tarjan的思路 时间戳是什么:在搜索时访问的最早时间 维护dfn[u]表示u的时间戳 low[u]表示u点所能回到的最早的祖先的时间戳 开一个栈,把搜索的点入栈.搜索时遇到已经搜过的点,取low[u]和dfn[v]的最小值,回溯时取low[u]和low[v]的最小值(标记上传)传到dfn[u]<=low[u]时表示已经回溯到最上面的

POJ 1236 Network of Schools (Tarjan + 缩点)

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 4871 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

USACO06JAN The Cow Prom /// tarjan求强联通分量 oj24219

题目大意: n个点 m条边的图 求大小大于1的强联通分量的个数 https://www.cnblogs.com/stxy-ferryman/p/7779347.html tarjan求完强联通分量并染色后 计算一下每种颜色的个数 就是每个强联通块的大小 #include <stdio.h> #include <cstring> #include <algorithm> #include <stack> using namespace std; const i

POJ 1236(强连通分量/Tarjan缩点)

传送门 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the

poj Popular Cows(tarjan +缩点)

Language: Default Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24384   Accepted: 10007 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