Title Description farmer John ' s N cows (1≤n≤105), numbered 1 ... N as always, happen to has too much time on their hooves. As a result, they has worked out a complex social hierarchy related to the order in which Farmer John milks them every mo Rning.

After weeks of study, Farmer John had made M observations about his cows ' social structure (1≤m≤50,000). Each observation is a ordered list of some of his cows, indicating that these cows should being milked in the same order in Which they appear in the This list. For example, if one of Farmer John's observations is the list 2, 5, 1, Farmer John should milk Cow 2 sometime before he mi Lks Cow 5, who should is milked sometime before he milks Cow 1.

Farmer John's observations was prioritized, so he goal was to maximize the value of X for which his milking order meets th E conditions outlined in the first X observations. If Multiple milking orders satisfy these first X conditions, Farmer John believes that it's a longstanding tradition that Cows with lower numbers outrank those with higher numbers, so he would like to milk the lowest-numbered cows first. More formally, if multiple milking orders satisfy these conditions, Farmer John would like to use the lexicographically SM Allest one. An ordering x is lexicographically smaller than a ordering y if for some J, Xi=yi for all i<j and Xj<yj (in other W Ords, the orderings is identical up to a certain point and at which X is smaller than yy).

Farmer John determine the best order in which to milk his cows.

Enter the first line contains N and M. The next M lines each describe an observation. Line i+1 describes observation I, and starts with the number of cows mi listed in the observation followed by the list of Mimi integers giving the ordering of cows in the observation. The sum of the Mi's is at most 200,000.

Output N space-separated integers, giving a permutation of 1 ... N containing the order in which Farmer John should milk his cows.

Sample input

4 33 1 2 32 4 23 3 4 1

Sample output

1 4 2 3

Tips

Here, Farmer John had four cows and should milk cow 1 before cow 2 and cow 2 before Cow 3 (the first observation), Cow 4 b Efore Cow 2 (the second observation), and cow 3 before cow 4 and cow 4 before Cow 1 (the third observation). The first and observations can satisfied simultaneously, but Farmer John cannot meet all of the these criteria at once, as To does so would require that cow 1 come before cow 3 and cow 3 before cow 1.

This means there is a possible orderings:1 4 2 3 and 4 1 2 3, the first being lexicographically smaller.

First we need to know what the topological sort is, and Mr. Water has made me a very figurative metaphor. Topology sequencing is like a point skill tree, and some skills point to a pre-set skill. Then the topological ordering can be made by accessing such a graph to establish a linear sequence satisfying the corresponding conditions. However, the problem requires minimal dictionary order. All of us use priority queues in the topology order. He has M line, we have two branch number, get the answer.

#include <bits/stdc++.h> #define MAXN 100005using namespace Std;typedef long long ll;int a[maxn];int ANS[MAXN]; vector<int> v[maxn/2];struct edge{int u,v,next;} Edge[maxn*2];int head[maxn];int cnt=0;int ind[maxn];void addedge (int u,int v) {ind[v]++; Edge[cnt].u=u; Edge[cnt].v=v; Edge[cnt].next=head[u]; head[u]=cnt++;} int N;bool Topsort () {priority_queue<int,vector<int>,greater<int> >q; int tot=-1,i; for (i=1;i<=n;i++) {if (ind[i]==0) Q.push (i); } while (!q.empty ()) {int u=q.top (); Ans[++tot]=u; Q.pop (); for (i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; ind[v]--; if (ind[v]==0) Q.push (v); }} if (tot==n-1) return true; else return false;} Using namespace Std;int main () {int m,i,k,j; scanf ("%d%d", &n,&m); for (i=0;i<m;i++) {scanf ("%d", &k); for (j=1;j<=k;j++) {int u; SCANF ("%d", &u); V[i].push_back (U); }} int l=0,r=m-1; int mm=0; while (l<=r) {int mid= (L+R) >>1; cnt=0; memset (head,-1,sizeof (head)); memset (Edge,0,sizeof (Edge)); memset (IND,0,SIZEOF (Ind)); for (i=0;i<=mid;i++) {for (j=0;j<v[i].size () -1;j++) {Addedge (v[i][j],v[i ][j+1]); }} if (Topsort ()) {l=mid+1; Mm=max (Mm,mid); } else {r=mid-1; }} memset (Head,-1,sizeof (head)); memset (Edge,0,sizeof (Edge)); memset (IND,0,SIZEOF (Ind)); cnt=0; for (i=0;i<=mm;i++) {for (j=0;j<v[i].size () -1;j++) {Addedge (V[i][j],v[i] [j+1]); }} topsort (); for (i=0;i<n-1;i++) {printf ("%d", ans[i]); } cout<<ans[i]<<endl; return 0;}

6348:milking Order (Two-point answer + topological sort)