• 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;
    }
    
  • 相关阅读:
    Winform使用ML.NET时无法加载 DLL“CpuMathNative”问题的解决方法
    离线安装nuget包EPPlus
    码云上webide怎么提交
    EXCEL中自定义格式输入的数据怎么完整复制
    远程桌面剪贴板不好用了
    电脑里明明安装了net4.7但是VS里不显示?
    微信公众号里的音频怎么下载
    Hibernate-ORM:06.Hibernate中三种状态
    Hibernate-ORM:05.Hibernate中的list()和iterator()
    Hibernate-ORM:04.Hibernate中的get()和load()
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13991532.html
Copyright © 2020-2023  润新知