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 ...
随机推荐
- key-value键值型数据库:Redis
key-value键值型数据库:Redis redis Redis是in-memory型(内存型)的键值数据库,数据在磁盘上是持久的,键类型是字符串,值类型是字符串.字符串集合(Set).sorted ...
- Linux系统调用--getrusage函数详解
Linux系统调用--getrusage函数详解 功能描述: 获得进程的相关资源信息.如:用户开销时间,系统开销时间,接收的信号量等等; 用法: #include <sys/t ...
- sharepoint services
I have got solution for authentication to share point web service I have use fedAuth Cookie and rtfa ...
- offset 后 指针数组转换
AcDbObjectId pidoffset; AcDbPolyline *plineOffset; AcDbVoidPtrArray ptarr=NULL; pline->getOffsetC ...
- Getting start with dbus in systemd (01) - Interface, method, path
Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...
- thupc & cts & apio & thusc 游记 (北京17日游记)
thupc & cts & apio & thusc 游记 (北京17日游记) Day 0 和隔壁校两人py了一下,六个人组了两队,(左哼哼)与(右哼哼),我和Camoufla ...
- LOJ #6009 「网络流 24 题」软件补丁
题面 某公司发现其研制的一个软件中有 $ n $ 个错误,随即为该软件发放了一批共 $ m $ 个补丁程序.每一个补丁程序都有其特定的适用环境,某个补丁只有在软件中包含某些错误而同时又不包含另一些错误 ...
- language support图标消失
在控制台下输入sudo apt-get install language-selector-gnome即可
- 运用循环求和( sum operation in python)
1.for loop example 1: sum of 1+2+...+10 ********** >>> sum=0 >>> for x in [1,2,3,4 ...
- Linux 复习二
第二章 一.Linux文件系统 1.基本概念 Linux文件系统为单根的树状结构,根为“/”,文件名大小写敏感,除了“/”都是可用字符,文件名以“.”开始的为隐藏文件. 2.常用文件夹 bin:可执行 ...