Description
John is the only priest in his town. September 1st is the John’s busiest day in a year because there is an old legend in the town that the couple who get married on that day will be forever blessed by the God of Love. This year N couples plan to get married on the blessed day. The i-th couple plan to hold their wedding from time Si to time Ti. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must be either at the beginning or the ending of the wedding (i.e. it must be either from Si to Si + Di, or from Ti - Di to Ti). Could you tell John how to arrange his schedule so that he can present at every special ceremonies of the weddings.
Note that John can not be present at two weddings simultaneously.
Input
The first line contains a integer N ( 1 ≤ N ≤ 1000).
The next N lines contain the Si, Ti and Di. Si and Ti are in the format of hh:mm.
Output
The first line of output contains “YES” or “NO” indicating whether John can be present at every special ceremony. If it is “YES”, output another N lines describing the staring time and finishing time of all the ceremonies.
Sample Input
Sample Output
YES
08:00 08:30
08:40 09:00
Source
题意:
给出n个婚礼,每个婚礼有个开始的时间和结束的时间,在婚礼期间,要举行持续时间为D的活动,这个活动只能在婚礼期间的前D时间内举行,或者是在婚礼期间的后D时间内举行,问能否安排一种方式使得能够参与n个婚礼的活动
思路:
每个活动有两个选择,而且还要满足一定的条件,这显然是一个2-SAT问题
方法:
2-SAT + Tarjan
首先根据两种选择是否首尾冲突进行连边,如果冲突,则连对立的边
根据建立的图,进行缩点,缩点后可以得到强连通分量的数量以及每个点属于哪一个强连通分量
求完SCC之后,判断两种情况是否冲突(在同一个SCC之内),如果冲突就直接输出“NO”即可
在判断的过程中同时记录对立的点所在的联通块的编号
其余的均为有解(“YES”)的情况
然后建立反图统计入度,跑拓扑排序,在拓扑排序的过程当中,如果对于一个没有被标记的点就染色为颜色1,将其对立的点染色为0
最后遍历n个点的染色信息,给出对应的输出即可:
ac_code: