题目:
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 10115 | Accepted: 3462 | 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
分析参挑战
代码:
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1010;
const int maxv=2010;
int n,s[maxn],t[maxn],d[maxn];///换算成分钟之后的时间
int v,belong[maxv];///顶点数 所属强连通分量拓扑序
vector<int> g[maxv];
vector<int> rg[maxv];///反向图
int vs[maxv],num;///后序遍历顶点标号 下标num属于0 - v-1
bool used[maxv];
inline void adde(int a,int b){///a->b
g[a].push_back(b); rg[b].push_back(a);
}
inline void dfs(int v){
used[v]=1;
for(int i=0;i<g[v].size();++i) if(!used[g[v][i]]) dfs(g[v][i]);
vs[num++]=v;
}
inline void rdfs(int v,int k){
used[v]=1;
belong[v]=k;
for(int i=0;i<rg[v].size();++i) if(!used[rg[v][i]]) rdfs(rg[v][i],k);
}
int scc(){///返回缩点之后点数
memset(used,0,sizeof(used));
num=0;
for(int i=0;i<v;++i) if(!used[i]) dfs(i);///得到后序标号
memset(used,0,sizeof(used));
int k=0;
for(int i=v/*num*/-1;i>=0;--i) if(!used[vs[i]]) rdfs(vs[i],k++);
return k;
}
int main(){///G++ C++大概都是500ms
int q,w,e,r,y;
while(scanf("%d",&n)==1){
for(int i=0;i<n;++i){
scanf("%d:%d %d:%d %d",&q,&w,&e,&r,&y);
///printf("%d:%d %d:%d %d\n",q,w,e,r,y);
s[i]=q*60+w; t[i]=e*60+r; d[i]=y;
}
v=n*2;
for(int i=0;i<n;++i){
for(int j=0;j<i;++j){
if(min(s[i]+d[i],s[j]+d[j])>max(s[i],s[j])){
adde(i,n+j); adde(j,n+i);
}
if(min(s[i]+d[i],t[j])>max(s[i],t[j]-d[j])){
adde(i,j); adde(n+j,n+i);
}
if(min(t[i],s[j]+d[j])>max(t[i]-d[i],s[j])){
adde(n+i,n+j); adde(j,i);
}
if(min(t[i],t[j])>max(t[i]-d[i],t[j]-d[j])){
adde(n+i,j); adde(n+j,i);
}
}
}
scc();
int f=1;
for(int i=0;i<n;++i){
if(belong[i]==belong[n+i]){
f=0; printf("NO\n");
}
}
if(!f) continue;
printf("YES\n");
for(int i=0;i<n;++i){
if(belong[i]>belong[n+i])
printf("%02d:%02d %02d:%02d\n",s[i]/60,s[i]%60,(s[i]+d[i])/60,(s[i]+d[i])%60);
else
printf("%02d:%02d %02d:%02d\n",(t[i]-d[i])/60,(t[i]-d[i])%60,t[i]/60,t[i]%60);
}
}
return 0;
}