[USACO10NOV]巧克力牛奶Chocolate Milk
题目描述
Farmer John’s milk production and shipping system is an intricate one! He uses milking machines for his many cows to harvest the milk that then flows into pipes.
Each of these pipes connects a milking machine to a joint, where it might be joined by exactly one more pipe (the milk flowing through both pipes merges). The milk then flows through additional pipes (which all start and end at joints) until it reaches the long central pipe connecting to the distribution room.
The milk then goes through a reverse process of splitting at various joints until it is flows into milk tanks that are picked up and taken to market.
Farmer John notices that there is at most one way for milk to travel from one joint to any other joint. Furthermore, since Farmer John is an efficient man by nature, he has made sure that milk will flow through each and every pipe; in other words, no pipe is unneeded.
If we think of a milking machine, joint, or milk tank as a node, there are N (2 <= N <= 100,000) nodes in total (and N-1 pipes
connecting them). The input describes each pipe as an ordered pair of nodes, A_i (1 <= A_i <= N) and B_i (1 <= B_i <= N; A_i < B_i) indicating milk flows from node A_i to node B_i. If there is no pipe coming in to A_i, it is a milking machine. Likewise, if no pipe goes out from B_i, it is a tank.
The demand of chocolate milk has skyrocketed in recent months, and Farmer John wants to install a chocolate inserter at one of the joints so he can create delicious chocolate milk for customers.
Being thrifty, Farmer John has only bought one chocolate inserter, so he wants to place it at a joint through which all the milk passes. He knows that such a joint exists.
Help Farmer John find all the possible places he can install the chocolate inserter. (Note that Farmer John cannot install the chocolate inserter at the same location as a milking machine.)
As an example, consider a milking setup like this one:
1 | 1 ----+ |
Visual inspection shows that the chocolate inserter can be installed at either joint 6 or 7, as all milk flows through those joints.
农民约翰的牛奶生产和运输系统是极其复杂的!他用挤奶机为他的许多奶牛收获牛奶,然后流入管道。
每一个管道连接挤奶机到一个节点,在那里它可能会加入一个管道(牛奶流过两个管道合并)。然后,牛奶流通过额外的管道(所有的开始点和结束点在其它节点上的),直到它到达一个关键的节点。
牛奶然后经过一个反向的过程,在不同的关节分裂,直到它流入牛奶罐被拾起,并送往市场。
农场主约翰注意到,牛奶从一个关节到另一个关节的方式最多。此外,由于农民约翰本质上是一个能干的人,他已经确信,牛奶会流通过每个管道;换句话说,没有管道是不必要的。
如果我们认为挤奶机,节点,或牛奶罐都看作一个节点,有N (2 <= N <= 100,000)节点总数(N-1个管道连接它们)。
输入描述每个管节点的有序对,A_i(1 <= A_i <= N)和B_i(1 <= B_i <= N;A_i < B_i)表示牛奶从A_i流向B_i。如果A_i入度为0,它是一个挤奶机。同样,B_i出度为0,它是一个牛奶罐。
巧克力牛奶的需求已经在最近几个月急剧增加,农民约翰想安装一个巧克力插件在一个节点上,保证他能为顾客创造美味的巧克力牛奶。
为了节俭,农民约翰只买了一个巧克力插件,所以他想把它安在一个牛奶都经过的节点上。他知道这样的节点存在。
帮助农民约翰找到所有可能的地方,他将在那儿安装巧克力的插件。(注:农民约翰巧克力插件不能安装在一个挤奶机上。)
1 | 1 ----+ |
巧克力插件可以安装在6或7上,所有牛奶流经这些节点。
输入输出格式
输入格式:
* Line 1: A single integer: N
* Lines 2..N: Line i+1 contains two space-separated integers that describe a pipe’s connectivity: A_i and B_i
第1行:一个整数:N
2到N行:包含空格分隔的两个整数描述管道的连接:A_i和B_i
输出格式:
* Lines 1..??: Integers, one per line and in ascending order, each denoting a possible joint at which to install the chocolate inserter.
第1行到??行:每行一个整数,升序排列,每个表示可以安装巧克力插件的节点。
输入输出样例
输入样例#1:
1 | 9 |
输出样例#1:
1 | 6 |
题解
这题我想了个乱搞做法结果没想到是正解23333
虽然从来没学过网络流但是我看完这道题莫名其妙的想到了最大流这个我都不知道概念的东西(跟最大流没有任何关系)
考虑一种类似模拟的算法:
将每台挤奶机设置一个流量,拓扑排序后平均转移流量,或者怎么分都行(差不多那种。。)因为我们只关心最大汇聚的流量是多少。
然后就交了10多遍轻松AC
Code:
1 | // luogu-judger-enable-o2 |
二分图匹配
找出给定二分图最大图匹配。(四种图匹配中最简单一种了)
Dinic是可以的,不过联赛前不打算学这种高级算法。
使用匈牙利算法解决。
首先引入增广路概念:已匹配的边和未匹配的边在一条长度为奇数的路径中交替出现,且端点是两个未匹配的点。这条路径即是这组边集的增广路。
不断寻找增广路并取反,直到图中没有任何增广路即最大二分图匹配(berge theorem)
Code:
1 |
|
这是一个较为低效的算法,复杂度为
n与m为二分图的两边的点。
Code:
1 |
|
[USACO15JAN]踩踏Stampede
题目描述
Farmer John’s N cows (1 <= N <= 50,000) appear to be stampeding along the road at the front of FJ’s farm, but they are actually just running in a foot race to see which cow is the fastest.
Viewed from above, each cow is represented by a unit-length horizontal line segment, specified by the coordinates of its left corner point at time t=0. For example, (-3,6) would specify a cow who at time t=0 is represented by the segment from (-3,6) to (-2,6). Each cow is moving to the right (in the +x direction) at a certain rate, specified by the integer amount of time it takes her to move 1 unit to the right.
FJ is not particularly thrilled that his cows are outside running instead of in the barn producing milk. He plans to admonish them with a stern lecture after the race ends. In order to determine which of his cows are participating in the race, FJ situates himself at (0,0) and looks along a ray extending in the +y direction. As the race unfolds, FJ sees a cow if she is ever the first cow visible along this ray. That is, a cow might not be visible if another cow is “in front” of her during the entire time she crosses FJ’s line of sight.
Please compute the number of cows FJ can see during the entire race.
DJ站在原点上向y轴正半轴看,然后有一群奶牛从他眼前飞过。这些奶牛初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走一个单位。 DJ能看见一匹奶牛当且仅当它身体任意某部位x坐标为0时,没有其它y坐标小于此奶牛的奶牛身体某部位x坐标为0。 问DJ能看见多少奶牛?
输入输出格式
输入格式:
INPUT: (file stampede.in)
The first line of the input contains N. Each of the following N lines describes a cow with three integers x y r, corresponding to a cow whose left endpoint is at (x,y) at time t=0, moving to the right at a continuous speed of 1 unit of distance every r units of time. The value of x is in the range -1000..-1, the value of y is in the range 1..1,000,000 (and distinct for every cow, to prevent any possible collisions), and the value of r is in the range 1..1,000,000.
输出格式:
OUTPUT: (file stampede.out)
A single integer, specifying the number of cows FJ can see during the
entire race (from t=0 onward).
输入输出样例
输入样例#1:
1 | 3 |
输出样例#1:
1 | 2 |
说明
SOLUTION NOTES:
FJ can see cows 1 and 2 but not cow 3.
题解
没想到还左闭右开。。。我怎么读题都没读出这个意思来。
总之思路就是离散化+扫描线+线段树。也就是我们算出每头牛的时间区间,然后从下到上线段覆盖完事了。
不卡我边界我就一边AC了嘤嘤嘤
Code:
1 |
|
[AHOI2008]紧急集合 / 聚会
题目描述
欢乐岛上有个非常好玩的游戏,叫做“紧急集合”。在岛上分散有N个等待点,有N-1条道路连接着它们,每一条道路都连接某两个等待点,且通过这些道路可以走遍所有的等待点,通过道路从一个点到另一个点要花费一个游戏币。
参加游戏的人三人一组,开始的时候,所有人员均任意分散在各个等待点上(每个点同时允许多个人等待),每个人均带有足够多的游戏币(用于支付使用道路的花费)、地图(标明等待点之间道路连接的情况)以及对话机(用于和同组的成员联系)。当集合号吹响后,每组成员之间迅速联系,了解到自己组所有成员所在的等待点后,迅速在N个等待点中确定一个集结点,组内所有成员将在该集合点集合,集合所用花费最少的组将是游戏的赢家。
小可可和他的朋友邀请你一起参加这个游戏,由你来选择集合点,聪明的你能够完成这个任务,帮助小可可赢得游戏吗?
输入输出格式
输入格式:
第一行两个正整数N和M(N<=500000,M<=500000),之间用一个空格隔开。分别表示等待点的个数(等待点也从1到N进行编号)和获奖所需要完成集合的次数。 随后有N-1行,每行用两个正整数A和B,之间用一个空格隔开,表示编号为A和编号为B的等待点之间有一条路。 接着还有M行,每行用三个正整数表示某次集合前小可可、小可可的朋友以及你所在等待点的编号。
输出格式:
一共有M行,每行两个数P,C,用一个空格隔开。其中第i行表示第i次集合点选择在编号为P的等待点,集合总共的花费是C个游戏币。
输入输出样例
输入样例#1:
1 | 6 4 |
输出样例#1:
1 | 5 2 |
说明
提示:
40%的数据中N<=2000,M<=2000
100%的数据中,N<=500000,M<=500000
题解
一道贪心结论题。主要考察对LCA的理解,大概NOIp D1T2难度或者D2T1
显然当三点LCA相同时没有什么优化,当有且仅有一个LCA比较低,那就选这个点,最后容斥一下发现答案对于
为
考虑数据范围比较大(HPD好),HPD LCA即可。
Code:
1 | // luogu-judger-enable-o2 |