Milking Order
Posted rhythm-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Milking Order相关的知识,希望对你有一定的参考价值。
问题 C: Milking Order
时间限制: 3 Sec 内存限制: 128 MB提交: 20 解决: 7
[提交] [状态] [讨论版] [命题人:]
题目描述
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 yy).
Please help Farmer John determine the best order in which to milk his cows.
输入
输出
样例输入
4 3
3 1 2 3
2 4 2
3 3 4 1
样例输出
1 4 2 3
提示
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 <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <map> #define range(i,a,b) for(int i=a;i<=b;++i) #define LL long long #define rerange(i,a,b) for(int i=a;i>=b;--i) #define fill(arr,tmp) memset(arr,tmp,sizeof(arr)) using namespace std; const int N=(int)5e5+5; int front[N],to[N<<1],link[N<<1],r[N],cnt,n,m; bool vis[N]; vector<int>MAP[N]; struct node{ int id; node(int tmp){id=tmp;} bool operator <(const node&a)const{ return a.id<id; } }; priority_queue<node>dd; void init(){ cin>>n>>m; range(i,1,m){ int s,e; cin>>s; range(j,1,s){ cin>>e; MAP[i].push_back(e); } } } void add_edge(int x,int y){ ++cnt; to[cnt]=y; link[cnt]=front[x]; front[x]=cnt; } void build_tree(int mid){ cnt=0; fill(front,0);fill(r,0);fill(vis,0); range(i,1,mid) range(j,0,MAP[i].size()-2) { add_edge(MAP[i][j], MAP[i][j + 1]); ++r[MAP[i][j+1]]; } } bool tsort(int x){ int sum=0; range(i,1,n)if(!r[i])++sum,dd.push(node(i)),vis[i]=1; while(!dd.empty()){ node head=dd.top(); if(x)cout<<head.id<<" "; dd.pop(); for(int i=front[head.id];i;i=link[i]){ --r[to[i]]; if(!r[to[i]]&&!vis[to[i]])dd.push(node(to[i])),++sum,vis[to[i]]=true; } } return sum==n; } void solve(){ int left=0,right=m+1,ans; while(left<=right){ int mid=((left+right)>>1); build_tree(mid); if(tsort(0))left=mid+1,ans=mid; else right=mid-1; } build_tree(ans); tsort(1); } int main() { init(); solve(); return 0; }
以上是关于Milking Order的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段