2-SAT 输出可行解
找可行解的方案就是:
根据第一次建的图建一个反图..然后求逆拓扑排序,建反图的原因是保持冲突的两个事件肯定会被染成不同的颜色
求逆拓扑排序的原因也是为了对图染的色不会发生冲突,输出可行解就是遍历一次逆拓扑排序时染成的颜色,输出同一组颜色的解就是其中的一组可行解。
 
代码:
 #include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <queue> const int maxn = ;
const int maxm = ;
struct node{
int u;
int v;
int next;
}edge1[maxm], edge2[maxm];
struct tt{
int s;
int e;
int l;
}tim[maxn];
int n, m, cnt1, cnt2, scc_cnt, dfs_clock;
int head1[maxn], head2[maxn], in[maxn], ct[maxn], ans[maxn];
int sccno[maxn], dfn[maxn], low[maxn], color[maxn];
std::stack<int>st; void init(){
cnt1 = ;
cnt2 = ;
scc_cnt = ;
dfs_clock = ;
memset(in, , sizeof(in));
memset(ans, , sizeof(ans));
memset(color, , sizeof(color));
memset(sccno, , sizeof(sccno));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(head1, -, sizeof(head1));
memset(head2, -, sizeof(head2));
} void add(int u, int v, struct node edge[], int head[], int &cnt){
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} void dfs(int u){
low[u] = dfn[u] = ++dfs_clock;
st.push(u);
for(int i = head1[u]; i != -; i = edge1[i].next){
int v = edge1[i].v;
if(!dfn[v]){
dfs(v);
low[u] = std::min(low[u], low[v]);
}
else if(!sccno[v]){
low[u] = std::min(low[u], dfn[v]);
}
}
if(low[u]==dfn[u]){
++scc_cnt;
while(){
int x = st.top();
st.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void toposort(){
std::queue<int>qu;
for(int i = ; i <= scc_cnt; i++){
if(in[i]==) qu.push(i);
}
while(!qu.empty()){
int u = qu.front();
qu.pop();
if(color[u]==){
color[u] = ;
color[ct[u]] = -;
}
for(int i = head2[u]; i != -; i = edge2[i].next){
int v = edge2[i].v;
--in[v];
if(in[v]==) qu.push(v);
}
}
} int main(){
while(~scanf("%d", &n)){
init();
for(int i = ; i < n; i++){
int s1, s2, t1, t2, l;
int sb = scanf("%d:%d %d:%d %d", &s1, &s2, &t1, &t2, &l);
sb++;
tim[i].s = s1*+s2;
tim[i].e = t1*+t2;
tim[i].l = l;
}
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i!=j){
if(tim[i].s<tim[j].s+tim[j].l && tim[j].s<tim[i].s+tim[i].l) add(i<<, j<<|, edge1, head1, cnt1);
if(tim[i].s<tim[j].e && tim[j].e-tim[j].l<tim[i].s+tim[i].l) add(i<<, j<<, edge1, head1, cnt1);
if(tim[i].e-tim[i].l<tim[j].s+tim[j].l && tim[j].s<tim[i].e) add(i<<|, j<<|, edge1, head1, cnt1);
if(tim[i].e-tim[i].l<tim[j].e && tim[j].e-tim[j].l<tim[i].e) add(i<<|, j<<, edge1, head1, cnt1);
}
}
}
for(int i = ; i < n+n; i++){
if(!dfn[i]) dfs(i);
}
for(int i = ; i < n+n; i++){
for(int j = head1[i]; j != -; j = edge1[j].next){
int v = edge1[j].v;
if(sccno[i] != sccno[v]){
add(sccno[v], sccno[i], edge2, head2, cnt2);
in[sccno[i]]++;
}
}
}
bool flag = false;
for(int i = ; i < n; i++){
if(sccno[i<<]==sccno[i<<|]){
flag = true;
break;
}
ct[sccno[i<<]] = sccno[i<<|];
ct[sccno[i<<|]] = sccno[i<<];
} if(flag) puts("NO");
else{
toposort();
for(int i = ; i < n+n; i++){
if(color[sccno[i]]==) ans[i] = ;
}
puts("YES");
for(int i = ; i < n; i++) {
if(ans[i<<]) printf("%02d:%02d %02d:%02d\n", tim[i].s/, tim[i].s%, (tim[i].s+tim[i].l)/, (tim[i].s+tim[i].l)%);
else printf("%02d:%02d %02d:%02d\n", (tim[i].e-tim[i].l)/, (tim[i].e-tim[i].l)%, tim[i].e/, tim[i].e%);
}
}
}
return ;
}
 
 

poj3683 Priest John's Busiest Day的更多相关文章

  1. POJ3683 Priest John's Busiest Day(2-SAT)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11049   Accepted: 3767   Special Judge ...

  2. 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 ...

  3. poj3683 Priest John's Busiest Day

    2-SAT. 读入用了黄学长的快速读入,在此膜拜感谢. 把每对时间当作俩个点.如果有交叉代表相互矛盾. 然后tarjan缩点,这样就能得出当前的2-SAT问题是否有解. 如果有解,跑拓扑排序就能找出一 ...

  4. 【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 ...

  5. 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 ...

  6. 图论(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 ...

  7. poj 3686 Priest John's Busiest Day

    http://poj.org/problem?id=3683 2-sat 问题判定,输出一组可行解 http://www.cnblogs.com/TheRoadToTheGold/p/8436948. ...

  8. POJ 3683 Priest John's Busiest Day (2-SAT)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6900   Accept ...

  9. POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10010   Accep ...

随机推荐

  1. oracle Array类型作为参数传入函数(存储过程) 大字符串参数解决方案

    1. 创建自定义的类型.由于Oracle没有提供现成的array类型,这里用table类型来模拟. CREATE OR REPLACE TYPE varchar_array is Table OF v ...

  2. Hbase的连接池--HTablePool被Deprecated之后

      说明: 最近两天在调研HBase的连接池,有了一些收获,特此记录下来. 本文先将官方文档(http://hbase.apache.org/book.html)9.3.1.1节翻译,方便大家阅读,然 ...

  3. ASP.NET的一套笔试题

    1.    自定义控件如何做?答:自定义控件,跟HtmlControl或WebControl相似,编译后可以添加引用到工具栏里面,直接用鼠标拖动使用.2.界面的布局?答:表格,div3.程序的执行过程 ...

  4. SQL分页查询总结{转}

    开发过程中经常遇到分页的需求,今天在此总结一下吧.简单说来方法有两种,一种在源上控制,一种在端上控制.源上控制把分页逻辑放在SQL层:端上控制一次性获取所有数据,把分页逻辑放在UI上(如GridVie ...

  5. Unity3d 联通沃商店接入问题

    Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Loope ...

  6. c++中的原子操作

    1. c/c++标准中没有定义任何操作符为原子的,操作符是否原子和平台及编译器版本有关 2. GCC提供了一组内建的原子操作,这些操作是以函数的形式提供的,这些函数不需要引用任何头文件 2.1 对变量 ...

  7. photosop快速对白色背景图片进行抠图

    因为其中有个作业,做个图书馆的小网页.所以打算取图书馆logo上面那几个字. 图片如下: 因为是白色背景,一开始打算使用魔棒工具,不过效果不好. 后来百度了下,使用色彩范围可以快速抠图 打开photo ...

  8. HDU 1098 Ignatius's puzzle(数学归纳)

    以下引用自http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=8466&messageid=2&deep=1 题意以 ...

  9. 能够将 HTML 表格转换成图表的jQuery插件:Chartinator

    点这里 一个jQuery 插件能够将HTML 表格转换成图表,使用 Google Charts 实现. Chartinator当前支持以下特性: Creation of the following c ...

  10. POJ 1113 Wall(Graham求凸包周长)

    题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...