Entry to Topology Sorting

Source: Internet
Author: User

Entry to Topology Sorting

10305-Ordering Tasks

John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.

Input

The input will consist of several instances of the problem. Each instance begins with a line containing two integers,1 <= n <= 100AndM.NIs the number of tasks (numbered from1ToN) AndMIs the number of direct precedence relations between tasks. After this, there will beMLines with two integersIAndJ, Representing the fact that taskIMust be executed before taskJ. An instanceN = m = 0Will finish the input.

Output

For each instance, print a line with n integers representing the tasks in a possible order of execution. Sample Input
5 4
1 2
2 3
1 3
1 5
0 0
Sample Output
1 4 2 5 3
Bare, the output order is arbitrary, so the DFS practice can be obtained.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=110;int mp[maxn][maxn];int toposort[maxn];int vis[maxn];int n,m,cnt;void dfs(int x){    vis[x]=1;    for(int i=1;i<=n;i++)        if(!vis[i]&&mp[x][i]&&i!=x)   dfs(i);    toposort[cnt++]=x;}void solve(){    CLEAR(vis,0);    for(int i=1;i<=n;i++)       if(!vis[i])  dfs(i);}int main(){     int x,y;     std::ios::sync_with_stdio(false);     while(cin>>n>>m&&(n+m))     {         CLEAR(mp,0);         while(m--)         {             cin>>x>>y;             mp[x][y]=1;         }         cnt=0;         solve();         for(int i=cnt-1;i>=0;i--)            printf(i==0?"%d\n":"%d ",toposort[i]);     }     return 0;}
HDU 1285

Determine competition rankings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 13115 Accepted Submission (s): 5264


Problem Description has N teams (1 <= N <= 500), numbered 1, 2, 3 ,...., n. After the competition is over, the referee committee will rank all participating teams from the past to the next. However, the referee Committee cannot directly obtain the results of each team, but only knows the results of each competition, that is, P1 wins P2, which is represented by P1 and P2, and is ranked before P2. Now, compile the program to determine the ranking.
 
The Input has several groups. The first behavior in each group is N (1 <= N <= 500) and M. N indicates the number of groups, M indicates that there are M rows of input data. In the next row of M data, each row also has two integers P1. P2 indicates that the P1 team won the P2 team.
 
Output provides a qualified ranking. There is a space between the output team numbers, and there is no space behind the last one.

Other note: the qualified ranking may not be unique. In this case, the team with a small number must be in front of the output. The input data must be correct, that is, input data to ensure a qualified ranking.
 
Sample Input
4 31 22 34 3
 
Sample Output
1 2 4 3

The smallest Lexicographic Order ..

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=550;int mp[maxn][maxn],vis[maxn];int in[maxn],que[maxn];int n,m,k;void toposort(){    k=0;int i;    while(k<n)    {        for(i=1;i<=n;i++)        {            if(!vis[i]&&!in[i])            {                que[++k]=i;                vis[i]=1;                break;            }        }        for(int j=1;j<=n;j++)        {            if(mp[i][j])  --in[j];        }    }}void output(){    for(int i=1;i<=k;i++)       printf(i==k?"%d\n":"%d ",que[i]);}int main(){     int x,y;     std::ios::sync_with_stdio(false);     while(~scanf("%d%d",&n,&m))     {         CLEAR(in,0);         CLEAR(mp,0);         CLEAR(vis,0);         while(m--)         {             scanf("%d%d",&x,&y);             if(!mp[x][y])             {               mp[x][y]=1;               in[y]++;             }         }         toposort();         output();     }     return 0;}

HDU 3342

Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 4619 Accepted Submission (s): 2106


Problem DescriptionACM-DIY is a large QQ group where your excellent acmers get together. it is so harmonious that just like a big family. every day, daily "holy cows" like HH, hh, AC, ZT, KP, BF, Qinz and so on chat on-line to exchange their ideas. when someone has questions, warm-hearted cows like Lost will come to help. then the one being helped will call Lost "master", and Lost will have a n Ice "prentice ". by and by, there are using pairs of "master and prentice ". but then problem occurs: there are too processing masters and too processing prentices, how can we know whether it is legal or not?

We all know a master can have tables prentices and a prentice may have a lot of masters too, it's legal. nevertheless, some cows are not so honest, they hold illegal relationship. take HH and 3 xian for instant, HH is 3xian's master and, at the same time, 3 xian is HH's master, which is quite illegal! To avoid this, please help us to judge whether their relationship is legal or not.

Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.
InputThe input consists of several test cases. for each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested) (2 <= N, M <= 100 ). then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. the input is terminated by N = 0.
To make it simple, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.
OutputFor each test case, print in one line the judgement of the messy relationship.
If it is legal, output "YES", otherwise "NO ".
Sample Input
3 20 11 22 20 11 00 0
 
Sample Output
YESNO

Determine whether the topology can be sorted.

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=110;int mp[maxn][maxn],vis[maxn];int in[maxn];int n,m,k;int toposort(){    k=0;int i;    for(int t=0;t<n;t++)    {      for(i=0;i<n;i++)      {        if(!vis[i]&&!in[i])        {            vis[i]=1;            in[i]--;            k++;            for(int j=0;j<n;j++)                if(mp[i][j])  in[j]--;            break;        }      }    }//    cout<<"2333  "<<k<<endl;    return k==n?1:0;}int main(){     int x,y;     std::ios::sync_with_stdio(false);     while(cin>>n>>m&&(n+m))     {         CLEAR(mp,0);         CLEAR(vis,0);         CLEAR(in,0);         while(m--)         {             cin>>x>>y;             if(!mp[x][y])             {                 mp[x][y]=1;                 in[y]++;             }         }         if(toposort())   cout<<"YES"<<endl;         else   cout<<"NO"<<endl;     }     return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.