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. NetAnalyzer笔记 之 一. 开篇语

    [创建时间:2015-08-26 22:00:12] NetAnalyzer下载地址 第一次写技术相关的博客,不足之处还请担待并告知. 在开始之前,先简单介绍一下NetAnalyzer, NetAna ...

  2. 几道php基础面试题

    前言 昨晚实验室一师弟在微薄上@我,给我发了几道php的基础面试题,这里把我写的答案贴出来 题目 (1)写一个函数获取URL的文件后缀,例如“http://www.feiyan.info/test.p ...

  3. [Javascript] lodash: memoize() to improve the profermence

    Link: https://lodash.com/docs#memoize Example: .service('UserPresenter', function(UserConstants){ va ...

  4. 『转载』Debussy快速上手(Verdi相似)

    『转载』Debussy快速上手(Verdi相似) Debussy 是NOVAS Software, Inc(思源科技)发展的HDL Debug & Analysis tool,这套软体主要不是 ...

  5. Temporary ASP.NET Files 文件夹中保存的是什么内容?[转]

    转自:http://www.cnblogs.com/suiqirui19872005/archive/2007/05/14/746320.html ASP.NET 页面请求的处理过程需要使用一些临时文 ...

  6. JavaScript操作剪贴板(转)

    IE是第一个支持与剪贴板相关的事件,以及通过JavaScript访问剪贴板数据的浏览器.IE的实现成为了某种标准,不仅Safari 2.Chrome和Firefox 3也都支持类似的事件和剪贴板(Op ...

  7. (转).net开发者对android第二周的学习体会

    这一周相对没有春节时这么闲了,白天也比较多的工作要做,每天晚上又要被我三岁的女儿折腾到十点, 实在没有多少时间学习.在前一周的基础上,这周我试着自己练习写了一个个人管理的android的程序,主要实现 ...

  8. iOS_SN_基于AFNetworking3.0网络封装

    转发文章,原地址:http://www.henishuo.com/base-on-afnetworking3-0-wrapper/?utm_source=tuicool&utm_medium= ...

  9. Java图片工具类,完成图片的截取和任意缩放

    package com.common.util; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Renderin ...

  10. 在mac平台运行debug.exe

    最近准备学习操作系统,想先复习一下汇编语言.因为用的是mac,而看的汇编教材(<汇编语言>王爽)使用到DOS下的debug,在网上搜了一圈发现,mac 也可以模拟运行debug. 先到网上 ...