Description
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. 

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. 

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. 

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 : Two space-separated integers: N and M 

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

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

Sample Input


Sample Output


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**| \ \********/ \ \******/ \ 4____/ / \_______/
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(即至少两个)才行
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
#include<map>
using namespace std;
#define N 100006
int n,m;
int tot; int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N]; struct Node
{
int from;
int to;
int next;
}edge[N<<];
void init()
{
tot=;
scc=;
tt=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
}
void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
}
void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
} for(int i=;i<=n;i++)
{
if(dfn[i]==-)
{
tarjan(i);
}
}
//printf("%d\n",scc);
map<int,int> mp;
for(int i=;i<=n;i++){
mp[col[i]]++;
}
int ans=;
for(int i=;i<=scc;i++){
if(mp[i]>) ans++;
}
printf("%d\n",ans);
}
return ;
}

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. poj 3744 Scout YYF I (矩阵)

    Description YYF -p. Here is the task, given the place of each mine, please calculate the probality t ...

  2. PyQt4--QPushButton阵列

    # -*- coding: utf-8 -*- from PyQt4.QtCore import * from PyQt4.QtGui import * import sys import funct ...

  3. Codeforces Round #143 (Div. 2) (ABCD 思维场)

    题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test: ...

  4. JMeter脚本参数化和断言设置( CSV Data Set Config )

    用Badboy录制了Jmeter的脚本,用Jmeter打开后形成了原始的脚本.但是在实际应用中,为了增强脚本的多样性,就要使脚本参数化.这里我以登录为例,参数化用户账号与用户密码.  图1 :原始脚本 ...

  5. C++11 多线程 基础

    C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用 ...

  6. java二维不定长数组测试

    package foxe; import javax.swing.JEditorPane;import javax.swing.JFrame; /** * @author fooxe * * @see ...

  7. linux oracle 设置随系统自动启动数据库实例和监听

    在root账户下修改/etc/oratab 文件: # vi /etc/oratab 找到orcl=/db/app/oracle/product/11.1.0/db_1 :N这一行 改为: orcl= ...

  8. 系统提供的UIImagePickerController

    1.从系统相册中读取 /* 判断选择的读取类型是否支持 UIImagePickerControllerSourceTypePhotoLibrary,普通相册 UIImagePickerControll ...

  9. iOS_SN_BlueTooth (二)iOS 连接外设的代码实现

    原文:http://www.cocoachina.com/ios/20150917/13456.html?utm_source=tuicool&utm_medium=referral 上一篇文 ...

  10. iOS_SN_Socket - AsyncSocket

    转载文章,原地址:http://yimouleng.com/2015/02/04/Socket-AsyncSocket/ 一.前言 公司的项目用到了Socket编程,之前在学习的过程当中,用到的更多的 ...