Luo Gu P2341 popular cattle point reduction Tarjan

Topic links: https://www.luogu.org/problem/P2341

Meaning of the questions: A like B, B like C, then A like C, everyone must like themselves, inquired of the owner was like how many people

Input Format: n, m are the number and the total number of people like the relationship between the next two lines each m numbers a, b representative of a like b

Analysis: Input formats can be converted readily occur to FIG topics do, in the FIG., A represents A like B B up, all strongly connected components which points are reachable to each other, and as long as there is a strongly connected component of a B up to point strongly connected component, then up to a point B in all, we took advantage of the strongly connected component Tarjan algorithm calculates and records the number of points in each strongly connected components, followed by reduction point. After shrinking point we continue to observe, we found that if the degree of point 0 is only one, then it must be a connected graph, and all other points up to this point, and that point could not reach the other point, then the point (condensation point after point in the strongly connected components) number is the answer, and if two or more points out of the 0 degree, this figure is not a connected graph, the answer is 0.

Direct compression set point algorithm Tarjan template, and the last recorded number of degrees to each point to a record.

#include <bits / STDC ++ H.>
 #define MAXN 10001
 the using  namespace STD; 
Vector < int > G [MAXN]; 
Stack < int > S;
 int n-, m;
 int DFN [MAXN], VIS [MAXN], Low [ MAXN], color [MAXN], num [MAXN], Colornum = 0 , res [MAXN], CNT;
 // number of points in each color record num, res recording each point of the 
void Paint ( int X) 
{ 
    s.pop (); 
    Color [X] = Colornum; 
    NUM [Colornum] ++ ; 
    VIS [X] = to false ; 
}
void tarjan(int x)
{
    dfn[x]=low[x]=++cnt;
    s.push(x);
    vis[x]=true;
    for(int i=0;i<G[x].size();i++)
    {
        int q=G[x][i];
        if (!dfn[q])
        {
            tarjan(q);
            low[x]=min(low[x],low[q]);
        }
        else if (vis[q]) low[x]=min(low[x],dfn[q]);
    }
    if (low[x]==dfn[x])
    {
        colornum++;
        while(s.top()!=x)
        {
            int t=s.top();
            paint(t);
        }
        paint(x);
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
    }
    for(int i=1;i<=n;i++)
    {
        if (!dfn[i]) tarjan(i);
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<G[i].size();j++){
            if(color[i]!=color[G[i][j]]){
                res[color[i]]++;
            }
        }
    }
    for(int i=1;i<=colornum;i++){
        if(ans==0&&res[i]==0){
            ans=num[i];
        }
        else if(res[i]==0){
            ans=0;break;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/qingjiuling/p/11313561.html