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:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

     
  • 相关阅读:
    java程序后台报错java.net.SocketException: Too many open files
    linux中,查看某个命令是来自哪个RPM包或者是通过哪个RPM包安装的
    Oracle卸载之linux快速卸载rac脚本-一键卸载
    40个DBA日常维护的SQL脚本
    Oracle SQL开发 之 Select语句完整的执行顺序
    Oracle开发 之 主-外键约束FK及约束的修改
    drop user 报错ora-00604
    oracle Bug 4287115(ora-12083)
    Ora-1157 ora-1110错误解决案例一枚
    rac库grid目录权限(6751)导致数据库宕机案例 此方法仅用于紧急救助
  • 原文地址:https://www.cnblogs.com/MengX/p/9067019.html
Copyright © 2011-2022 走看看