sdut3037--let the leader go first (topological sort)

Source: Internet
Author: User

Let the leader go first. Time limit:2000ms Memory limit:65536k Title Description

After the end of the company in a fire pull, everyone run, or run will be killed. Please do not mess, in order through the fire tunnel, speaking of order, then the problem.

According to the Chinese characteristics of the socialist culture, we strictly implement one thing, that is, let the leadership go first.

Now N people, marking from 1 to n. If a is The leader of B, you have to put a in front of B.

Then you'll have to arrange the order of everyone. I promise there must be a solution.

Enter more than one set of inputs, and then for each test data, the first row has two integersN (1 <= n <= 30000)and them (1 <= m <= 100000), each representing the number of people andThe number of subordinate existence.


Then m lines, two integers a and bper line, indicate that there is a leader of b . a and the b must be different.

Outputs the order in which each test data is queued, separated by a space.

Sample input
5 103 51 42 51 23 41 42 31 53 51 2
Sample output
1 2 3) 4 5

Topological sort, the data is relatively big, should use adjacency table, but this problem of background data water, with array also past.
1#include <stdio.h>2#include <iostream>3#include <algorithm>4#include <queue>5#include <string.h>6 #defineMAXN 50017  Short intMAPP[MAXN][MAXN];8 int inch[MAXN], VIS[MAXN];9 intN, M;Ten  One voidtopo () A { -     intI, J, K, cnt =0, flag=0, ss=0; -      for(i=1; i<=n; i++) the     { -          for(j=1; j<=n; J + +) -         { -            if(inch[j]==0&&!Vis[j]) +            { -cnt++; +                    if(ss==N) A                 { atFlag =1; -                      Break; -                 } -                    if(cnt==N) -                 { -printf"%d\n", j); inss++; -                      Break; to                 } +                 Else -                 { theprintf"%d", j); *ss++; $                 }Panax Notoginseng            } -         } the         if(flag) Break; +         if(inch[i]==0) A         { the              for(k=1; k<=n; k++) +             { -                 if(Mapp[i][k]) $                 { $                     inch[k]--; -                 } -             } thevis[i]=1; -         }Wuyi     } the } -  Wu using namespacestd; -  About intMain () $ { -     intx, y, I; -      while(~SCANF ("%d%d", &n, &m)) -     { Amemset (MAPP,0,sizeof(MAPP)); +Memsetinch,0,sizeof(inch)); thememset (Vis,0,sizeof(Vis)); -          for(i=0; i<m; i++) $         { thescanf"%d%d", &x, &y); the             if(mapp[x][y]==1); the             Else{ the                 inch[y]++; -Mapp[x][y] =1; in             } the         } the topo (); About     } the     return 0; the}




sdut3037--let the leader go first (topological sort)

Related Article

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.