poj3683 Priest John's Busiest Day
2-SAT。
读入用了黄学长的快速读入,在此膜拜感谢。
把每对时间当作俩个点。如果有交叉代表相互矛盾。
然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解。
如果有解,跑拓扑排序就能找出一个特定的解。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 5000 + 10;
const int maxm = 2000000 + 10;
inline int read() //by hzwer 实在太好了。。我用下。。跪谢。
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
} int g[maxn],v[maxm],next[maxm],eid;
int deg[maxn],g2[maxn],v2[maxm],next2[maxm],eid2;
int color[maxn],cid;
int vis[maxn];
int dfn[maxn],low[maxn],vid;
int s[maxn],sp;
int a[maxn],b[maxn];
int op[maxn];
int q[maxm],l,r,u;
int c[maxn];
int n; void addedge(int a,int b) {
v[eid]=b; next[eid]=g[a]; g[a]=eid++;
} void addedge2(int a,int b) {
deg[b]++;
v2[eid2]=b; next2[eid2]=g2[a]; g2[a]=eid2++;
} bool con(int x,int y) {
if(b[x]<=a[y] || b[y]<=a[x]) return 0;
return 1;
} void build() {
memset(g,-1,sizeof(g));
n=read();
for(int i=1,t;i<=n;i++) {
a[i<<1]=read();
a[i<<1]=a[i<<1]*60+read();
b[i<<1|1]=read();
b[i<<1|1]=b[i<<1|1]*60+read();
t=read();
b[i<<1]=a[i<<1]+t;
a[i<<1|1]=b[i<<1|1]-t;
}
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++) if(i!=j) {
if(con(i<<1,j<<1)) {
addedge(i<<1,j<<1|1);
addedge(j<<1,i<<1|1);
}
if(con(i<<1,j<<1|1)) {
addedge(i<<1,j<<1);
addedge(j<<1|1,i<<1|1);
}
if(con(i<<1|1,j<<1)) {
addedge(i<<1|1,j<<1|1);
addedge(j<<1,i<<1);
}
if(con(i<<1|1,j<<1|1)) {
addedge(i<<1|1,j<<1);
addedge(j<<1|1,i<<1);
}
} }
void tarjan(int u) {
dfn[u]=low[u]=++vid;
s[++sp]=u; vis[u]=1;
for(int i=g[u];~i;i=next[i]) {
if(vis[v[i]]==0) {
tarjan(v[i]);
low[u]=min(low[u],low[v[i]]);
}
else if(vis[v[i]]==1) {
low[u]=min(low[u],dfn[v[i]]);
}
} if(dfn[u]==low[u]) {
++cid;
do {
color[s[sp]]=cid;
vis[s[sp]]=2;
}while(s[sp--]!=u);
}
} void dfs(int u) {
if(c[u]) return;
c[u]=-1;
for(int i=g2[u];~i;i=next2[i]) dfs(v2[i]);
} void print(int x) {
printf("%.2d:%.2d %.2d:%.2d\n",a[x]/60,a[x]%60,b[x]/60,b[x]%60);
} void solve() {
for(int i=2;i<=((n<<1)|1);i++) if(!vis[i]) tarjan(i);
for(int i=1;i<=n;i++) {
if(color[i<<1]==color[i<<1|1]) {
printf("NO\n");
return;
}
}
printf("YES\n");
memset(g2,-1,sizeof(g2));
for(int u=2;u<=((n<<1)|1);u++) {
for(int i=g[u];~i;i=next[i]) if(color[u]!=color[v[i]])
addedge2(color[v[i]],color[u]);
} for(int i=1;i<=n;i++) {
op[color[i<<1]]=color[i<<1|1];
op[color[i<<1|1]]=color[i<<1];
} for(int u=1;u<=cid;u++) if(!deg[u]) q[++r]=u; while(r) {
u=q[r--];
if(c[u]) continue;
c[u]=1; dfs(op[u]);
for(int i=g2[u];~i;i=next2[i]) {
deg[v2[i]]--;
if(!deg[v2[i]]) q[++r]=v2[i];
}
}
for(int i=1;i<=n;i++)
if(c[color[i<<1]]==1) print(i<<1);
else print(i<<1|1);
} int main() {
build();
solve();
return 0;
}
poj3683 Priest John's Busiest Day的更多相关文章
- POJ3683 Priest John's Busiest Day(2-SAT)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11049 Accepted: 3767 Special Judge ...
- POJ3683 Priest John's Busiest Day 【2-sat】
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- poj3683 Priest John's Busiest Day
2-SAT 输出可行解 找可行解的方案就是: 根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色 求逆拓扑排序的原因也是为了对图染的色不会发生冲突, ...
- 【POJ3683】Priest John's Busiest Day
题目 John is the only priest in his town. September 1st is the John's busiest day in a year because th ...
- 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 ...
- 图论(2-sat):Priest John's Busiest Day
Priest John's Busiest Day Description John is the only priest in his town. September 1st is the Jo ...
- poj 3686 Priest John's Busiest Day
http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...
- 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+方案输出)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10010 Accep ...
随机推荐
- Careercup - Facebook面试题 - 5998719358992384
2014-05-02 00:22 题目链接 原题: Given a matrix consisting of 's. 题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个. 解法:四邻接还是八邻 ...
- Zabbix实现告警分级
Zabbix中trigger的severity的值定义了trigger的不同严重程度,其中severity默认的6个值为 Not classified, Information, Warning, A ...
- oracle一些函数
NVL( string1, replace_with):判断string1是否为空,如果是空就用replace_with代替. NVL2(E1, E2, E3)的功能为:如果E1为NULL,则函数返回 ...
- 母函数入门笔记(施工中…
定义:对于一个数列,它的母函数(即生成函数)为 为了对这个准确求值,我们设 举一个简单的例子 例1 对于数列 他的生成函数为 ,那么应用一下等比数列求和公式 这里由于 所以当时 那么 例 ...
- C# 生成MD5编码方法(不同位数)
/// <summary> /// </summary> /// <param name="strSource"& ...
- iOS7 兼容及部分细节
1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...
- java.util.ResourceBundle
转载自: http://lavasoft.blog.51cto.com/62575/184605 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译 ...
- URAL 1297 Palindrome 最长回文子串
POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...
- Jenkins使用
1. Jenkins工作流程: ①配置代码源,从代码源(如svn.git等)拉取代码,放入工作区 ②构建触发器(引发构建的条件,比如一定周期.代码提交更改等),从而能自动的进行构建 ③构建,选择构建的 ...
- POJ 1781
#include <iostream> #include <string> #include <cmath> using namespace std; unsign ...