1. 程式人生 > >poj3683 Priest John's Busiest Day

poj3683 Priest John's Busiest Day

scanf val cc++ markdown str false include sin struct

不用topsort的,我也不知道為啥。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
    int fro, too;
}nd[2005];
struct Edge{
    int too, nxt, val;
}edge[4000005];
int uu, vv, ww, n, hea[2005], cnt, dfn[2005], loo[2005], scc, bel[2005];
int sta[2005], din, ind;
bool ins[2005
]; bool pd(int x, int y){ if(nd[x].fro<=nd[y].fro && nd[x].too>nd[y].fro) return true; swap(x, y); if(nd[x].fro<=nd[y].fro && nd[x].too>nd[y].fro) return true; return false; } void add_edge(int fro, int too){ edge[++cnt].nxt = hea[fro]; edge[cnt].too = too; hea[fro] = cnt; } void
tarjan(int u){ dfn[u] = loo[u] = ++ind; sta[++din] = u; ins[u] = true; for(int i=hea[u]; i; i=edge[i].nxt){ int t=edge[i].too; if(!dfn[t]){ tarjan(t); loo[u] = min(loo[u], loo[t]); } else if(ins[t]) loo[u] = min(loo[u], dfn[t]); } int
j; if(dfn[u]==loo[u]){ scc++; do{ j = sta[din--]; ins[j] = false; bel[j] = scc; }while(dfn[j]!=loo[j]); } } int main(){ cin>>n; for(int i=0; i<n; i++){ scanf("%d:%d", &uu, &vv); uu = uu * 60 + vv; scanf("%d:%d", &vv, &ww); vv = vv * 60 + ww; scanf("%d", &ww); nd[2*i].fro = uu, nd[2*i].too = uu + ww; nd[2*i+1].fro = vv - ww, nd[2*i+1].too = vv; } for(int i=0; i<n+n; i++) for(int j=i+1; j<n+n; j++) if(pd(i, j)){ add_edge(i, j^1); add_edge(j, i^1); } for(int i=0; i<n+n; i++) if(!dfn[i]) tarjan(i); for(int i=0; i<n+n; i+=2) if(bel[i]==bel[i^1]){//說明以前有要求既不能選i又不能選i^1 printf("NO\n"); return 0; } printf("YES\n"); for(int i=0; i<n+n; i+=2){ int id=bel[i]<bel[i^1]?i:i+1;//為什麽要這樣做?id小說明先求出來,在圖裏是靠近下遊的,影響小 printf("%02d:%02d %02d:%02d\n", nd[id].fro/60, nd[id].fro%60, nd[id].too/60, nd[id].too%60); } return 0; }

poj3683 Priest John's Busiest Day