POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)
题目地址:POJ 3683
第一次做须要输出可行解的题目。
。
。大体思路是先用强连通来推断是否有可行解,然后用逆序建图。用拓扑排序来进行染色。然后输出可行解。
详细思路见传送门
由于推断的时候少写了一个等号。。检查了好长时间。。sad。。。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int head[2100], cnt, index, top, ans;
int dfn[2100], low[2100], belong[2100], instack[2100], stak[2100];
struct N
{
int l, r;
} time[2100];
struct node
{
int u, v, next;
} edge[10000000];
void add(int u, int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
bool pan(N x, N y)
{
if((x.l<y.l&&x.r<=y.l)||(x.r>y.r&&x.l>=y.r))
return 1;
return 0;
}
void tarjan(int u)
{
dfn[u]=low[u]=++index;
instack[u]=1;
stak[++top]=u;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u])
{
ans++;
while(1)
{
int v=stak[top--];
instack[v]=0;
belong[v]=ans;
if(u==v) break;
}
}
}
int head1[2100], cnt1, ct[2100], in[2100], color[2100];
struct node1
{
int u, v, next;
} edge1[10000000];
void add1(int u, int v)
{
edge1[cnt1].v=v;
edge1[cnt1].next=head1[u];
head1[u]=cnt1++;
}
void topo()
{
queue<int>q;
int i;
for(i=1; i<=ans;i++)
{
if(in[i]==0) q.push(i);
}
while(!q.empty())
{
int u=q.front();
q.pop();
if(!color[u])
{
color[u]=1;
color[ct[u]]=-1;
}
for(i=head1[u];i!=-1;i=edge1[i].next)
{
int v=edge[i].v;
in[v]--;
if(!in[v]) q.push(v);
}
}
}
void init()
{
memset(head,-1,sizeof(head));
cnt=top=ans=index=0;
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(head1,-1,sizeof(head1));
memset(in,0,sizeof(in));
memset(color,0,sizeof(color));
cnt1=0;
}
int main()
{
int n, i, j, a1, b1, a2, b2, c;
scanf("%d",&n);
init();
for(i=0; i<n; i++)
{
scanf("%d:%d%d:%d%d",&a1,&b1,&a2,&b2,&c);
time[i<<1].l=a1*60+b1;
time[i<<1].r=a1*60+b1+c;
time[i<<1|1].l=a2*60+b2-c;
time[i<<1|1].r=a2*60+b2;
}
for(i=0; i<n<<1; i++)
{
for(j=0; j<i; j++)
{
if(!pan(time[i],time[j]))
{
add(i,j^1);
add(j,i^1);
//printf("%d %d\n",i,j);
}
}
}
for(i=0; i<n<<1; i++)
{
if(!dfn[i])
tarjan(i);
}
int flag=0;
for(i=0; i<n; i++)
{
if(belong[i<<1]==belong[i<<1|1])
{
flag=1;
break;
}
ct[belong[i<<1]]=belong[i<<1|1];
ct[belong[i<<1|1]]=belong[i<<1];
}
if(flag)
puts("NO");
else
{
puts("YES");
for(i=0;i<n<<1;i++)
{
for(j=head1[i];j!=-1;j=edge1[j].next)
{
int v=edge1[j].v;
if(belong[i]!=belong[v])
{
add1(belong[v],belong[i]);
in[belong[i]]++;
}
}
}
topo();
for(i=0;i<n;i++)
{
if(color[belong[i<<1]]==1)
{
printf("%02d:%02d %02d:%02d\n",time[i<<1].l/60,time[i<<1].l%60,time[i<<1].r/60,time[i<<1].r%60);
}
else
{
printf("%02d:%02d %02d:%02d\n",time[i<<1|1].l/60,time[i<<1|1].l%60,time[i<<1|1].r/60,time[i<<1|1].r%60);
}
}
}
return 0;
}
POJ 3683 Priest John's Busiest Day (2-SAT+输出可行解)的更多相关文章
- POJ 3684 Priest John's Busiest Day 2-SAT+输出路径
强连通算法推断是否满足2-sat,然后反向建图,拓扑排序+染色. 一种选择是从 起点開始,还有一种是终点-持续时间那个点 開始. 若2个婚礼的某2种时间线段相交,则有矛盾,建边. easy出错的地方就 ...
- POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题)
POJ 3683 Priest John's Busiest Day / OpenJ_Bailian 3788 Priest John's Busiest Day(2-sat问题) Descripti ...
- POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- POJ 3683 Priest John's Busiest Day(2-SAT 并输出解)
Description John is the only priest in his town. September 1st is the John's busiest day in a year b ...
- poj - 3683 - Priest John's Busiest Day(2-SAT)
题意:有N场婚礼,每场婚礼的开始时间为Si,结束时间为Ti,每场婚礼有个仪式,历时Di,这个仪式要么在Si时刻开始,要么在Ti-Di时刻开始,问能否安排每场婚礼举行仪式的时间,使主持人John能参加所 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
题意:有n对新人要在同一天结婚.结婚时间为Ti到Di,这里有时长为Si的一个仪式需要神父出席.神父可以在Ti-(Ti+Si)这段时间出席也可以在(Di-Si)-Si这段时间.问神父能否出席所有仪式,如 ...
- POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...
- POJ 3683 Priest John's Busiest Day
2-SAT简单题,判断一下两个开区间是否相交 #include<cstdio> #include<cstring> #include<cmath> #include ...
随机推荐
- C/C++ char*、char[]
一.本质区别 char* s是一个保存字符串首地址的指针变量,char a[]是许多连续的内存单元,单元中的元素为char类型.之所以用char *能达到char []的效果,还是字符串的本质,即给你 ...
- 【sqli-labs】 less65 GET -Challenge -Blind -130 queries allowed -Variation4 (GET型 挑战 盲注 只允许130次查询 变化4)
双引号括号闭合 http://192.168.136.128/sqli-labs-master/Less-65/?id=1")%23
- BZOJ1013 + BZOJ1923 + POJ1830 (高斯消元)
三个题放在一起写了 主要是搞搞模板 在这里简述一下怎么写高斯消元 就和代数里学的加减消元学的一样 把矩阵化为上三角形形式 然后进行回代 同时枚举当前要消元的未知数和当前化简到哪一行了 然后从这一行往后 ...
- Python【每日一问】34
问: 基础题: 定义函数实现以下功能:求出 0-n 所能组成的奇数个数,位数最多 n+1 (0<n<10),比如键盘输入n=7,求出0-7所能组成的奇数个数 提高题: 有如下分数序列: 2 ...
- java导出word的6种方式(转发)
来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...
- 简述Centos系统启动流程
1. Centos5 POST开机自检 运行CMOS中的BIOS程序,加载第一个启动磁盘的Bootloader 由Bootloader读取kernel 通过挂载临时根目录initramfs加载核心模块 ...
- (四)Python3 循环语句——for
for循环的一般格式如下: for <variable> in <sequence>: <statements> else: <statements> ...
- (二)Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用
#!usr/bin/env python # -*- coding: utf-8 -*- def test(): print('hello, world') if __name__ == " ...
- 在线安全清空慢查询日志slowlog
mysql> show variables like '%slow_query%';+------------------------------------+--------------- ...
- list tuple dict (列表,元祖,字典间的相互转换)
#-*- coding:UTF-8 -*- #author:RXS002 #1.字典 dict = {'name':'Zara','age':7,'class':'First'} #字典转换为字符串, ...