The Cow Prom
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1451   Accepted: 922

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

 
典型的阅读理解题,看了好久的题其实就是求有向图强连通分量结点数>=2的个数。虽然简单,但是第一次确敲错了好几个小地方,敲代码时一定要专心,下面列出错误
/*
ID: LinKArftc
PROG: 3180.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ; struct Edge {
int v, next;
} edge[maxm]; int head[maxn], tot; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot ++;
} int n, m;
int dfn[maxn], low[maxn];
bool ins[maxn];
int scc, Time;
stack <int> st;
vector <int> vec[maxn]; void tarjan(int u) {
int v;
dfn[u] = low[u] = ++ Time;
st.push(u);
ins[u] = true;
for (int i = head[u]; i + ; i = edge[i].next) {
v = edge[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[v], low[u]);
} else if (ins[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {//刚开始写成dfn[u] == low[v]了
scc ++;
do {
v = st.top();
st.pop();
ins[v] = false;
vec[scc].push_back(v);
} while (u != v);
}
} int main() { //input;
int u, v;
while (~scanf("%d %d", &n, &m)) {
init();
for (int i = ; i <= m; i ++) {//刚开始写成i<=n了
scanf("%d %d", &u, &v);
addedge(u, v);
}
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
while (!st.empty()) st.pop();
for (int i = ; i <= n; i ++) vec[i].clear();
scc = ;
Time = ;
for (int i = ; i <= n; i ++) {
if (!dfn[i]) tarjan(i);
}
int ans = ;
for (int i = ; i <= scc; i ++) {
if (vec[i].size() >= ) ans ++;
}
printf("%d\n", ans);
} return ;
}

POJ3180(有向图强连通分量结点数>=2的个数)的更多相关文章

  1. 有向图强连通分量的Tarjan算法和Kosaraju算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  2. 有向图强连通分量Tarjan算法

    在https://www.byvoid.com/zhs/blog/scc-tarjan中关于Tarjan算法的描述非常好,转述如下: 首先解释几个概念: 有向图强连通分量:在有向图G中,如果两个顶点间 ...

  3. 有向图强连通分量的Tarjan算法

    有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...

  4. 有向图强连通分量 Tarjan算法

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...

  5. 【转】有向图强连通分量的Tarjan算法

    原文地址:https://www.byvoid.com/blog/scc-tarjan/ [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly con ...

  6. 图的连通性:有向图强连通分量-Tarjan算法

    参考资料:http://blog.csdn.net/lezg_bkbj/article/details/11538359 上面的资料,把强连通讲的很好很清楚,值得学习. 在一个有向图G中,若两顶点间至 ...

  7. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

  8. 有向图强连通分量的Tarjan算法及模板

    [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强联通(strongly connected),如果有向图G的每两个顶点都强联通,称有向图G是一个强联通图.非强联通图有向 ...

  9. hdu1269(有向图强连通分量)

    hdu1269 题意 判断对于任意两点是否都可以互相到达(判断有向图强连通分量个数是否为 1 ). 分析 Tarjan 算法实现. code #include<bits/stdc++.h> ...

随机推荐

  1. Git创建project

    1.登录创建新仓库 命名 2.https://gitforwindows.org/  下载git的windows客户端,输入git查看是否成功 3.创建文件夹,写内容并查看,和linux指令一样 4. ...

  2. The Erdös-Straus Conjecture 题解

    题面 Description The Brocard Erdös-Straus conjecture is that for any integern > 2 , there are posit ...

  3. Cassandra 数据库设计

    Cassandra 2.* CQL3.1 最近更新:2015-10-30 索引的设计 在Cassandra中经常会发现,索引不够用,不好用,各种不强大. 比如,我关注的人的需求uid + follow ...

  4. java面笔准备

    这套面试题主要目的是帮助那些还没有java软件开发实际工作经验,而正在努力寻找java软件开发工作的朋友在笔试时更好地赢得笔试和面试.由于这套面试题涉及的范围很泛,很广,很杂,大家不可能一天两天就看完 ...

  5. java对数组的操作

    1 拷贝数组 数组全拷贝 数组定位拷贝 2 判断数组是否相等(每个元素都对应相等) 3 数组和集合的相互转化 import java.util.Arrays; import java.util.Lis ...

  6. PokeCats开发者日志(二)

      现在是PokeCats游戏开发的第四天的上午,来记录一下昨天做的事情吧. day3   day3主要是添加音效和优化界面,本以为添加个音效1~2个小时就够了吧,没想到贼不顺,弄了一个下午才搞好. ...

  7. 【UML】状态图介绍

    1.Statechart Diagram 即状态图,主要用于描述一个对象在其生存期间的动态行为,表现为一个对象所经历的状态序列.引起状态转移的事件(Event).因状态转移而伴随的动作(Action) ...

  8. Codeforces Round #525 (Div. 2)E. Ehab and a component choosing problem

    E. Ehab and a component choosing problem 题目链接:https://codeforces.com/contest/1088/problem/E 题意: 给出一个 ...

  9. mysql修改表中某个字段的默认值

    Mysql中用SQL增加.删除字段,修改字段名.字段类型.注释,调整字段顺序总结   在网站重构中,通常会进行数据结构的修改,所以添加,删除,增加mysql表的字段是难免的,有时为了方便,还会增加修改 ...

  10. WebOS系列-了解Wekbit【邓侃】

    注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/02/02/1660634.html  作者:李嘉昱 这是Kan老大的We ...