Cat cat rushes forward

Topic: Cat and cat rush forward
Title: As we all know, TT is a heavy cat lover, he has a magic magic cat.

One day, TT watched the cat game on station B. There are N cats in total, numbered 1, 2, 3, ..., N to play. After the game, the Up host will rank all the cats from front to back and distribute the favorite dried fish. Unfortunately, at this time, TT's electronic equipment was hit by the cosmic ray of de-smart, and even no Internet access at all, and naturally the final award ceremony was not seen.

Fortunately, the magic cat of TT recorded the results of each game. Now he wants to program to determine the sequence of the smallest lexicographic order. Please help him.

Input:
There are several groups of inputs, the first row in each group is two numbers N (1 <= N <= 500), M; where N represents the number of cats and cats, M represents the next M rows of input data. In the next M rows of data, each row also has two integers P1, P2 means that the cat with the number P1 wins the cat with the number P2.
Output:
Give a ranking that meets the requirements. There is a space between the numbers of cats and cats during the output, and there is no space after the last one!
Other instructions: the ranking that meets the conditions may not be unique. At this time, the team with the smaller number is required to be in front of the output; the input data is guaranteed to be correct, ie Enter the data to ensure that there is a ranking that meets the requirements.

Problem-solving ideas: This problem is a typical topological sort, and then find a minimum dictionary order;

Code:

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct node
{
    int next,to,w;
}e[100005];
int head[100005],n,m,tot=0,d[100005],k=0,in[100005],out[100005];
void add(int x,int y)
{
    e[++tot].to=y;
    e[tot].next=head[x];
    head[x]=tot;
    out[x]++;
    in[y]++;
}
void dfs()
{
    priority_queue<int,vector<int>,greater<int> > q;//维护一个最小堆
    for(int i=1;i<=n;i++)
    {
        if(in[i]==0)
        {
            q.push(i);
        }
    }
    while(!q.empty())//拓扑排序
    {
        int u=q.top();
        q.pop();
        d[k++]=u;
        for(int i=head[u];i;i=e[i].next)
        {
            int to=e[i].to;
            in[to]--;
            if(in[to]==0)
            {
                q.push(to);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        k=0,tot=0;
        memset(head,0,sizeof(head));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(d,0,sizeof(d));
        int p1,p2;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&p1,&p2);
            add(p1,p2);//加边
        }
        dfs();
        for(int i=0;i<k;i++)
        {
            if(i){printf(" ");}
            printf("%d",d[i]);
        }
        cout<<endl;
    }
}
PGZ
Published 34 original articles · praised 0 · visits 860

Guess you like

Origin blog.csdn.net/qq_43653717/article/details/105498597
Recommended