zoukankan      html  css  js  c++  java
  • 图论(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 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 Si to 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 Si, Ti and Di. Si 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

    2
    08:00 09:00 30
    08:15 09:00 20
    
    

    Sample Output

    YES
    08:00 08:30
    08:40 09:00

      闹半天结果是数组开小了。

      这里是2-sat O(n+m)输出任意解的模板。

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 using namespace std;
      5 const int maxn=4010;
      6 const int maxm=4000010;
      7 int n;
      8 struct E{
      9     int cnt,fir[maxn],to[maxm],nxt[maxm];
     10     void addedge(int a,int b){
     11         nxt[++cnt]=fir[a];
     12         fir[a]=cnt;
     13         to[cnt]=b;
     14     }
     15 }e,g;
     16 
     17 struct Time{
     18     int h,s;
     19     Time(int h_=0,int s_=0){
     20         h=h_;s=s_;
     21     }
     22     friend bool operator <=(Time x,Time y){
     23         return x.h!=y.h?x.h<y.h:x.s<=y.s;
     24     }
     25     friend Time operator +(Time x,Time y){
     26         Time ret(0,0);
     27         ret.s=(x.s+y.s)%60;
     28         ret.h=x.h+y.h+(x.s+y.s)/60;
     29         return ret;
     30     }
     31     friend Time operator -(Time x,Time y){
     32         Time ret(0,0);
     33         ret.s=x.s-y.s+(x.s<y.s?1:0)*60;
     34         ret.h=x.h-y.h-(x.s<y.s?1:0);
     35         return ret;
     36     }
     37     void Print(){
     38         printf("%02d:%02d",h,s);
     39     }
     40 }p1[maxn],p2[maxn],p3[maxn],p4[maxn];
     41 
     42 bool Judge(Time pl,Time pr,Time ql,Time qr){
     43     if(pr<=ql||qr<=pl)return false;
     44     return true;
     45 }
     46 
     47 int tot,ID[maxn],low[maxn],scnt;
     48 int st[maxn],top,scc[maxn];
     49 
     50 void Tarjan(int x){
     51     ID[x]=low[x]=++tot;st[++top]=x;
     52     for(int i=e.fir[x];i;i=e.nxt[i])
     53         if(!ID[e.to[i]]){
     54             Tarjan(e.to[i]);
     55             low[x]=min(low[x],low[e.to[i]]);
     56         }
     57         else if(!scc[e.to[i]])
     58             low[x]=min(low[x],ID[e.to[i]]);
     59     if(low[x]==ID[x]){
     60         ++scnt;
     61         while(true){  
     62             int y=st[top--];
     63             scc[y]=scnt;
     64             if(x==y)break;
     65         }
     66     }
     67 }
     68 
     69 bool Solve(){
     70     for(int i=0;i<n*2;i++)
     71         if(!ID[i])Tarjan(i);
     72     
     73     for(int i=0;i<n;i++)
     74         if(scc[i*2]==scc[i*2+1])
     75             return false;
     76     return true;        
     77 }
     78 
     79 int rev[maxn];
     80 int match[maxn],in[maxn];
     81 int q[maxn],front,back;
     82 void Topo(){
     83     for(int i=0;i<n;i++){
     84         rev[scc[i*2]]=scc[i*2+1];
     85         rev[scc[i*2+1]]=scc[i*2];
     86     }
     87     for(int x=0;x<n*2;x++)
     88         for(int i=e.fir[x];i;i=e.nxt[i])
     89             if(scc[x]!=scc[e.to[i]])
     90                 g.addedge(scc[e.to[i]],scc[x]),in[scc[x]]+=1;
     91                 
     92     for(int i=1;i<=scnt;i++)
     93         if(!in[i])q[back++]=i;
     94     
     95     while(front<back){
     96         int x=q[front++];
     97         if(match[x]==0){
     98             match[x]=1;
     99             match[rev[x]]=2;
    100         }
    101         for(int i=g.fir[x];i;i=g.nxt[i])
    102             if(--in[g.to[i]]==0)q[back++]=g.to[i];
    103     }    
    104 }
    105 
    106 void Print(){
    107     for(int i=0;i<n;i++){
    108         if(match[scc[i*2]]==1){
    109             p1[i].Print();printf(" ");
    110             p2[i].Print();printf("
    ");
    111         }
    112         else{
    113             p3[i].Print();printf(" ");
    114             p4[i].Print();printf("
    ");
    115         }
    116     }
    117 }
    118 
    119 int main(){
    120     scanf("%d",&n);
    121     for(int i=0,d;i<n;i++){
    122         scanf("%d:%d",&p1[i].h,&p1[i].s);
    123         scanf("%d:%d",&p4[i].h,&p4[i].s);
    124         scanf("%d",&d);
    125         p2[i]=p1[i]+Time(d/60,d%60);
    126         p3[i]=p4[i]-Time(d/60,d%60);
    127     }
    128     
    129     for(int i=0;i<n;i++)
    130         for(int j=i+1;j<n;j++){
    131             if(Judge(p1[i],p2[i],p1[j],p2[j])){
    132                 e.addedge(i*2,j*2+1);
    133                 e.addedge(j*2,i*2+1);
    134             }
    135             if(Judge(p1[i],p2[i],p3[j],p4[j])){
    136                 e.addedge(i*2,j*2);
    137                 e.addedge(j*2+1,i*2+1);
    138             }
    139             if(Judge(p3[i],p4[i],p1[j],p2[j])){
    140                 e.addedge(i*2+1,j*2+1);
    141                 e.addedge(j*2,i*2);
    142             }
    143             if(Judge(p3[i],p4[i],p3[j],p4[j])){
    144                 e.addedge(i*2+1,j*2);
    145                 e.addedge(j*2+1,i*2);
    146             }
    147         }
    148     
    149     if(!Solve())
    150         printf("NO
    ");
    151     else{
    152         printf("YES
    ");
    153         Topo();Print();
    154     }
    155     return 0;
    156 }
  • 相关阅读:
    Matlab高级教程_第一篇:Matlab基础知识提炼_01
    量化投资_量化投资系统框架的DIY_02_01
    计量经济与时间序列_时间序列之物理含义
    计量经济与时间序列_协整和误差修正模型
    数学之美_正态分布(Python代码)
    [转载] ./configure,make,make install的作用
    [转载]Deep Learning(深度学习)学习笔记整理
    AJAX XML 实例
    百度搜索插件源码
    apache 服务器在ubuntu上图片无法显示解决
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5635491.html
Copyright © 2011-2022 走看看