题意描述

好像找不到链接(找到了请联系作者谢谢),所以题目描述会十分详细:

Problem 1: Ordering Cows [Bruce Merry, South African Computer Olympiad, 2003]

Farmer John has noticed that when his N (1 <= N <= 1,500) cows line up to be milked, certain cows always line up ahead of certain other cows. He has made a list of L (1 <= L <= 10,000) pairs of cows for which he has noticed this. He wants to send this list to his friend, Farmer Bob. Unfortunately, the inter-farm cowmunication network is very slow.

FJ has realized that he can compress the list by removing redundant information. The information he has is all of the form "cow A lines up before cow B". Information is redundant if it can be deduced from

other information on the list. For example, given "Alice is before Betty", "Betty is before Carol" and "Alice is before Carol" the last statement is redundant. Of course, the cows are numbered with unique serial numbers in the range 1..N; only a few of them have names (and those are not used here).

Help Farmer John by finding the smallest subset of his list from which the rest of the list can be deduced. Happily, the answer is guaranteed to be unique, and the original list contains no contradictions such as "Alice before Betty" and "Betty before Alice".

PROBLEM NAME: order

INPUT FORMAT:

  • Line 1: Two space-separated integers: N and L.

  • Lines 2..L+1: Two integers X and Y (1 <= X,Y <= N), indicating that cow X lines up before cow Y. No fact is duplicated.

SAMPLE INPUT (file order.in):

5 6

3 5

4 2

5 2

2 1

3 1

4 1

OUTPUT FORMAT:

  • Line 1: A single line with the number of facts that Farmer John will send to Farmer Bob.

  • Lines 2..U+1: The facts from the input file that Farmer John will transmit. These must be lists in the same format as the entries in the input file. They must be sorted numerically on the first entry in the line, with ties being broken by the second entry on the line.

SAMPLE OUTPUT (file order.out):

4

2 1

3 5

4 2

5 2

OUTPUT DETAILS:

The facts "cow 3 before cow 1" and "cow 4 before cow 1" can be deduced from the 4 facts listed in the output. However, none of the output facts can be deduced from the others.

给你关于 \(N\) 个数的 \(L\) 对大小关系,其中关系 x y 表示 x>y

如果有关系 x y 与关系 y z 那么显然关系 x z 是不必要的。

求出这 \(L\) 对关系中的最少必要关系。(即通过省下的这些关系可以推出被删去的关系)

写出其数量并按照以数字一为主关键字,以数字二为次关键字的顺序从小到大输出。

算法分析

类似传递闭包,但是这里要求你排除冗余的关系。

那么显然,如果我们可以确定一个顺序,保证后面的关系不会影响前面的关系。

那么只需要从前往后扫一遍,遇到多余的关系排除即可。(因为只有前面的关系可能影响后面的关系)

排除的方法之后再说。

确定方法的关系:

  1. 将每个关系当做有向边建图,易证这是个 DAG 图。
  2. 将此图拓扑排序,按照拓扑排序的顺序给各个节点从小到大赋值。
  3. 按照每个关系的数字一拓扑值从大到小为主关键字,数字二拓扑值从小到大为次关键字排序。
  4. 所得即上面所说的遍历顺序。

解释一下,我们给每个节点赋予一个 ”贡献值“,表示其对其它节点的影响力。

那么显然影响大的边要先考虑(这也符合上面的顺序)。

易得,拓扑值大的节点影响力也大,所以以其从大到小为主关键字。

但是,当从同一个点出发考虑关系时,就应当优先考虑拓扑值小的,理由如下:

那么显然只有按照以从小到大为次关键字,才能将关系 1 3 排除。

然后就按照这个顺序往后排查即可,具体方法为状压 DP。

但是由于节点数量很多,所以要用到 C++ STL 的 bitset。

假设有关系 \(x\to y\),如果 \(bits(x,y)\) 这个关系为多余关系。

否则 \(bits(x)|=bits(y)\)。

代码实现

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<bitset>
#include<queue>
#define N 1510
#define M 10010
using namespace std; int n,m,head[N],cnt=0;
int ru[N],Topo[N],tot=0;
struct Edge{
int nxt,to,frm;
}ed[M];
struct Ans{
int u,v;
}ans[M];
bitset<N>bits[N];
queue<int>q; int read(){
int x=0,f=1;char c=getchar();
while(c<'0' || c>'9') f=(c=='-')?-1:1,c=getchar();
while(c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*f;
} void add(int u,int v){
ed[++cnt].nxt=head[u];
ed[cnt].frm=u;
ed[cnt].to=v;
head[u]=cnt;
return;
} void topo(){
for(int i=1;i<=n;i++)
if(!ru[i]) q.push(i);
while(!q.empty()){
int u=q.front();q.pop();
Topo[u]=++tot;
for(int i=head[u];i;i=ed[i].nxt)
if(!--ru[ed[i].to]) q.push(ed[i].to);
}
return;
} bool cmp(Edge a,Edge b){
if(a.frm!=b.frm)
return Topo[a.frm]>Topo[b.frm];
return Topo[a.to]<Topo[b.to];
} bool cnp(Ans a,Ans b){
if(a.u!=b.u) return a.u<b.u;
return a.v<b.v;
} int main(){
//freopen("order.in","r",stdin);
//freopen("order.out","w",stdout);
memset(ru,0,sizeof(ru));
n=read(),m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read();
add(u,v);++ru[v];
}
topo();//简简单单的拓扑排序。
sort(ed+1,ed+m+1,cmp);
for(int i=1;i<=n;i++)
bits[i][i]=true;//预处理。
tot=0;
for(int i=1;i<=m;i++){
int u=ed[i].frm,v=ed[i].to;
if(bits[u][v]) continue;
bits[u]=bits[u]|bits[v];//DP。
ans[++tot]=(Ans){u,v};
}
sort(ans+1,ans+tot+1,cnp);
printf("%d\n",tot);
for(int i=1;i<=tot;i++)
printf("%d %d\n",ans[i].u,ans[i].v);
//fclose(stdin);fclose(stdout);
return 0;
}

完结撒花。

Ordering Cows的更多相关文章

  1. DP百题练(二)

    目录 DP百题练(二) 区间 DP NOI1995 石子合并 IOI1998 Polygon CH5302 金字塔 USACO06FEB Treats for the Cows G/S LG1043 ...

  2. [poj2182] Lost Cows (线段树)

    线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...

  3. Lost Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9669   Accepted: 6228 Description N (2 ...

  4. Lost Cows(线段树 POJ2182)

    Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...

  5. POJ 2182 Lost Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10996   Accepted: 7059 Description N (2 ...

  6. HDU-2711 Lost Cows

    Lost Cows Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. Lost Cows(BIT poj2182)

    Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10609   Accepted: 6797 Descri ...

  8. POJ 2182 Lost Cows(牛排序,线段树)

    Language: Default Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9207   Acce ...

  9. poj 2182 Lost Cows(段树精英赛的冠军)

    主题链接:http://poj.org/problem? id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. Centos-显示目录或者目录下文件信息-ls

    ls 显示指定目录信息或指定目录下文件和目录信息,后边不跟文件目录路径信息默认为当前工作目录 默认显示输出信息的总行数统计数 相关参数 -a 显示所有文件或子目录,包含隐藏文档 # linux中以 . ...

  2. 024 01 Android 零基础入门 01 Java基础语法 03 Java运算符 04 关系运算符

    024 01 Android 零基础入门 01 Java基础语法 03 Java运算符 04 关系运算符 本文知识点:Java中的关系运算符 关系运算符

  3. C++extern关键字理解

    extern是一种"外部声明"的关键字,字面意思就是在此处声明某种变量或函数,在外部定义. 下面的示意图是我的理解. extern关键字的主要作用是扩大变量/函数的作用域,使得其它 ...

  4. 2020武汉dotNET俱乐部分享交流活动正式启动

    去年9月去上海参加了2019 .NET开发者峰会,感触良多.回来后便一直想着在武汉也组织一场这样的活动,推动一下武汉.NET的发展.由于疫情的影响,这个想法一直被搁浅,好在疫情总算是控制住了,所以我们 ...

  5. 8-kubernetes-安全

    kubernetes安全框架 访问K8S集群的资源需要过三关:认证.鉴权.准入控制,任意一个不通过都会失败 普通用户若要安全访问集群API server,往往需要证书.token或者用户名+密码,po ...

  6. ansible-playbook简介

    1. ansible-playbook简介 • Playbooks 与 adhoc 相比,是一种完全不同的运用 ansible 的方式,是非常之强大的. • 简单来说,playbooks 是一种简单的 ...

  7. 多测师讲解selenium _ 获取input输入文本值_高级讲师肖sir

    1.get方法来获取到对应元素它的值 案例代码比如在输入框中输入666 driver.find_element_by_css_selector('#kw').send_keys('666')l =dr ...

  8. 界面酷炫,功能强大!这款 Linux 性能实时监控工具超好用!

    对于维护.管理Linux系统来说,它的性能监控非常重要,特别是实时监控数据,这个数据有利于我们判断服务器的负载压力,及时调整资源调配,也有助于更好的服务于业务.所以,今天民工哥给大家安利一款 Linu ...

  9. Verilog基础入门——简单的语句块编写(二)

    [题干] [代码] module top_module( input a, input b, output out ); assign out = a&b; endmodule [与或非门等] ...

  10. pytest文档57-计算单元测试代码覆盖率(pytest-cov)

    前言 我们在做测试的时候,经常遇到领导的灵魂拷问:你的测试用例覆盖率是多少,达到100%了么?你如何保证你的测试质量? 测试用例的覆盖率如何统计呢,如何知道开发的代码,我们都测到了,不会存在漏测的情况 ...