poj 3683(2-sat+拓扑排序)
| 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
#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+拓扑排序)的更多相关文章
- POJ 2367 (裸拓扑排序)
http://poj.org/problem?id=2367 题意:给你n个数,从第一个数到第n个数,每一行的数字代表排在这个行数的后面的数字,直到0. 这是一个特别裸的拓扑排序的一个题目,拓扑排序我 ...
- poj 3687 Labeling Balls(拓扑排序)
题目:http://poj.org/problem?id=3687题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号球最轻 ...
- [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10161 Accepted: 2810 D ...
- poj 2762(强连通分量+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意:给出一个有向图,判断任意的两个顶点(u,v)能否从u到达v,或v到达u,即单连通,输出Yes或No. 分析:对于同一个强连 ...
- POJ 2585.Window Pains 拓扑排序
Window Pains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1888 Accepted: 944 Descr ...
- POJ 1270 Following Orders 拓扑排序
http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- POJ 1128 Frame Stacking (拓扑排序)
题目链接 Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ...
- Poj 2367 Genealogical tree(拓扑排序)
题目:火星人的血缘关系,简单拓扑排序.很久没用邻接表了,这里复习一下. import java.util.Scanner; class edge { int val; edge next; } pub ...
- POJ 1094 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
随机推荐
- 简谈Redis
1.为什么使用redis 分析:博主觉得在项目中使用redis,主要是从两个角度去考虑:性能和并发.当然,redis还具备可以做分布式锁等其他功能,但是如果只是为了分布式锁这些其他功能,完全还有其他中 ...
- 修改 root密码
sudo su #切换到root账户sudo passwd root #输入密码
- 【】node基础概念问题(转载)
1.nodejs编写HelloWord,了解什么是nodejs,nodejs有什么特点 2.nodejs的模块怎么用,如何载入别的模块(require),如何给另一模块调用(module, mod ...
- 【转】Java中的IO操作
在使用io操作之前,先看一下java中的文件类File如何使用.File包括文件和目录,对文件和目录的操作是新建目录mkdir,新建文件createNewFile,删除文件和目录delete,以及其他 ...
- openjudge1944 吃糖果
描述名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N >0).妈妈告诉名名每天可以吃一块或者两块巧克力.假设名名每天都吃巧克力,问名名共有多 ...
- Android定位(是否使用GPS进行定位)
TencentLocationRequest request = TencentLocationRequest.create(); request.setRequestLevel(TencentLoc ...
- apple网址
https://developer.apple.com/downloads/index.action# 开发工具下载
- codeforces 691D(数据结构)
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input stan ...
- Java并发包——线程池
Java并发包——线程池 摘要:本文主要学习了Java并发包中的线程池. 部分内容来自以下博客: https://www.cnblogs.com/dolphin0520/p/3932921.html ...
- 记一次springMVC的跨域解决方案
日期:2019年5月18日 事情原因:由于微信小程序的开发只有测试环境,而后台提供借口的环境是开发环境:两个环境的域名不同,导致前端开发产生了跨域问题: 理论概念: 1.同源策略:同源策略是浏览器的安 ...