CF 412 D Giving Awards (topological sort)

Source: Internet
Author: User

The employees of the R1 company often spend time Together:they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else.

Today is the day of giving off money rewards. The R1 company CEO would invite employees into he office one by one and rewarding each one for the hard work of this month. The CEO knows who owes money to whom. And he also understands that if he invites personxTo He office for a reward, and then immediately invite persony, who had lent some moneyx, then they can meet. Of course, in such a situation, the joy's personxFrom he brand new money reward would be much less. Therefore, the R1 CEO decided to invite the staff in such an order that the described situation would not happen for any PA IR of employees invited one after another.

However, there is a lot of employees in the company, and the CEO doesn ' t has a lot of time. Therefore, the task has been assigned. Given the debt relationships between all the employees, determine in which order they should is invited to the office of T He R1 company CEO, or determine this described order does not exist.

Input

The first line contains space-separated integersNandm-the number of employees in R1 and the number of debt relations. Each of the followingmLines contains, space-separated integers ai , bi (1?≤? ) Ai,? b i? ≤? n; a i? ≠? b i), these integers indicate the person number ai Owes money to a person a number bi . Assume that all the employees is numbered from 1 toN.

It is guaranteed, this each pair of people P, Q is mentioned in the input data at the most once. In particular, the input data is not contain pairs p, Q and q, p simultaneously.

Output

Print-1 if the described order does not exist. Otherwise, print the permutation of n distinct integers. The first number should denote the number of the person who goes to the CEO of office first, the second number denote the per Son who goes second and so on.

If There is multiple correct orders, you is allowed to print any of them.

Sample Test (s) input
2 11 2
Output
Input
3 31 22) 33 1
Output

2 1 3


Meet if X to Y borrow money, first give y, topological sort, meet the criteria of the person to do topological sort output.

#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 R EP (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) const int maxn=3*1e4+100;    Vector<int>mp[maxn];int vis[maxn];int n,num,m;void dfs (int x) {if (vis[x]) return;    Vis[x]=1;    for (int i=0;i<mp[x].size (); i++) Dfs (Mp[x][i]);    num++;    if (num==n) cout<<x<<endl; else cout<<x<< "";}    int main () {int x, y;        while (cin>>n>>m) {CLEAR (vis,0);        REPF (I,1,n) mp[i].clear ();            REP (i,m) {cin>>x>>y;        Mp[x].push_back (y);        } num=0;    REPF (I,1,n) if (!vis[i]) DFS (i); } return 0;}


CF 412 D Giving Awards (topological sort)

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.