POJ 3683 Priest John's Busiest Day

传送门

AcWing 371 牧师约翰最忙碌的一天

POJ 不支持 C++11 简直人间苦难

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> p;
const int maxn(2e3 + 10);
const int maxm(4e6 + 10);
int ecnt, head[maxn];
int tim, dfn[maxn], low[maxn];
int scnt, id[maxn];
bool vis[maxn];
stack<int> st;
vector<p> seg;

struct edge {
    int to, nxt;
} edges[maxm];

template<typename T>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

void addEdge(int u, int v)
{
    edges[ecnt].to = v;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

inline int ls(int id)
{
    return id << 1;
}

inline int rs(int id)
{
    return id << 1 | 1;
}

int _stoi(string s)
{
    int sum = 0;
    while (not s.empty() and s[0] == '0') {
        s.erase(0, 1);
    }
    while (not s.empty()) {
        sum = sum * 10 + s[0] - '0';
        s.erase(0, 1);
    }
    return sum;
}

string _to_string(int v)
{
    string s;
    do {
        s.push_back('0' + (v % 10));
        v /= 10;
    } while (v);
    return string(s.rbegin(), s.rend());
}

string time_to_string(int t)
{
    int h = t / 60;
    int m = t % 60;
    string sh = (h < 10 ? "0" : "") + _to_string(h);
    string sm = (m < 10 ? "0" : "") + _to_string(m);
    return sh + ":" + sm;
}

int getTime(const string& s)
{
    int h = _stoi(s.substr(0, 2));
    int m = _stoi(s.substr(3));
    return h * 60 + m;
}

bool overlap(p a, p b)
{
    if (a.first > b.first) {
        swap(a, b);
    }
    return a.second > b.first;
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++tim;
    st.push(u);
    vis[u] = true;
    for (int i = head[u]; compl i; i = edges[i].nxt) {
        int v = edges[i].to;
        if (not dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if (vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        ++scnt;
        int v = -1;
        do {
            v = st.top();
            st.pop();
            vis[v] = false;
            id[v] = scnt;
        } while (u not_eq v);
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    memset(head, -1, sizeof head);
    int n = read<int>();
    string beg, end;
    for (int i = 0; i < n; ++i) {
        cin >> beg >> end;
        int t = read<int>();
        int a = getTime(beg), b = getTime(end);
        seg.push_back(p(a, a + t));
        seg.push_back(p(b - t, b));
    }
    for (int i = 0; i < n; ++i) {
        for (int j = i + 1; j < n; ++j) {
            if (overlap(seg[ls(i)], seg[ls(j)])) {
                addEdge(ls(i), rs(j));
                addEdge(ls(j), rs(i));
            }
            if (overlap(seg[ls(i)], seg[rs(j)])) {
                addEdge(ls(i), ls(j));
                addEdge(rs(j), rs(i));
            }
            if (overlap(seg[rs(i)], seg[ls(j)])) {
                addEdge(rs(i), rs(j));
                addEdge(ls(j), ls(i));
            }
            if (overlap(seg[rs(i)], seg[rs(j)])) {
                addEdge(rs(i), ls(j));
                addEdge(rs(j), ls(i));
            }
        }
    }
    for (int i = 0; i < n * 2; ++i) {
        if (not dfn[i]) {
            tarjan(i);
        }
    }
    bool flag = false;
    for (int i = 0; i < n; ++i) {
        if (id[ls(i)] == id[rs(i)]) {
            flag = true;
            break;
        }
    }
    if (flag) {
        puts("NO");
    } else {
        puts("YES");
        for (int i = 0; i < n; ++i) {
            if (id[ls(i)] < id[rs(i)]) {
                cout << time_to_string(seg[ls(i)].first) << " " << time_to_string(seg[ls(i)].second) << endl;
            } else {
                cout << time_to_string(seg[rs(i)].first) << " " << time_to_string(seg[rs(i)].second) << endl;
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/singularity2u/p/13991532.html