POJ

链接:

https://vjudge.net/problem/POJ-3683

题意:

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.

思路:

把时间用分钟表示,然后两个时间枚举不能交叉的情况。
tarjan缩点,tupo排序求路径

代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 20071027;
const int MAXN = 1e3+10;
int Next[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

struct Node
{
    int l1, r1;
    int l2, r2;
}node[MAXN];
vector<int> G[MAXN*2], Gt[MAXN*2];
int sccnum[MAXN*2], dfn[MAXN*2], low[MAXN*2];
int opp[MAXN*2], disin[MAXN*2], col[MAXN*2];
stack<int> St;
int n, scc_cnt, dfn_clock;

void tarjan(int u)
{
    dfn[u] = low[u] = ++dfn_clock;
    St.push(u);
    for (int i = 0;i < (int)G[u].size();i++)
    {
        int v = G[u][i];
        if (!dfn[v])
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (!sccnum[v])
            low[u] = min(low[u], dfn[v]);
    }

    if (low[u] == dfn[u])
    {
        ++scc_cnt;
        while(true)
        {
            int x = St.top();
            St.pop();
            sccnum[x] = scc_cnt;
            if (x == u)
                break;
        }
    }
}

bool solve()
{
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(sccnum, 0, sizeof(sccnum));
    dfn_clock = scc_cnt = 0;
    for (int i = 0;i < 2*n;i++)
        if (!dfn[i]) tarjan(i);
    for (int i = 0;i < 2*n;i+=2)
    {
        if (sccnum[i] == sccnum[i+1])
            return false;
        opp[sccnum[i]] = sccnum[i+1];
        opp[sccnum[i+1]] = sccnum[i];
    }
    return true;
}

bool check(int l1, int r1, int l2, int r2)
{
    if (r1 <= l2 || r2 <= l1)
        return false;
    return true;
}

void tupo()
{
    memset(disin, 0, sizeof(disin));
    memset(col, -1, sizeof(col));
    for (int i = 0;i < 2*n;i++) Gt[i].clear();
    for (int i = 0;i < 2*n;i++)
    {
        for (int j = 0;j < (int)G[i].size();j++)
        {
            int to = G[i][j];
            if (sccnum[i] == sccnum[to]) continue;
            Gt[sccnum[to]].push_back(sccnum[i]);
            disin[sccnum[i]]++;
        }
    }
    queue<int> que;
    for (int i = 1;i <= scc_cnt;i++)
        if (!disin[i]) que.push(i);
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        if (col[u] == -1)
        {
            col[u] = 1;
            col[opp[u]] = 0;
        }
        for (int i = 0;i < (int)Gt[u].size();i++)
        {
            int to = Gt[u][i];
            if (--disin[to] == 0)
                que.push(to);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    while(~scanf("%d", &n))
    {
        for (int i = 0;i < 2*n;i++)
            G[i].clear();
        int h1, m1, h2, m2, l;
        for (int i = 0;i < n;i++)
        {
            scanf("%d:%d %d:%d %d", &h1, &m1, &h2, &m2, &l);
            int s = h1*60+m1, e = h2*60+m2;
            node[i].l1 = s, node[i].r1 = s+l;
            node[i].l2 = e-l, node[i].r2 = e;
        }
        for (int i = 0;i < n;i++)
        {
            for (int j = i+1;j < n;j++)
            {
                if (check(node[i].l1, node[i].r1, node[j].l1, node[j].r1))
                {
                    G[2*i].push_back(2*j+1);
                    G[2*j].push_back(2*i+1);
                }
                if (check(node[i].l1, node[i].r1, node[j].l2, node[j].r2))
                {
                    G[2*i].push_back(2*j);
                    G[2*j+1].push_back(2*i+1);
                }
                if (check(node[i].l2, node[i].r2, node[j].l1, node[j].r1))
                {
                    G[2*i+1].push_back(2*j+1);
                    G[2*j].push_back(2*i);
                }
                if (check(node[i].l2, node[i].r2, node[j].l2, node[j].r2))
                {
                    G[2*i+1].push_back(2*j);
                    G[2*j+1].push_back(2*i);
                }
            }
        }
        /*
        for (int i = 0;i < 2*n;i++)
        {
            cout << i << ": ";
            for (auto v: G[i])
                cout << v << ' ' ;
            cout << endl;
        }
        */
        if (solve())
        {
            puts("YES");
            tupo();
            for (int i = 0;i < n;i++)
            {
                if (col[sccnum[i*2]] == 1)
                    printf("%02d:%02d %02d:%02d
", node[i].l1/60, node[i].l1%60, node[i].r1/60, node[i].r1%60);
                else
                    printf("%02d:%02d %02d:%02d
", node[i].l2/60, node[i].l2%60, node[i].r2/60, node[i].r2%60);
            }
        }
        else
        {
            puts("NO");
        }
    }

    return 0;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12152935.html