Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11127   Accepted: 3785   Special Judge

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

2
08:00 09:00 30
08:15 09:00 20

Sample Output

YES
08:00 08:30
08:40 09:00

Source

将每个时间段拆分成两个 就变成的2-sat模型
利用拓扑排序+染色输出任意方案就可以了
注意数组大小
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const ll mod=1e9+;
struct Node{
int x,y;
}a[N];
struct node{
int to,next;
}edge[N*N];
struct NODE{
int to,next;
}Edge[N*N];
int Head[N];
int head[N],low[N],dfn[N];
int vis[N*],belong[N*];
int opp[N*];
int cnt,t,tot;
stack<int>st;
void init(){
tot=;
t=;
cnt=;
memset(belong,-,sizeof(belong));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
void add(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void ADD(int u,int v){
Edge[tot].to=v;
Edge[tot].next=Head[u];
Head[u]=tot++;
}
void tarjan(int u){
vis[u]=;
st.push(u);
dfn[u]=low[u]=++t;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(dfn[v]==){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
cnt++;
int vv;
do{
vv=st.top();
st.pop();
belong[vv]=cnt;
vis[vv]=;
}while(vv!=u);
}
}
int n;
int in[N];
void topsort(){
queue<int>q;
memset(vis,,sizeof(vis));
for(int i=;i<=cnt;i++)if(in[i]==)q.push(i);
while(q.empty()==){
int u=q.front();
q.pop();
if(vis[u]==){
vis[u]=;
vis[opp[u]]=-;
}
for(int i=Head[u];i!=-;i=Edge[i].next){
int v=Edge[i].to;
in[v]--;
if(in[v]==){
q.push(v);
}
}
}
}
int check(int i,int j){
if(a[i].y<=a[j].x){
return ;
} if(a[i].x>=a[j].y){
return ;
}
return ;
}
char str1[];
char str2[];
int main(){
while(scanf("%d",&n)!=EOF){
int val;
init();
memset(in,,sizeof(in));
for(int i=;i<(n<<);i=i+){
scanf("%s%s%d",str1,str2,&val);
int x=(str1[]-'')**+(str1[]-'')*+(str1[]-'')*+str1[]-'';
int y=(str2[]-'')**+(str2[]-'')*+(str2[]-'')*+str2[]-'';;
a[i].x=x;
a[i].y=x+val;
a[i+].x=y-val;
a[i+].y=y;
}
for(int i=;i<(n<<);i=i+){
for(int j=;j<(n<<);j=j+){
if(i==j)continue;
if(check(i,j)==)add(i,j^);
if(check(i,j^)==)add(i,j);
if(check(i^,j)==)add(i^,j^);
if(check(i^,j^)==)add(i^,j);
}
}
for(int i=;i<(n<<);i++){
if(dfn[i]==)tarjan(i);
}
int flag=;
for(int i=;i<(n<<);i=i+){
opp[belong[i]]=belong[i^];
opp[belong[i^]]=belong[i];
if(belong[i]==belong[i^])flag=;
}
if(flag){
cout<<"NO"<<endl;
continue;
}
cout<<"YES"<<endl;
memset(Head,-,sizeof(Head));
tot=;
for(int u=;u<(n<<);u++){
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(belong[u]!=belong[v]){
ADD(belong[v],belong[u]);
in[belong[u]]++;
}
}
} topsort();
int aa,b,c,d;
for(int i=;i<n;i++){
if(vis[belong[i<<]]==){
aa=a[i*].x/;
b=a[i*].x%;
c=a[i*].y/;
d=a[i*].y%;
//cout<<a[i*2].x<<" "<<a[i*2].y<<endl;
printf("%02d:%02d %02d:%02d\n",aa,b,c,d);
}
else{
aa=a[i*+].x/;
b=a[i*+].x%;
c=a[i*+].y/;
d=a[i*+].y%;
//cout<<a[i*2+1].x<<" "<<a[i*2+1].y<<endl;
printf("%02d:%02d %02d:%02d\n",aa,b,c,d);
}
}
}
}

poj 3683(2-sat+拓扑排序)的更多相关文章

  1. POJ 2367 (裸拓扑排序)

    http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...

  2. poj 3687 Labeling Balls(拓扑排序)

    题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...

  3. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

  4. poj 2762(强连通分量+拓扑排序)

    题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...

  5. POJ 2585.Window Pains 拓扑排序

    Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1888   Accepted: 944 Descr ...

  6. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  7. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  8. POJ 1128 Frame Stacking (拓扑排序)

    题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...

  9. Poj 2367 Genealogical tree(拓扑排序)

    题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...

  10. POJ 1094 (传递闭包 + 拓扑排序)

    题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...

随机推荐

  1. [Python3网络爬虫开发实战] 1.3.2-Beautiful Soup的安装

    Beautiful Soup是Python的一个HTML或XML的解析库,我们可以用它来方便地从网页中提取数据.它拥有强大的API和多样的解析方式,本节就来了解下它的安装方式. 1. 相关链接 官方文 ...

  2. JAVA加载Properties配置资源文件

    JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...

  3. 如何在matlab里安装libsvm包

    有时我们需要用到SVR(支持向量回归)方法,而 matlab 自带的svm工具箱不能做回归分析,于是有了安装libsvm包的打算. 中间遇到一些困难,比如找不到编译器等等,经过一下午和一晚上的努力,在 ...

  4. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  5. tarjan 割点 割边

    by   GeneralLiu tarjan 求 割点 割边 无向图  的 割点 割边: 对于无向连通图来说, 如果删除   一个点以及与它相连的边   之后, 使得这个图不连通, 那么该点为割点 : ...

  6. JSP内置对象和EL内置对象

    JSP共有九大内置对象: (1) HttpSession类的session对象作用:主要用于来分别保存每个用户信息,与请求关联的会话:         会话状态维持是Web应用开发者必须面对的问题. ...

  7. 常用的delphi 第三方控件

    Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...

  8. codevs——1031 质数环

    1031 质数环  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 一个大小为N(N<=17 ...

  9. Redis2019年3.22

    redis缓存技术学习 一. redis基础配置 1. redis简介 1.1 redis 是c语言编写的一个缓存服务器, 是一个内存版本的nosql非关系型数据,大概11w/s访问处理. 数据都在本 ...

  10. python 交互模式 方向键乱码问题解决

    python交互模式下通常用向上键来找到之前执行的命令,用左右键移动光标.这很方便.但有的时候这些键在按完后却会出现乱码. 本文只解决CentOS 6.4 下 python2.7.8 的乱码问题. 这 ...