zoukankan      html  css  js  c++  java
  • 武汉大学第十六届程序设计大赛(网络赛) F.A-maze-ing(Tarjan,缩点,求最长链)

                                 A-maze-ing

    题目传送门:滴滴滴

    Long long age, a wise but barbaric king made a plan to convert the
    arena to a maze so that he could get pleasure from watching his
    servant running the maze confusedly. The structure of the arena could
    be abstracted into a directed connected graph comprised of n (1 ≤ n ≤
    105 ) nodes and m (1 ≤ m ≤ 2 × 105 ) edges. The king had not decided
    where to set up the starting nodes and the end node. So the king
    proposed a requirement. Whichever two points u, v he chose, there
    existed a path from one point to the other(a path from u to v or from
    v to u). Moreover, the king hoped to improve the difficulty of the
    game, so the number of nodes in the maze should be as large as
    possible. You are only required to output the maximum number of nodes
    in the maze.

    Input

    There are two positive integers n and m in the first line. And then m
    lines follow, in each line there are two positive integers u and v,
    describing that an edge from node u to node v.

    Output

    Just output one integer, the maximum size of the maze

    Sample Input

    3 2
    1 2
    1 3

    Sample Output

    2



    题目意思: n个点m条边,求出最长的一条连接长度,比如样例3-1-2长为2
    比赛的时候慌的一匹,默默翻翻字典。后面比赛完,问了下学长emmm。
    思路:tarjan缩点,在重新建图,记录下每个连通图的点数,然后topsort求最长链就行了

    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    #include<ctime>  
    #include<cstdlib>  
    #include<algorithm>  
    #include<cmath>  
    #include<string>  
    #include<queue>  
    #include<vector>  
    #include<stack>  
    #include<list>  
    #include<map>  
    #include<set>  
    #define P pair<int ,int >  
    using namespace std;  
    const int maxn = 100000+5;  
    int Laxt[maxn],cnt,n,m,ans;  
    int times,dfn[maxn],low[maxn],scc[maxn],scc_cnt,sccnum[maxn],in[maxn],dis[maxn];  
    int stc[maxn],top,instc[maxn];  
    map<P,int> mp;  
    struct node  
    {  
        int from;  
        int to;  
        int next;  
        int w;  
    } edge[maxn*20],e[maxn*20] ;  
    void add(int u,int v,int w)  
    {  
        edge[++cnt].from=u;  
        edge[cnt].next=Laxt[u];  
        Laxt[u]=cnt;  
        edge[cnt].to=v;  
        edge[cnt].w=w;  
    }  
    int dfs(int u)  
    {  
        dfn[u]=low[u]=++times;  
        stc[++top]=u;  
        instc[u]=1;  
        for(int i=Laxt[u]; i; i=edge[i].next)  
        {  
            int v=edge[i].to;  
            if(!dfn[v])  
            {  
                dfs(v);  
                low[u]=min(low[u],low[v]);  
            }  
            else if(instc[v])  
            {  
                low[u]=min(low[u],low[v]);  
            }  
        }  
        if(dfn[u]==low[u])  
        {  
            scc_cnt++;  
            while(true)  
            {  
                int x=stc[top--];  
                scc[x]=scc_cnt;  
                sccnum[scc_cnt]++;  
                instc[x]=0;  
                if(x==u) break;  
            }  
        }  
    }  
    queue<int> q;  
    void tarjan()  
    {  
        for(int i=1; i<=n; i++)  
            if(!dfn[i]) dfs(i);  
        for(int i=1; i<=cnt; i++)  
        {  
            e[i]=edge[i];  
        }  
        memset(Laxt,0,sizeof(Laxt));  
        for(int i=1; i<=cnt; i++)  
        {  
            if(scc[e[i].from]!=scc[e[i].to]&&!mp[make_pair(scc[e[i].from],scc[e[i].to])])  
            {  
                in[scc[e[i].to]]++;  
                mp[make_pair(scc[e[i].from],scc[e[i].to])]=1;  
                add(scc[e[i].from],scc[e[i].to],1);  
            }  
        }  
        for(int i=1; i<=scc_cnt; i++)  
        {  
            if(in[i]==0)  
            {  
                q.push(i);  
                dis[i]=sccnum[i];  
            }  
            else  
            {  
                dis[i]=0;  
            }  
        }  
    }  
    void topsort()  
    {  
        int now;  
        while(!q.empty())  
        {  
            now=q.front();  
            q.pop();  
            for(int j=Laxt[now]; j; j=edge[j].next)  
            {  
                in[edge[j].to]--;  
                if(in[edge[j].to]==0)q.push(edge[j].to);  
                if(dis[now]+sccnum[edge[j].to]>dis[edge[j].to])  
                {  
                    dis[edge[j].to]=dis[now]+sccnum[edge[j].to];  
                }  
            }  
        }  
    }  
    int main()  
    {  
        scanf("%d %d",&n,&m);  
            int a,b;  
            for(int i=1; i<=m; i++)  
            {  
                scanf("%d %d",&a,&b);  
                add(a,b,1);  
            }  
            tarjan();  
            topsort();  
            int maxnode=0;  
            for(int i=1; i<=n; i++)  
            {  
                if(maxnode<dis[i])  
                {  
                    maxnode=dis[i];  
                }  
            }  
            printf("%d
    ",maxnode);  
    }  

    打比赛时候有思路是不可能的,这辈子不可能的,只可能补题来维持生活这样子。

    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

     
  • 相关阅读:
    京东css三角形做法
    css三角
    java 优势和劣势
    windows常用命令
    25 abstract 抽象
    24static 和final关键字之final
    面试题----static
    定义变量在前和定义变量在后
    java注解
    23static 和final关键词 之static 和运行顺序
  • 原文地址:https://www.cnblogs.com/MengX/p/9067019.html
Copyright © 2011-2022 走看看