POJ-3683 Priest John's Busiest Day

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 Sito 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 Sito 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 SiTi and DiSi 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

建图是难点啊...然后就是跑Tarjan

#include <cstdio>
#include <stack>
#include <algorithm>
#include <vector>

#define MAX 2010

using namespace std;

int S[MAX], D[MAX], T[MAX];
vector<int> MGraph[MAX];
int DFN[MAX];
int LOW[MAX];
int inStack[MAX];
int tot = 1;
int scc_cnt = 0;
int sccno[MAX];
stack<int> s;
int n;

void add_edge( int from, int to ) {
    //printf( "%d %d\n", from, to );
    MGraph[from].push_back( to );
}

void tarjan( int u ) {
    DFN[u] = LOW[u] = tot++;    // 新进入的节点初始化
    inStack[u] = 1;             // u节点入栈标记
    s.push( u );                // u节点入栈
    // printf( "%d入栈\n", u );

    for( int i = 0; i < MGraph[u].size(); i++ ) {
        int v = MGraph[u][i];
        if( !DFN[v] ) {
            tarjan( v );
            LOW[u] = min( LOW[u], LOW[v] );
        }
        else if( inStack[v] ) {
            LOW[u] = min( LOW[u], DFN[v] );
        }
    }

    // 如果当前点是整个强连通分量子树的最小根
    if( LOW[u] == DFN[u] ) {
        scc_cnt++;
        int cur;
        //printf( "强连通分量: " );
        do {
            cur = s.top();
            sccno[cur] = scc_cnt;
            s.pop();
            inStack[cur] = 0;
            //printf( "%d ", cur );
        } while( cur != u );
        //printf( "\n" );
    }
}

void solve() {
    for( int i = 1; i <= n; i++ ) {
        for( int j = 1; j < i; j++ ) {
            if( min( S[i] + D[i], S[j] + D[j] ) > max( S[i], S[j] ) ) {
                // xi -> xj' ,xj -> xi'
                add_edge( i, j + n );
                add_edge( j, i + n );
            }
            if( min( S[i] + D[i], T[j] ) > max( S[i], T[j] - D[j] ) ) {
                // xi -> xj,xj' -> xi'
                add_edge( i, j );
                add_edge( j + n, i + n );
            }
            if( min( T[i], S[j] + D[j] ) > max( S[j], T[i] - D[i] ) ) {
                //  xi' -> xj',xj -> xi
                add_edge( i + n, j + n );
                add_edge( j, i );
            }
            if( min( T[i], T[j] ) > max( T[i] - D[i], T[j] - D[j] ) ) {
                // xi' -> xj ,xj' -> xi
                add_edge( i + n, j );
                add_edge( j + n, i );
            }
        }
    }

    // 求有向图的强连通分量
    // 注意图中共有2 * n个点
    for( int i = 1; i <= 2 * n; i++ ) {
        if( !DFN[i] ) {
            tarjan( i );
        }
    }

    //for( int i = 1; i <= 2 * n; i++ ) printf( "%d: %d\n", i, sccno[i] );

    // 判断可行性
    for( int i = 1; i <= n; i++ ) {
        if( sccno[i] == sccno[n + i] ) {
            printf( "NO\n" );
            return;
        }
    }

    // 输出可行解
    printf( "YES\n" );
    for( int i = 1; i <= n; i++ ) {
        if( sccno[i] < sccno[i + n] ) // 在仪式开始时举行
            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 );
    }
}

void init() {
    memset( DFN, 0, sizeof( DFN ) );
    memset( LOW, 0, sizeof( LOW ) );
    memset( inStack, 0, sizeof( inStack ) );
    memset( sccno, 0, sizeof( sccno ) );
    for( int i = 0; i < MAX; i++ ) MGraph[i].clear();
    while( !s.empty() ) s.pop();
    tot = 1;
    scc_cnt = 0;
}
int main() {
    while( scanf( "%d", &n ) != EOF ) {
        init();
        for( int i = 1; i <= n; i++ ) {
            int x1, y1, x2, y2;
            char p, q;
            scanf( "%02d:%02d %02d:%02d %d", &x1, &y1, &x2, &y2, &D[i] );
            S[i] = x1 * 60 + y1;
            T[i] = x2 * 60 + y2;
        }
        solve();
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值