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;
}

【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348的更多相关文章

  1. codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序

    题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...

  2. CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序

    题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...

  3. CodeForces - 1100E 二分+拓扑排序

    题意: 一个n个节点的有向图,节点标号从1到n,存在m条单向边.每条单向边有一个权值,代表翻转其方向所需的代价.求使图变成无环图,其中翻转的最大边权值最小的方案,以及该方案翻转的最大的边权. Inpu ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) D. Robot Rapping Results Report 二分+拓扑排序

    D. Robot Rapping Results Report 题目连接: http://www.codeforces.com/contest/655/problem/D Description Wh ...

  5. Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)

    题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...

  6. USACO 2016 US Open Contest, Gold解题报告

    1.Splitting the Field http://usaco.org/index.php?page=viewproblem2&cpid=645 给二维坐标系中的n个点,求ans=用一个 ...

  7. 拓扑排序(三)之 Java详解

    前面分别介绍了拓扑排序的C和C++实现,本文通过Java实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处 ...

  8. 拓扑排序(二)之 C++详解

    本章是通过C++实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处:http://www.cnblogs. ...

  9. 拓扑排序(一)之 C语言详解

    本章介绍图的拓扑排序.和以往一样,本文会先对拓扑排序的理论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑 ...

随机推荐

  1. [转] createObjectURL方法 实现本地图片预览

    ie6 可以直接显示本本地路径的图片 如: <img src="file://c:/3.jpg" />  ~~~网上都说ie7就不支持这种文件系统路径的url,但测试 ...

  2. Centos7编译安装GCC7.2

    通常编译的时候可能需要新版本的gcc,本文就说明下基于低版本的gcc升级为gcc7.2 wget 'http://mirrors-usa.go-parts.com/gcc/releases/gcc-7 ...

  3. 【BZOJ1417】Pku3156 Interconnect

    题解: 比较简单的一道题 显然我们只需要知道每个联通块的大小就可以了 然后发现x1+xn=30 其中xi<=xi+1的状态只有5000 所以直接记忆化搜索就可以了 emm原来map还可以映射ve ...

  4. nginx的with-http_sub_module模块使用之替换字符串

    一.介绍 该ngx_http_sub_module模块是一个过滤器,通过将一个指定的字符串替换为另一个字符串来修改响应.该模块不是默认生成的,它应该使用--with-http_sub_module 配 ...

  5. python全栈开发day98-DRF

    1.CBV源码流程 2.restful协议 1 所有的数据,不过是通过网络获取的还是操作(增删改查)的数据,都是资源,将一切数据视为资源是REST区别与其他架构风格的最本质属性 2 面向资源架构(RO ...

  6. JS uint8Array转String

    Uint8Array转字符串 function Uint8ArrayToString(fileData){ var dataString = ""; ; i < fileDa ...

  7. UOJ#191. 【集训队互测2016】Unknown 点分治 分治 整体二分 凸包 计算几何

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ191.html 题目传送门 - UOJ191 题意 自行移步集训队论文2016中罗哲正的论文. 题解 自行 ...

  8. Codechef CHSIGN Change the Signs(May Challenge 2018) 动态规划

    原文链接http://www.cnblogs.com/zhouzhendong/p/9004583.html 题目传送门 - Codechef CHSIGN 题意 第一行,一个数$T$,表示数据组数. ...

  9. 更新pip和setuptools

    python -m pip install -U pip setuptools

  10. POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)

    <题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...