6467 Strahler Order
In geology, a river system can be represented as a directed graph. Each river segment is an edge; with
the edge pointing the same way the water flows. Nodes are either the source of a river segment (for
example, a lake or spring), where river segments merge or diverge, or the mouth of the river.
uva Live 6467 Strahler Order(拓扑排序)
Note: The number in a box is the order. The number in a circle is the node number.
The Strahler order of a river system is computed as follows.
• The order of each source node is 1.
• For every other node, let i be the highest order of all its upstream nodes. If just one upstream
node has order i, then this node also has order i. If two or more upstream nodes have order i,
then this node has order i + 1.
The order of the entire river system is the order of the mouth node. In this problem, the river
system will have just one mouth. In the picture above, the Strahler order is three (3).
You must write a program to determine the order of a given river system.
The actual river with the highest order is the Amazon, with order 12. The highest in the U.S. is
the M ississippi, with order 10.Node M is the mouth of the river. It has no outgoing edges.
Input
The first line of input contains a single integer K, (1 ≤ K ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of multiple lines of input. The first line of each data set contains three
positive integers, K, M and P (2 ≤ m ≤ 1000). K is the data set number. M is the number of nodes
in the graph and P is the number of edges. The first line is followed by P lines, each describing an edge
of the graph. The line will contain two positive integers, A and B, indicating that water flows from
node A to node B (1 ≤ A, B ≤ M). Node M is the mouth of the river. It has no outgoing edges.
Output
For each data set there is a single line of output. The line consists of the data set number, a single
space and the order of the river system.
Sample Input
1
1 7 8
1 3
2 3
6 4
3 4
3 5
6 7
5 7
4 7
Sample Output
1 3

题意:有n个点m条边,入度为0的标为1,如果有2个或以上的等级为i的点是某点的前驱,那这个点的等级就是i+1,如果只有一个等级为i的是他的前驱,这个点就是i,求这一堆点中最大的那个等级呀
代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <map>
#include<stack>
#include <vector>
using namespace std;
#define ll long long
#define eps 0.001
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define memset(a,b) memset(a,b,sizeof(a))
#define MAX 1005
queue <int> q;
int mapp[MAX][MAX];
int in[MAX];
void init()
{
    while(q.size()>0)
        q.pop();
}
int toposort(int n)
{
    int node[MAX];
    int vis[MAX];
    memset(vis,0);
    memset(node,0);
    for(int i=1;i<=n;i++){
        if(in[i]==0){
            node[i]=1;
            q.push(i);
        }
    }
    while(q.size()>0){
        int flag=q.front();
        q.pop();
        if(vis[flag]==1)
            node[flag]++;
        for(int i=1;i<=n;i++){
            if(mapp[flag][i]==1){
                if(node[flag]==node[i])       //代表之前连接过一个node【s】的或者是两个以上node【s】-1的
                    vis[i]=1;
                if(node[flag]>node[i])
                    vis[i]=0,node[i]=node[flag];      //一定要vis【i】=0,因为这一句WA了,假设某个点的前驱等级为2,2,3  
                    //在遍历完前两个2的后,vis【i】=1了,此时再遍历那个3的时候,node【i】=3,这个点入队后,
                    //由于vis【i】=1,node数组又变成4了,就不对惹
                in[i]--;
            }
            else{
                continue;
            }
            if(in[i]==0)
                q.push(i);
        }
    }
    int ans=-1;
    for(int i=1;i<=n;i++)
        ans=max(ans,node[i]);//cout<<node[i]<<endl;
    return ans;
}
int main()
{
    int t,num,m,p;
    cin>>t;
    while(t--){
        int a,b;
        memset(in,0);
        memset(mapp,0);
        cin>>num>>m>>p;
        init();
        for(int i=0;i<p;i++){
            cin>>a>>b;
            mapp[a][b]=1;
            in[b]++;
        }
        int ans=toposort(m);
        cout<<num<<" "<<ans<<endl;

    }

    return 0;
}

相关文章:

  • 2021-05-20
  • 2021-06-20
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
猜你喜欢
  • 2021-10-04
  • 2021-06-23
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-11-12
相关资源
相似解决方案