POJ 3683 priest John ' s busiest day (2-SAT) __ graph theory

Source: Internet
Author: User
Description

John is the only priest in his town. September 1st is the John's busiest day in a year because there are an old legend in the town this the couple who get Marri Ed on this day would be forever blessed by the God of love. This year N couples the married of 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 is a special ceremony on which the couple stand the before and Accept blessings. The i-th couple need Di minutes to finish this ceremony. Moreover, this ceremony must is either at the beginning or the ending of the wedding (i.e. it must is either from Si to Si + Di, or from Ti-di to Ti). Could you tell John how to arrange his schedule so and can present at every special ceremonies of the weddings.

Note of that John can is present at two weddings simultaneously.

Input

The 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 "a" 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 the all ceremonies.

Sample Input

2
08:00 09:00
08:15 09:00 20

Sample Output

YES
08:00 08:30
08:40 09:00

the

There's only one priest in a small town, and now some new people are getting married, a priest is required to host a ceremony, giving the start of each couple's wedding time s and end time t, and the time required for the ceremony (each pair may take a different length of time) d, the priest can be in the beginning of the wedding time D (S to S +D) or the end of the time D (t-d to T) to complete this ritual. Now ask if there is an arrangement for the priest to complete the ceremony of all couples ' weddings, and if so, to export an arrangement.

train of Thought

For each wedding ceremony I, there are only two options for special ceremonies at the beginning or end, so the variable XI x_i can be defined, and the XI X_i is the true representative I I at the beginning of a special ceremony.

For the wedding ceremony i,j i,j, if the Xi,xj x_i,x_j conflict, then ¬xi∨¬xj \neg the x_i \lor the \neg is true.

In the same vein we then draw all the clauses for the beginning and end, the end and the beginning, the end and the end, and the final form of the involution is: (¬XI∨¬XJ) ∧ (XI∨¬XJ) ∧ (¬XI∨XJ) ∧ (XI∨XJ) (\neg x_i \lor \neg x_j) \land (x_i \lor \neg X_j) \land (\neg x_i \lor x_j) \land (x_i \lor x_j)

So the question is turned into a 2-sat problem.

AC Code

#include <iostream> #include <cstring> #include <cstdio> #include <vector> using namespace std;

typedef __int64 LL;

const int MAXN = 4E6+10;
        Class Twosat {public:struct node {int to;
    int next;
    } EDGE[MAXN];
    int head[maxn],tot,n;
    BOOL VIS[MAXN];

    int s[maxn],top;
        void init (int n) {memset (head,-1,sizeof (head));
        tot = 0;
    This->n = n;
        } void Addedge (int u,int v) {edge[tot].to = v;
        Edge[tot].next = Head[u];
    Head[u] = tot++;
        bool Dfs (int x) {if (vis[x^1]) return false;
        if (Vis[x]) return true;
        Vis[x] = true;
        s[top++] = x;
        for (int i=head[x], i!=-1 i=edge[i].next) if (!dfs (edge[i].to)) return false;
    return true;
        BOOL Solve () {memset (vis,false,sizeof (VIS)); for (int i=0; i<n; i+=2) {if (vis[i]| |
     VIS[I^1]) continue;       top = 0;
                if (!dfs (i)) {while (top) vis[s[--top]] = false;
            if (!dfs (i^1)) return false;
    } return true;

}} Sat;
typedef pair<int,int> P;
Vector<p> CNT;

int n; void Build () {for (int i=0; i<n; i+=2) {for (int j=i+2; j<n; j+=2) {if (min) (cnt[
            I].second,cnt[j].second) >max (Cnt[i].first,cnt[j].first)) Sat.addedge (i,j^1), Sat.addedge (j,i^1); if (min (cnt[i].second,cnt[j^1].second) >max (Cnt[i].first,cnt[j^1].first)) Sat.addedge (i,j), Sat.adde
            Dge (i^1,j^1); if (min (cnt[i^1].second,cnt[j].second) >max (Cnt[i^1].first,cnt[j].first)) Sat.addedge (i^1,j^1), SAT.ADDEDG
            E (j,i); if (min (cnt[i^1].second,cnt[j^1].second) >max (Cnt[i^1].first,cnt[j^1].first)) Sat.addedge (i^1,j), Sat.adde
        Dge (J^1,i); int main () {while (~scan)}}F ("%d", &n)) {cnt.clear ();
        n*=2;
        Sat.init (n);
        int h1,m1,h2,m2,d;
            for (int i=0; i<n; i+=2) {scanf ("%d%*c%d%d%*c%d%d", &h1,&m1,&h2,&m2,&d);
            H1 = H1*60+M1;
            H2 = h2*60+m2;
            Cnt.push_back (P (h1,h1+d));
        Cnt.push_back (P (H2-D,H2));
        Build ();
        if (!sat.solve ()) puts ("NO");
            else {puts ("YES");
                for (int i=0; i<n; i+=2) {int index = sat.vis[i]?i:i^1; printf ("%02d:%02d%02d:%02d\n", cnt[index].first/60,cnt[index].first%60,cnt[index].second/60,cnt[index].second%
            60);
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.