Priest John's busiest day
Time limit:2000 ms |
|
Memory limit:65536 K |
Total submissions:8144 |
|
Accepted:2769 |
|
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 Poj founder monthly contest-2008.08.31, dagger and Facer |
2-Sat
1 ~ N indicates that the I-th ceremony is starting, n + 1 ~ 2n indicates end
Two time periods I and j conflict :~ I recommend ~ J
2-The principle of SAT itself is:
If I is set up, J is set up, and an edge is connected.
Then, you can use the strongly connected component to shrink the Dag.
Obviously, the elements in each big point are not the same. line I-> J indicates that if I is set up, J is set up.
# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <functional> # include <iostream> # include <cmath> # include <cctype> # include <ctime> # include <vector> using namespace STD; # define for (I, n) for (INT I = 1; I <= N; I ++) # define fork (I, k, n) for (INT I = K; I <= N; I ++) # define rep (I, n) for (INT I = 0; I <n; I ++) # define Ford (I, n) for (INT I = N; I --) # define repd (I, n) for (INT I = N; I> = 0; I --) # define forp (x) for (I Nt p = pre [X]; P = next [p]) # define lson (x <1) # define rson (x <1) + 1) # define MEM (a) memset (A, 0, sizeof (a); # define Memi (a) memset (A, 127, sizeof ()); # define Memi (a) memset (A, 128, sizeof (a); # define Inf (2139062143) # define F (100000007) # define maxn (1000 + 10) # define maxn (2000 + 10) long MUL (long a, long B) {return (A * B) % F;} long add (long, long long B) {return (a + B) % F;} long sub (long Long A, long B) {return (a-B + (a-B)/f * F + F) % F;} typedef long ll; Class SSC {public: int N, B [maxn], num [maxn]; vector <int> G [maxn], RG [maxn]; // figure, reverse graph vector <int> vs; // The following figure traverses the vertex list void MEM (INT _ n) {n = _ n; MEM (Num) for (I, n) G [I]. clear (), RG [I]. clear ();. clear ();} void addedge (int u, int v) {G [u]. push_back (V); RG [v]. push_back (U);} void DFS (int x) {B [x] = 1; rep (I, G [X]. size () {If (! B [G [x] [I]) DFS (G [x] [I]);}. push_back (x);} void RDFS (int x, int K) {B [x] = 1; num [x] = K; rep (I, RG [X]. size () {If (! B [RG [x] [I]) RDFS (RG [x] [I], k) ;}} int SSC () {MEM (B) for (I, n) if (! B [I]) DFS (I); MEM (B) int K = 0; repd (I, vs. Size ()-1) if (! B [vs [I]) RDFS (vs [I], ++ K); Return K ;}}; class two_sat // (A |! B) & (c | B )&&... {public: int N; SSC s; void MEM (INT _ n) {n = _ n; S. mem (2 * n);} int no (int u) {If (u> 0) return U; else return N-U;} void addedge (int u, int V) // (B branch ~ C): B-c {S. addedge (NO (-U), no (v); S. addedge (NO (-V), no (u) ;}} s; bool cross (int A, int B, int C, int d) // interval [, b] [c, d] Intersection {If (min (B, d)> MAX (a, c) return 1; return 0;} int N, s [maxn], t [maxn], d [maxn]; int main () {// freopen ("poj1_3_template.in", "r", stdin ); // freopen (". out "," W ", stdout); CIN> N; For (I, n) {int HH, mm, HH2, mm2; scanf (" % d: % d: % d ", & HH, & mm, & HH2, & mm2, & D [I]); s [I] = HH * 60 + mm; t [I] = HH2 * 60 + mm2; t [I]-= d [I];} s. m EM (n); For (I, n) for (J, n) if (I ^ J) {If (Cross (s [I], s [I] + d [I], s [J], s [J] + d [J]) s. addedge (-I,-j); If (Cross (s [I], s [I] + d [I], t [J], T [J] + d [J]) s. addedge (-I, j); If (Cross (T [I], t [I] + d [I], s [J], s [J] + d [J]) s. addedge (I,-j); If (Cross (T [I], t [I] + d [I], t [J], T [J] + d [J]) s. addedge (I, j);} s. s. SSC (); For (I, n) if (S. s. num [I] = S. s. num [n + I]) {printf ("NO \ n"); Return 0;} printf ("Yes \ n"); For (I, n) {If (S. s. num [I] <S. s. num [n + I]) printf (" % 02d: % 02d % 02d: % 02d \ n ", t [I]/60, t [I] % 60, (t [I] + d [I]) /60, (t [I] + d [I]) % 60); // I-> ~ I indicates that I is false else printf ("% 02d: % 02d % 02d: % 02d \ n", s [I]/60, s [I] % 60, (s [I] + d [I])/60, (s [I] + d [I]) % 60);} return 0 ;}
Poj 3683 (Priest John's busiest day-Strongly Connected Component Solving 2-Sat) [template: 2-Sat]