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 next;
}edge[MAXM];

int head[MAXN];
int tot;

void addEdge(int u,int v){
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void init(){
    memset(head,-1,sizeof(head));
    tot=0;
}

int LOW[MAXN],DFS[MAXN],Stack[MAXN],Num[MAXN],Belong[MAXN];
bool Instack[MAXN];
int Index;
int scc;
int top;

void Targan(int u){
    LOW[u]=DFS[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;

    int v;
    for(int i=head[u];i!=-1;i=edge[i].next){
        v=edge[i].to;
        if(!DFS[v]){
            Targan(v);
            if(LOW[u]>LOW[v])
                LOW[u]=LOW[v];
        }else if(Instack[v]&&LOW[u]>DFS[v]){
                LOW[u]=DFS[v];
        }
    }

    if(LOW[u]==DFS[u]){
        scc++;

        do{
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
            Num[scc]++;
        }while(v!=u);
    }
}

void Solve(int n){
    memset(Instack,false,sizeof(Instack));
    memset(DFS,0,sizeof(DFS));
    memset(Num,0,sizeof(Num));

    Index=scc=top=0;
    for(int i=1;i<=n;i++)
        if(!DFS[i])
        Targan(i);
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    init();
    for(int i=0;i<m;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        addEdge(u,v);
    }
    Solve(n);
    int ans=0;
    for(int i=1;i<=scc;i++)
        if(Num[i]>=2)
        ans++;
    cout<<ans<<endl;
}
时间: 2024-10-20 13:48:08

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+缩点 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(强联通)

题目大意: 约翰的N(2≤N≤10000)只奶牛非常兴奋,因为这是舞会之夜!她们穿上礼服和新鞋子,别上鲜花,她们要表演圆舞. 只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好,顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.为了跳这种圆舞,她们找了M(2≤M≤50000)条绳索.若干只奶牛的蹄上握着绳索的一端,绳索沿顺时针方绕过水池,另一端则捆在另一些奶牛身上.这样,一些奶牛就可以牵引另一些奶牛.有的奶牛可能握有很多绳索,也有的奶牛可能一

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 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1132   Accepted: 713 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. T

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c

POJ 3189 Steady Cow Assignment(最大流)

POJ 3189 Steady Cow Assignment 题目链接 题意:一些牛,每个牛心目中都有一个牛棚排名,然后给定每个牛棚容量,要求分配这些牛给牛棚,使得所有牛对牛棚的排名差距尽量小 思路:这种题的标准解法都是二分一个差值,枚举下界确定上界,然后建图判断,这题就利用最大流进行判断,值得一提的是dinic的效率加了减枝还是是卡着时间过的,这题理论上用sap或者二分图多重匹配会更好 代码: #include <cstdio> #include <cstring> #inclu

POJ 3268 Silver Cow Party(SPFA)

Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X