1007 The Cow Prom

Problem 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 <br> <br>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

Analyze

有向图强连通分量

tarjan's algorithm   

Source Code

#include<iostream>
#include<stack>
#include<vector>
#define N 10001
using namespace std;
int dfn[N];//深度优先搜索访问次序
int low[N];//能追溯到的最早的次序
int vis[N];//记录是否遍历过
int cnt;//索引号
int sig;//强连通分量个数
stack<int> S;//targan算法中的栈
vector<int> Edge[N];//邻接表

void Tarjan(int u)//targan算法
{
    vis[u]=1;
    dfn[u]=low[u]=++cnt;
    S.push(u);
    for(int i=0;i<Edge[u].size();i++)
    {
        int v=Edge[u][i];
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
            low[u]=min(low[u],low[v]);
    }
    if(dfn[u]==low[u])
    {
        int now,sum=0;
        while(1)
        {
            sum++;
            now=S.top();
            S.pop();
            vis[now]=0;
            if(now==u)//保证sum在每个强连通分量求出时只自增一次
                break;
        }
        if(sum>1)
            sig++;
    }
}

int main()
{
    int n,m;
    cin>>n>>m;
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        Edge[a].push_back(b);//有向图
    }
    for(int i=1;i<=n;i++)
     {
         if(!dfn[i]) Tarjan(i);
     }
    cout<<sig<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdau20171989/article/details/81218433