time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John’s N cows (1≤N≤105), numbered 1…N as always, happen to have too much time on their hooves. As a result, they have worked out a complex social hierarchy related to the order in which Farmer John milks them every morning.
After weeks of study, Farmer John has made M observations about his cows’ social structure (1≤M≤50,000). Each observation is an ordered list of some of his cows, indicating that these cows should be milked in the same order in which they appear in 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 milks cow 5, who should be milked sometime before he milks cow 1.
Farmer John’s observations are prioritized, so his goal is to maximize the value of X for which his milking order meets the conditions outlined in the first X observations. If multiple milking orders satisfy these first X conditions, Farmer John believes that it is 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 smallest one. An ordering x is lexicographically smaller than an ordering y if for some j, xi=yi for all i<j and xj<yj (in other words, the two orderings are identical up to a certain point, at which x is smaller than y).
Please help Farmer John determine the best order in which to milk his cows.
Input
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 mi integers giving the ordering of cows in the observation. The sum of the mi’s is at most 200,000.
Output
Output N space-separated integers, giving a permutation of 1…N containing the order in which Farmer John should milk his cows.
Example
inputCopy
4 3
3 1 2 3
2 4 2
3 3 4 1
outputCopy
1 4 2 3
Note
Here, Farmer John has four cows and should milk cow 1 before cow 2 and cow 2 before cow 3 (the first observation), cow 4 before cow 2 (the second observation), and cow 3 before cow 4 and cow 4 before cow 1 (the third observation). The first two observations can be satisfied simultaneously, but Farmer John cannot meet all of these criteria at once, as to do so would require that cow 1 come before cow 3 and cow 3 before cow 1.
This means there are two possible orderings: 1 4 2 3 and 4 1 2 3, the first being lexicographically smaller.
思路:拓扑排序,判环,记录路径
#include <bits/stdc++.h>
#define ll long long
using namespace std;
typedef pair<ll,ll> P;
const ll M=1e5+10;
ll n,m,in[M];
vector<ll> nt[M],lis[M];
bool vis[M],used[M];
void link(ll x){
for(ll i=1;i<=n;i++)
nt[i].clear();
for(ll i=0;i<x;i++){
for(ll j=0;j<lis[i].size()-1;j++){
nt[lis[i][j]].push_back(lis[i][j+1]);
}
}
}
void cnt_in(){
memset(in,0,sizeof(in));
for(ll i=1;i<=n;i++)
for(ll j=0;j<nt[i].size();j++){
in[nt[i][j]]++;
}
}
bool check(ll x){
memset(vis,0,sizeof(vis));
link(x);
queue<ll> q;
cnt_in();
for(ll i=1;i<=n;i++){
if(in[i]==0)
q.push(i);
}
ll cnt=0;
while(!q.empty()){
ll top=q.front();
q.pop();
vis[top]=1,cnt++;
for(ll i=0;i<nt[top].size();i++){
in[nt[top][i]]--;
if(in[nt[top][i]]==0 && !vis[nt[top][i]])
q.push(nt[top][i]);
}
}
return cnt==n;
}
void top_sort(){
priority_queue<ll,vector<ll>,greater<ll> > q;
memset(vis,0,sizeof(vis));
cnt_in();
for(ll i=1;i<=n;i++)
if(in[i]==0 && !vis[i]){
q.push(i);
}
while(!q.empty()){
ll top=q.top();
q.pop();
cout<<top<<' ';
vis[top]=1;
for(ll i=0;i<nt[top].size();i++){
in[nt[top][i]]--;
if(in[nt[top][i]]==0 && !vis[nt[top][i]])
q.push(nt[top][i]);
}
}
}
int main(){
cin>>n>>m;
for(ll i=0;i<m;i++){
ll x;
cin>>x;
for(ll j=0;j<x;j++){
ll y;
cin>>y;
lis[i].push_back(y);
}
}
ll l=1,r=m+1;
while(r-l>1){
ll mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
link(l);
top_sort();
return 0;
}