poj 3683 2-sat

来源:互联网 发布:大气监测实时数据 编辑:程序博客网 时间:2024/05/22 12:02

Priest John's Busiest Day
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8584 Accepted: 2922 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 Sito 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 SiTi and DiSi 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

208:00 09:00 3008:15 09:00 20

Sample Output

YES08:00 08:3008:40 09:00

题目:有n个婚礼,其中每个婚礼的仪式时间有两个可选 (s,s+d) (t-d,t),问最后是否所有婚礼仪式不重叠,如果不重叠把每个仪式的时间输出。

思路:把每个仪式的两个可选时间看做两个点,A,A*; 就成了2-sat了。


学习2-sat看这篇博客http://blog.csdn.net/jarjingx/article/details/8521690

#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<vector>#include<queue>#include<cmath>#include<stack>#include<set>#include<map>#define INF 0x3f3f3f3f#define MAX 10005#define mod 10000007#define CLR(a,b) memset((a),(b),sizeof((a)))#pragma comment(linker, "/STACK:102400000,102400000")#define ul u<<1#define ur (u<<1)|1using namespace std;typedef long long ll;struct edege {    int v,next;}e[1000000*2];vector<int> g[1000000];int tot;int head[MAX];void addedge(int u,int v) {    e[tot].v=v;    e[tot].next=head[u];    head[u]=tot++;}bool judge(int a,int b,int c,int d) {    if(b<=c||d<=a)return false;    return true;}int sccno[MAX],tarjan_clock,scc_cnt;int pre[MAX],lowlink[MAX];stack<int> sk;void Tarjan(int u) {    pre[u]=lowlink[u]=++tarjan_clock;    sk.push(u);    for(int i=head[u]; i!=-1; i=e[i].next) {        int v=e[i].v;        if(!pre[v]) {            Tarjan(v);            lowlink[u]=min(lowlink[v],lowlink[u]);        } else        if(!sccno[v]) lowlink[u]=min(lowlink[u],pre[v]);    }    if(lowlink[u]==pre[u]) {        scc_cnt++;        while(1) {            int x=sk.top();            sk.pop();            sccno[x]=scc_cnt;            if(u==x) break;        }    }}void find_scc(int n) {    CLR(pre,0);    CLR(sccno,0);    tarjan_clock=scc_cnt=0;    for(int i=1; i<=n; i++) {        if(!pre[i]) Tarjan(i);    }}int id[MAX],cnt,degree[MAX],color[MAX],ot[MAX];void top_sort(int u) {    degree[u]=-1;    if(!color[u]) {        color[u]=1;        color[ot[u]]=-1;    }    int len=g[u].size();    for(int i=0;i<len;i++) {        int v=g[u][i];        degree[v]--;        if(!degree[v]) top_sort(v);    }}void print(int a,int b) {    printf("%02d:%02d ",a/60,a%60);    printf("%02d:%02d\n",b/60,b%60);}void init() {    tot=0;    cnt=0;    CLR(head,-1);    CLR(degree,0);    CLR(color,0);}int main() {    int s[MAX],t[MAX],D[MAX];    int n,a,b,c,d;    scanf("%d",&n);    init();    for(int i=1; i<=n; i++) {        scanf("%d:%d%d:%d%d",&a,&b,&c,&d,&D[i]);        s[i]=a*60+b;        t[i]=c*60+d;    }    for(int i=1; i<=n; i++) {        for(int j=i+1; j<=n; j++) {            if(judge(s[i],s[i]+D[i],s[j],s[j]+D[j])) {                addedge(i,j+n);                addedge(j,i+n);            }            if(judge(s[i],s[i]+D[i],t[j]-D[j],t[j])) {                addedge(i,j);                addedge(j+n,i+n);            }            if(judge(t[i]-D[i],t[i],s[j],s[j]+D[j])) {                addedge(i+n,j+n);                addedge(j,i);            }            if(judge(t[i]-D[i],t[i],t[j]-D[j],t[j])) {                addedge(i+n,j);                addedge(j+n,i);            }        }    }    find_scc(2*n);    for(int i=1; i<=n; i++)        if(sccno[i]==sccno[i+n]) {            printf("NO\n");            return 0;        }    printf("YES\n");    for(int u=1; u<=2*n; u++)        for(int i=head[u]; i!=-1; i=e[i].next) {            int v=e[i].v;            if(sccno[u]!=sccno[v]) {                g[sccno[v]].push_back(sccno[u]);                degree[sccno[u]]++;            }        }    for(int i=1;i<=n;i++) {        ot[sccno[i]]=sccno[i+n];        ot[sccno[i+n]]=sccno[i];    }    for(int i=1;i<=scc_cnt;i++)        if(!degree[i]) top_sort(i);    for(int i=1;i<=n;i++) {        if(color[sccno[i]]==1)            print(s[i],s[i]+D[i]);        else print(t[i]-D[i],t[i]);    }    return 0;}


0 0
原创粉丝点击