POJ 3180 The Cow Prom(强联通)

题目大意:

约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞.

只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好,顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.为了跳这种圆舞,她们找了M(2≤M≤50000)条绳索.若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛.有的奶牛可能握有很多绳索,也有的奶牛可能一条绳索都没有握。比如说贝茜,她的圆舞跳得是否成功,可以这样检验:沿着她牵引的绳索,找到她牵引的奶牛,再沿着这只奶牛牵引的绳索,又找到一只被牵引的奶牛,如此下去,若最终能回到原位,则她的圆舞跳得成功,因为这一个环上的奶牛可以逆时针牵引而跳起旋转的圜舞.如果不能回到原位,那她的圆舞是不成功的.

如果两只成功跳圆舞的奶牛有绳索相连,那她们可以同属一个组合.

给出每一条绳索的描述,请找出,成功跳了圆舞的奶牛有多少个组合?

题目分析: 求的是有多个大于等于2的强联通分量;

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
usingnamespace std;
#define INF 0x7ffffff
#define maxn 10005
typedef longlong LL;
#define Min(a,b) (a<b?a:b)
#define MOD 1000000007
int m, n, Time, top, ans;
int Stack[maxn], dfn[maxn], low[maxn];
bool InStack[maxn];
vector<vector<int> > G;
void init()
{
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    ans = Time = top = 0;
    G.clear();
    G.resize(n+2);
}

void Tarjan(int u)
{
    dfn[u] = low[u] = ++Time;
    Stack[top++] = u;
    InStack[u] = true;
    int len = G[u].size(), v, k = 0;

    for(int i=0; i<len; i++)
    {
        v = G[u][i];
        if( !low[v] )
        {
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        elseif( InStack[v] )
            low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        do
        {
            v  = Stack[--top];
            k ++;
            InStack[u] = false;
        }while(u != v);
        if(k >= 2)
            ans ++;
    }
}

void solve()
{
    for(int i=1; i<=n; i++)
    {
        if(!low[i])
            Tarjan(i);
    }

    printf("%d\n", ans);

}

int main()
{
    while(scanf("%d %d",&n, &m) != EOF)
    {
        init();
        while(m --)
        {
            int a, b;
            scanf("%d %d",&a, &b);
            G[a].push_back(b);
        }
        solve();
    }
    return0;
}
时间: 2024-10-12 17:02:06

POJ 3180 The Cow Prom(强联通)的相关文章

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 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(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

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 2186 Popular cows(Kosaraju+强联通分量模板)

题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为牛C是红人,那么牛A也认为牛C是红人.求被其他所有牛认为是红牛的牛的总数. 解题思路:把所有牛看成顶点,把有序对(A,B)看成从A到B的有向边,那么题目就变成了求所有顶点都可到达的顶点的总数.我们可以得到一个结论,如果一个强连通分量里有一头牛被认为是红人,那么该强联通分量里的所有牛都是红人,这显然是

POJ 1236 Network of Schools(强联通缩点)

Network of Schools 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

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 2186 Popular Cows (强联通)

id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23819   Accepted: 9767 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <=

(连通图 缩点 强联通分支)Popular Cows -- poj --2186

http://poj.org/problem?id=2186 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 to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that co