USACO 06JAN 牛的舞会 洛谷2863

题目描述

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.

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

只有奶牛才能表演这种圆舞.圆舞需要一些绳索和一个圆形的水池.奶牛们围在池边站好, 顺时针顺序由1到N编号.每只奶牛都面对水池,这样她就能看到其他的每一只奶牛.

为了跳这种圆舞,她们找了 M(2

#include<iostream>
#include<cstdio>

using namespace std;
const int MAXN = 10005;

struct Edge{
    int nxt,to;
}edge[MAXN*5];

int n,m,head[MAXN],low[MAXN],dfn[MAXN];
int cnt,num,ans,top,stack[MAXN];
bool vis[MAXN];

inline void add(int bg,int ed){
    edge[++cnt].to=ed;
    edge[cnt].nxt=head[bg];
    head[bg]=cnt;
}

inline void tarjan(int u){
    vis[u]=1;
    stack[++top]=u;
    dfn[u]=low[u]=++num;
    for(int i=head[u];i;i=edge[i].nxt){
        int v=edge[i].to;
        if(!dfn[v]){
            tarjan(v);
            low[u]=min(low[u],low[v]); 
        }
        else if(vis[v])
            low[u]=min(low[u],dfn[v]); 
    }
    if(low[u]==dfn[u]){
        if(stack[top]!=u)
            ans++;
        vis[u]=0;
        while(stack[top]!=u){
            vis[stack[top]]=0;
            top--;
        }
        top--;
    }
}

int main(){
    scanf("%d%d",&n,&m);
    for(register int i=1;i<=m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
    }
    for(register int i=1;i<=n;i++)
        if(!dfn[i]) tarjan(i);
    printf("%d",ans);
    return 0;
}
posted @ 2018-04-25 22:53  Monster_Qi  阅读(118)  评论(0编辑  收藏  举报