Description
  1. The N ( <= N <= ,) 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.
  2.  
  3. 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 ..N. Each cow faces the tank so she can see the other dancers.
  4.  
  5. They then acquire a total of M ( <= M <= ,) 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.
  6.  
  7. 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,
  8. if she dances the other way, she must instantly pull the entire group counterclockwise (anti-clockwise in British English).
  9.  
  10. 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.
  11.  
  12. 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

  1. Line : Two space-separated integers: N and M
  2.  
  3. Lines ..M+: 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

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

Sample Input

  1.  

Sample Output

  1.  

Hint

  1. Explanation of the sample:
  2.  
  3. ASCII art for Round Dancing is challenging. Nevertheless, here is a representation of the cows around the stock tank:
  4. _1___
  5.  
  6. /**** \
  7.  
  8. 5 /****** 2
  9.  
  10. / /**TANK**|
  11.  
  12. \ \********/
  13.  
  14. \ \******/
  15.  
  16. \ 4____/ /
  17.  
  18. \_______/
  19. Cows , , and are properly connected and form a complete Round Dance group. Cows and 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

 
用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<<];
  26. void init()
  27. {
  28. tot=;
  29. scc=;
  30. tt=;
  31. memset(head,-,sizeof(head));
  32. memset(dfn,-,sizeof(dfn));
  33. memset(low,,sizeof(low));
  34. memset(vis,,sizeof(vis));
  35. memset(col,,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]=;
  48. s.push(u);
  49. int cnt=;
  50. for(int i=head[u];i!=-;i=edge[i].next)
  51. {
  52. int v=edge[i].to;
  53. if(dfn[v]==-)
  54. {
  55. // sum++;
  56. tarjan(v);
  57. low[u]=min(low[u],low[v]);
  58. }
  59. else if(vis[v]==)
  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]=;
  71. }while(x!=u);
  72. }
  73. }
  74. int main()
  75. {
  76. while(scanf("%d%d",&n,&m)==)
  77. {
  78. init();
  79. for(int i=;i<m;i++){
  80. int x,y;
  81. scanf("%d%d",&x,&y);
  82. add(x,y);
  83. }
  84.  
  85. for(int i=;i<=n;i++)
  86. {
  87. if(dfn[i]==-)
  88. {
  89. tarjan(i);
  90. }
  91. }
  92. //printf("%d\n",scc);
  93. map<int,int> mp;
  94. for(int i=;i<=n;i++){
  95. mp[col[i]]++;
  96. }
  97. int ans=;
  98. for(int i=;i<=scc;i++){
  99. if(mp[i]>) ans++;
  100. }
  101. printf("%d\n",ans);
  102. }
  103. return ;
  104. }

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

  1. POJ 3180 The cow Prom Tarjan基础题

    题目用google翻译实在看不懂 其实题目意思如下 给一个有向图,求点个数大于1的强联通分量个数 #include<cstdio> #include<algorithm> #i ...

  2. poj 3180 The Cow Prom(强联通分量)

    http://poj.org/problem?id=3180 The Cow Prom Time Limit: 1000MS   Memory Limit: 65536K Total Submissi ...

  3. POJ 3180 The Cow Prom(SCC)

    [题目链接] http://poj.org/problem?id=3180 [题目大意] N头牛,M条有向绳子,能组成几个歌舞团?要求顺时针逆时针都能带动舞团内所有牛. [题解] 等价于求点数大于1的 ...

  4. LuoGu-P2863牛的舞会The Cow Prom[tarjan 缩点模板]

    传送门:https://www.luogu.org/problemnew/show/P2863 思路:tarjan模板题,之前会的tarjan,一直想学缩点到底是什么操作,发现就是把同组的放在一个数组 ...

  5. POJ 3180 The Cow Prom(强联通)

    题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.           只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的 ...

  6. [poj] 3180 the cow prom

    原题 这是一道强连通分量板子题. 我们只用输出点数大于1的强连通分量的个数! #include<cstdio> #include<algorithm> #include< ...

  7. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

  8. [USACO06JAN]牛的舞会The Cow Prom Tarjan

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  9. luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

随机推荐

  1. iPhone 5,6,6 plus 尺寸

  2. 未知宽高div水平垂直居中3种方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...

  3. 具体解说Android的图片下载框架UniversialImageLoader之磁盘缓存的扩展(二)

    相对于第一篇来讲,这里讲的是磁盘缓存的延续.在这里我们主要是关注四个类.各自是DiskLruCache.LruDiskCache.StrictLineReader以及工具类Util. 接下来逐一的对它 ...

  4. c#中关于virtual,override和new的理解

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  5. jQuery制作焦点图(轮播图)

    焦点图(轮播图) 案例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  6. openssl 证书请求和自签名命令req详解

    1.密钥.证书请求.证书概要说明 在证书申请签发过程中,客户端涉及到密钥.证书请求.证书这几个概念,初学者可能会搞不清楚三者的关系,网上有的根据后缀名来区分三者,更让人一头雾水.我们以申请证书的流程说 ...

  7. Struts2上传文件

    jsp: <form action="file_upload.action" method="post" enctype="multipart/ ...

  8. CodeSmith使用总结--创建一个基础模板

    问:为什么要用CodeSmith? 答曰:因为我懒的写. Codesmith是一款非常不错的懒人工具,我也经常会用到,因为它在“重复代码”方面能够节省我们很多时间,并且解除了我们重复繁琐并且乏味的“码 ...

  9. .NET 4.0 兼容 .NET 2.0 的方法

    使用.net开发桌面应用,广大亲门最头疼的莫过于客户端部署的问题.基于.net 2.0 的winfrom程序因为 Framework 的分发包大小为20M左右还好解决,不幸的是如果项目中使用了Wcf, ...

  10. 《第一行代码》学习笔记13-UI(2)

    1.EditText:程序和用户进行交互的重要控件,允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理. 2.Android控件使用的一般规律:给控件定义一个id->指定下控件的宽 ...