poj 2552 JOJ 2083 The Bottom of a Graph 图的强连通性

The Bottom of a Graph
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3066 Accepted: 1227

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2

Source

题目大意找SINK点,即从这个点出发所有到达的其他点,这些其他点也能到达这个点,即为SINK点
联想到图的连通性,可知在一个强连通分支中,如果独立于其他的强连通分支,这里面的点都是SINK点。而要是不独立与其他的强连通分支,则不是SINK点,即可。那么就要计算出所有的出度为0的点,当然是在缩图中,哈哈,缩图中!!!
以下为关键代码

 

void Tarjan(int u) //Tarjan

{

    int v;

    node[u].DFN=node[u].LOW=(++idx);

    instack[u]=true;

    stack[++top]=u;

    for(Edge*p=node[u].first; p; p=p->next)

    {

        v=p->adj;

        if(!node[v].DFN)

        {

            Tarjan(v);

            if(node[v].LOW<node[u].LOW)

                node[u].LOW=node[v].LOW;

        }

        else if(instack[v]&&node[v].DFN<node[u].LOW)

            node[u].LOW=node[v].DFN;

    }

    if(node[u].DFN==node[u].LOW)

    {

        b_cnt++;

        do

        {

            v=stack[top--];

            instack[v]=false;

            node[v].belongs=b_cnt;

        }

        while(u!=v);

    }

}

void Tarjan_SCC() //StronglyConnectedComponent

{

    int i,t1,t2;

    b_cnt=idx=top=0;//b_cnt 新图的点数

    for(i=1; i<=N; i++)

        if(!node[i].DFN)

            Tarjan(i);

    if(b_cnt<=1)

    {

        for(int i=1;i<=N;i++)

        {

             if(i!=1) printf(" ");

             printf("%d",i);

        }

        printf("/n");

    }

    else

    {

        Init2();

        memset(hash,-1,sizeof(hash));

        for(i=1; i<=N; i++)

        {

            flag[i]=true;

            for(Edge*p=node[i].first; p; p=p->next)

            if(node[i].belongs!=node[p->adj].belongs

               &&hash[node[i].belongs]!=node[p->adj].belongs)

            {

                    hash[node[i].belongs]=node[p->adj].belongs;

                    InsertEdge2(node[i].belongs,node[p->adj].belongs);

            }

            else

            if(node[i].belongs==node[p->adj].belongs&&!flag[p->adj])

            {

                    now[node[i].belongs].sum++;

                    flag[p->adj]=true;

            }

        }

                /*if(node[i].belongs!=node[p->adj].belongs)

                    out[node[i].belongs]++,in[node[p->adj].belongs]++;*/

        int k=0,kk=0;

        for(i=1; i<=b_cnt; i++)

        {

            k=0;

            for(Edge*p=now[i].first; p; p=p->next)//计算出度数

                                k++;

            if(k==0)  {f[i]=true;kk++;}

        }

        /*t1=t2=0;

        for(i=1; i<=b_cnt; i++)

        {

            if(!in[i]) t1++;

            if(!out[i])t2++;

        }*/

        if(kk==0)

        {

            printf("/n");

        }

        else

        {

            int m=0;

            for(int i=1;i<=N;i++)

            if(f[node[i].belongs])

            {

                if(m!=0) printf(" ");

                printf("%d",i);

                m++;

            }

            printf("/n");

        }

    }

}

 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值