【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348
Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348
PROBLEM
题目描述
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 yy).
Please help Farmer John determine the best order in which to milk his cows.
输入
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.
样例输入
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.
MEANING
给你n个点,m条链(边集),要求选前k条链,使得所有点和选择的边构成一个DAG,k要尽可能大,输出点的最小拓扑序。
SOLUTION
如果前k条链不能构成DAG,,那么k+1条链也不能构成DAG,因此k的取值在 \(k_{max}\)的左侧合法,右侧不合法 ,所以可以二分求k的最大值,对构成的图拓扑排序,即可判断图是否为DAG。
CODE
#define IN_PC() freopen("C:\\Users\\hz\\Desktop\\in.txt","r",stdin)
#define IN_LB() freopen("C:\\Users\\acm2018\\Desktop\\in.txt","r",stdin)
#define OUT_PC() freopen("C:\\Users\\hz\\Desktop\\out.txt","w",stdout)
#define OUT_LB() freopen("C:\\Users\\acm2018\\Desktop\\out.txt","w",stdout)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int n, m;
priority_queue<int,vector<int>,greater<int> > pq;
vector<int> ans;
struct edge {
int v, w,nex;
} ed[MAXN * 4];
int in[MAXN],head[MAXN], cnt;
void addedge(int u, int v,int w) {
cnt++;
ed[cnt].v = v;
ed[cnt].w = w;
ed[cnt].nex = head[u];
head[u] = cnt;
}
bool judge(int num) {
for(int i=1;i<=n;i++)in[i] = 0;
for(int i=1;i<=n;i++){
for(int j = head[i];j;j=ed[j].nex){
if(ed[j].w<=num){
in[ed[j].v]++;
}
}
}
for(int i=1;i<=n;i++){
if(!in[i])pq.push(i);
}
ans.clear();
while(!pq.empty()){
int u = pq.top();
ans.push_back(u);
pq.pop();
for(int i=head[u];i;i=ed[i].nex){
int v = ed[i].v;
if(ed[i].w>num)continue;
in[v]--;
if(!in[v]){
pq.push(v);
}
}
}
return ans.size() == (unsigned)n;
}
int main() {
// IN_PC();
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) {
int k,p,pre;
scanf("%d", &k);
for(int j = 0; j < k; j++) {
scanf("%d", &p);
if(j)addedge(pre,p,i);
pre = p;
}
}
int l = 0, r = m - 1;
while(l < r) {
int mid = (l + r +1) / 2;
if(judge(mid))
l = mid;
else r = mid - 1;
}
judge(l);
for(unsigned int i=0;i<ans.size();i++)printf("%s%d",i==0?"":" ",ans[i]);
printf("\n");
return 0;
}