• code vs1506传话(塔尖)+tarjan图文详解


    1506 传话

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 白银 Silver
     
    题目描述 Description

    一个朋友网络,如果a认识b,那么如果a第一次收到某个消息,那么会把这个消息传给b,以及所有a认识的人。

    如果a认识b,b不一定认识a。

    所有人从1到n编号,给出所有“认识”关系,问如果i发布一条新消息,那么会不会经过若干次传话后,这个消息传回给了i,1<=i<=n。

    输入描述 Input Description

    第一行是n和m,表示人数和认识关系数。

    接下来的m行,每行两个数a和b,表示a认识b。1<=a, b<=n。认识关系可能会重复给出,但一行的两个数不会相同。

    输出描述 Output Description

    一共n行,每行一个字符T或F。第i行如果是T,表示i发出一条新消息会传回给i;如果是F,表示i发出一条新消息不会传回给i。

     

    样例输入 Sample Input

    4 6

    1 2

    2 3

    4 1

    3 1

    1 3

    2 3

    样例输出 Sample Output

    T

    T

    T

    F

    数据范围及提示 Data Size & Hint

    n<=1000

    1<=a, b<=n

    分类标签 Tags 

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,m,cnt,l;
    int dfn[1001],low[1001],front[1001];
    int s[1001];
    bool v[1001];
    bool ans[1001];
    int cc[1001];
    int top=1;
    struct node
    {
        int next,to;
    }e[1000001];
    void add(int x,int y,int d)
    {
        e[d].to=y;
        e[d].next=front[x];
        front[x]=d;
    }
    void push(int x)
    {
        s[top++]=x;
        v[x]=true;
    }
    void pop(int ll)
    {
        top--;
        v[s[top]]=false;
        cc[ll]=s[top];
    }
    void tarjer(int k)//目前根节点 
    {
            dfn[k]=low[k]=++cnt;
            push(k);
        for(int i=front[k];i;i=e[i].next)//枚举与他相连的每一条边 
        {
            int t=e[i].to;//指向的点 
            if(!dfn[t])  
            {
              tarjer(t);
              low[k]=min(low[k],low[t]);
            }
            else
            {
                if(v[t])
                low[k]=min(low[k],dfn[t]);
            }
        }    
        if(low[k]==dfn[k])
            {
                l=0;
                while(dfn[s[top-1]]!=dfn[k])
                 {
                    l++;
                    pop(l);
                }
                pop(++l);
            }
            if(l>1)
            {
                for(int i=1;i<=l;i++)
                 ans[cc[i]]=true;
            }        
    }
    int main()
    {
        freopen("message","r",stdin);
        freopen("message.out","w",stdout);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y,i);
        }
        for(int i=1;i<=n;i++)
        if(!dfn[i])
        tarjer(i);
        for(int i=1;i<=n;i++)
         if(ans[i]) printf("T
    ");
         else printf("F
    ");
    }

    塔尖正解,这个题其实是塔尖的模板;

    tarjan是一种求环的方法;

    Tarjan算法

    一种由Robert Tarjan提出的求解有向图强连通分量的线性时间的算法。 
    Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。 
    定义dfn(u)为节点u搜索的次序编号,low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。 
    当dfn(u)=low(u)时,以u为根的搜索子树上所有节点是一个强连通分量。

    算法图解: 
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述
    这里写图片描述

    伪代码:

    tarjan(u){
        DFN[u]=Low[u]=++Index   //为节点u设定次序编号和Low初值
        Stack.push(u)           //将节点u压入栈中
    
        foreach(u,v) in E       //枚举每一条边
        if(v is not visted) //如果节点v未被访问过
            tarjan(v)       //继续向下找
            Low[u]=min(Low[u],Low[v])
        else if(v in S)     //如果节点v还在栈内
            Low[u]=min(Low[u],DFN[v])
        if(DFN[u]==Low[u])  //如果节点u是强连通分量的根
        repeat
            v=S.pop//将v退栈,为该强连通分量中一个顶点
            print v
        until(u==v)
    }

    可以发现,运行Tarjan算法的过程中,每个顶点都被访问了一次,且只进出了一次堆栈,每条边也只被访问了一次,所以该算法的时间复杂度为O(N+M)。N为点数,M为边数。

    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    modis数据处理的坑(MOD02,mod03,mod04,MCD19A2)
    TensorFlow安装笔记(CPU版)
    mod35云掩膜产品用法
    ERA-Interim数据学习
    收集空气质量数据走的路
    GEE windows 环境配置
    Spatiotemporal continuous estimates of PM2.5 concentrations in China, 2000–2016: A machine learning method with inputs from satellites, chemical transport model, and ground observations
    Exploiting ConvNet Diversity for Flooding Identification
    Missing Data Reconstruction in Remote Sensing Image With a Unified Spatial–Temporal–Spectral Deep Convolutional Neural Network(缺失数据补全,时空谱网络)
    多线程
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/6051104.html
Copyright © 2020-2023  润新知