poj 3683(2-sat+拓扑排序)

Priest John's Busiest Day
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 11127   Accepted: 3785   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 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

Source

将每个时间段拆分成两个 就变成的2-sat模型
利用拓扑排序+染色输出任意方案就可以了
注意数组大小
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstdlib>
  4 #include<cctype>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<map>
  8 #include<queue>
  9 #include<stack>
 10 #include<set>
 11 #include<vector>
 12 #include<algorithm>
 13 #include<string.h>
 14 typedef long long ll;
 15 typedef unsigned long long LL;
 16 using namespace std;
 17 const int INF=0x3f3f3f3f;
 18 const double eps=0.0000000001;
 19 const int N=2000+10;
 20 const ll mod=1e9+7;
 21 struct Node{
 22     int x,y;
 23 }a[N];
 24 struct node{
 25     int to,next;
 26 }edge[N*N];
 27 struct NODE{
 28     int to,next;
 29 }Edge[N*N];
 30 int Head[N];
 31 int head[N],low[N],dfn[N];
 32 int vis[N*10],belong[N*10];
 33 int opp[N*10];
 34 int cnt,t,tot;
 35 stack<int>st;
 36 void init(){
 37     tot=0;
 38     t=0;
 39     cnt=0;
 40     memset(belong,-1,sizeof(belong));
 41     memset(low,0,sizeof(low));
 42     memset(dfn,0,sizeof(dfn));
 43     memset(vis,0,sizeof(vis));
 44     memset(head,-1,sizeof(head));
 45 }
 46 void add(int u,int v){
 47     edge[tot].to=v;
 48     edge[tot].next=head[u];
 49     head[u]=tot++;
 50 }
 51 void ADD(int u,int v){
 52     Edge[tot].to=v;
 53     Edge[tot].next=Head[u];
 54     Head[u]=tot++;
 55 }
 56 void tarjan(int u){
 57     vis[u]=1;
 58     st.push(u);
 59     dfn[u]=low[u]=++t;
 60     for(int i=head[u];i!=-1;i=edge[i].next){
 61         int v=edge[i].to;
 62         if(dfn[v]==0){
 63             tarjan(v);
 64             low[u]=min(low[u],low[v]);
 65         }
 66         else if(vis[v]){
 67             low[u]=min(low[u],dfn[v]);
 68         }
 69     }
 70     if(low[u]==dfn[u]){
 71         cnt++;
 72         int vv;
 73         do{
 74             vv=st.top();
 75             st.pop();
 76             belong[vv]=cnt;
 77             vis[vv]=0;
 78         }while(vv!=u);
 79     }
 80 }
 81 int n;
 82 int in[N];
 83 void topsort(){
 84     queue<int>q;
 85     memset(vis,0,sizeof(vis));
 86     for(int i=1;i<=cnt;i++)if(in[i]==0)q.push(i);
 87     while(q.empty()==0){
 88         int u=q.front();
 89         q.pop();
 90         if(vis[u]==0){
 91             vis[u]=1;
 92             vis[opp[u]]=-1;
 93         }
 94         for(int i=Head[u];i!=-1;i=Edge[i].next){
 95             int v=Edge[i].to;
 96             in[v]--;
 97             if(in[v]==0){
 98                 q.push(v);
 99             }
100         }
101     }
102 }
103 int check(int i,int j){
104     if(a[i].y<=a[j].x){
105        return 1;
106     }
107 
108     if(a[i].x>=a[j].y){
109         return 1;
110     }
111     return 0;
112 }
113 char str1[100];
114 char str2[100];
115 int main(){
116     while(scanf("%d",&n)!=EOF){
117         int val;
118         init();
119         memset(in,0,sizeof(in));
120         for(int i=0;i<(n<<1);i=i+2){
121            scanf("%s%s%d",str1,str2,&val);
122            int x=(str1[0]-'0')*60*10+(str1[1]-'0')*60+(str1[3]-'0')*10+str1[4]-'0';
123            int y=(str2[0]-'0')*60*10+(str2[1]-'0')*60+(str2[3]-'0')*10+str2[4]-'0';;
124            a[i].x=x;
125            a[i].y=x+val;
126            a[i+1].x=y-val;
127            a[i+1].y=y;
128         }
129         for(int i=0;i<(n<<1);i=i+2){
130             for(int j=0;j<(n<<1);j=j+2){
131                 if(i==j)continue;
132                 if(check(i,j)==0)add(i,j^1);
133                 if(check(i,j^1)==0)add(i,j);
134                 if(check(i^1,j)==0)add(i^1,j^1);
135                 if(check(i^1,j^1)==0)add(i^1,j);
136             }
137         }
138         for(int i=0;i<(n<<1);i++){
139             if(dfn[i]==0)tarjan(i);
140         }
141         int flag=0;
142         for(int i=0;i<(n<<1);i=i+2){
143             opp[belong[i]]=belong[i^1];
144             opp[belong[i^1]]=belong[i];
145             if(belong[i]==belong[i^1])flag=1;
146         }
147         if(flag){
148             cout<<"NO"<<endl;
149             continue;
150         }
151         cout<<"YES"<<endl;
152         memset(Head,-1,sizeof(Head));
153         tot=0;
154         for(int u=0;u<(n<<1);u++){
155             for(int i=head[u];i!=-1;i=edge[i].next){
156                 int v=edge[i].to;
157                 if(belong[u]!=belong[v]){
158                     ADD(belong[v],belong[u]);
159                     in[belong[u]]++;
160                 }
161             }
162         }
163 
164         topsort();
165         int aa,b,c,d;
166         for(int i=0;i<n;i++){
167             if(vis[belong[i<<1]]==1){
168                 aa=a[i*2].x/60;
169                 b=a[i*2].x%60;
170                 c=a[i*2].y/60;
171                 d=a[i*2].y%60;
172                 //cout<<a[i*2].x<<" "<<a[i*2].y<<endl;
173                 printf("%02d:%02d %02d:%02d\n",aa,b,c,d);
174             }
175             else{
176                 aa=a[i*2+1].x/60;
177                 b=a[i*2+1].x%60;
178                 c=a[i*2+1].y/60;
179                 d=a[i*2+1].y%60;
180                 //cout<<a[i*2+1].x<<" "<<a[i*2+1].y<<endl;
181                 printf("%02d:%02d %02d:%02d\n",aa,b,c,d);
182             }
183         }
184     }
185 }

 

posted on 2018-03-21 16:19  见字如面  阅读(316)  评论(0编辑  收藏  举报

导航