POJ3683 Priest John's Busiest Day, poj1_3busiest
Time Limit:2000 MS |
|
Memory Limit:65536 K |
Total Submissions:11049 |
|
Accepted:3767 |
|
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 yearNCouples plan to get married on the blessed day.I-Th couple plan to hold their wedding from timeSiTo timeTi. According to the traditions in the town, there must be a special ceremony on which the couple stand before the priest and accept blessings.I-Th couple needDiMinutes 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 fromSiToSi+Di, Or fromTi-DiToTi). Cocould 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 integerN(1 ≤N≤ 1000 ).
The nextNLines containSi,TiAndDi.SiAndTiAre in the formatHh: 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 anotherNLines 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
Source
Question:
POJ Founder Monthly Contest-2008.08.31, Dagger and Facer
In a small town, there is only one priest. There is a legend in this town that people who get married in September 1 will be blessed by God of love, but a priest will hold a ceremony. The ceremony was either held at the beginning of the wedding, or the wedding was just over. Now we know that there are n weddings, which tell you the start time and end time of each, and the time required to hold the ceremony. Ask the priest if he can attend all the weddings. If yes, give out a solution.
For every wedding, we can abstract it into a point pair.
For conflicting points, we can consider it as limiting the relationship between A and B.
In this way, this question becomes a 2-SAT question.
Then follow the routine
Using tarjan to scale down points, brute force reverse graphs, and Topology Sorting
#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<vector>#include<queue>#define Pair pair<int,int>#define F first#define S secondusing namespace std;const int MAXN=1e6+10;//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)char buf[1<<20],*p1=buf,*p2=buf;inline int read(){ char c=getchar();int x=0,f=1; while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f;}Pair P[MAXN];bool check(int x,int y){ if((P[x].S<=P[y].F)||(P[x].F>=P[y].S)) return 0; else return 1;}struct node{ int u,v,nxt;}edge[MAXN];int head[MAXN],num=1;inline void AddEdge(int x,int y){ edge[num].u=x; edge[num].v=y; edge[num].nxt=head[x]; head[x]=num++;}int dfn[MAXN],low[MAXN],vis[MAXN],color[MAXN],colornum=0,tot;stack<int>s;void tarjan(int now){ dfn[now]=low[now]=++tot; vis[now]=1; s.push(now); for(int i=head[now];i!=-1;i=edge[i].nxt) { if(!dfn[edge[i].v]) tarjan(edge[i].v),low[now]=min(low[now],low[edge[i].v]); else if(vis[edge[i].v]) low[now]=min(low[now],dfn[edge[i].v]); } if(dfn[now]==low[now]) { int h;colornum++; do { h=s.top();s.pop(); color[h]=colornum; vis[h]=0; }while(h!=now); }}vector<int>E[MAXN];int enemy[MAXN],inder[MAXN],ans[MAXN];void Topsort(){ queue<int>q; for(int i=1;i<=colornum;i++) if(inder[i]==0) q.push(i); while(q.size()!=0) { int p=q.front();q.pop(); if(!ans[p]) ans[p]=1,ans[enemy[p]]=-1; for(int i=0;i<E[p].size();i++) { inder[E[p][i]]--; if(inder[E[p][i]]==0) q.push(E[p][i]); } }}int main(){ #ifdef WIN32 freopen("a.in","r",stdin); #else #endif memset(head,-1,sizeof(head)); int N=read(); for(int i=1;i<=N;i++) { int a,b,c,d,len; scanf("%d:%d %d:%d %d",&a,&b,&c,&d,&len); P[i].F=a*60+b; P[i].S=a*60+b+len; P[i+N].F=c*60+d-len; P[i+N].S=c*60+d; } for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j) continue; if(check(i,j)) AddEdge(i,j+N); if(check(i,j+N)) AddEdge(i,j); if(check(i+N,j)) AddEdge(i+N,j+N); if(check(i+N,j+N)) AddEdge(i+N,j); } } for(int i=1;i<=N;i++) if(!dfn[i]) tarjan(i); for(int i=1;i<=N;i++) if(color[i]==color[i+N]) {printf("NO\n");return 0;} printf("YES\n"); for(int i=1;i<=N;i++) enemy[color[i]]=color[i+N], enemy[color[i+N]]=color[i]; for(int i=1;i<=N<<1;i++) { for(int j=head[i];j!=-1;j=edge[j].nxt) { if(color[i]!=color[edge[j].v]) { E[color[edge[j].v]].push_back(color[i]); inder[color[i]]++; } } } Topsort(); for(int i=1;i<=N;i++) { if(ans[color[i]]==1) printf("%.2d:%.2d %.2d:%.2d\n",P[i].F/60,P[i].F%60,P[i].S/60,P[i].S%60); else printf("%.2d:%.2d %.2d:%.2d\n",P[i+N].F/60,P[i+N].F%60,P[i+N].S/60,P[i+N].S%60); } return 0; }