• 洛谷 P6378 [PA2010]Riddle


    传送门

    对于每个点在其所在的部分中考虑,每个点用 (a_i) 来表示,对其新增一个状态 (pre_i) 表示在该部分前 (i) 个点中是否存在被选为关键点。

    (a_i longrightarrow pre_{a_i} , pre_{a_i}^prime longrightarrow a_i^prime)

    (pre_{a_{i-1}} longrightarrow pre_{a_i} , pre_{a_i}^prime longrightarrow pre_{a_{i-1}}^prime)

    (pre_{a_{i-1}} longrightarrow a_i^prime , a_i longrightarrow pre_{a_{i-1}}^prime)

    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    using p = pair<int, int>;
    const double pi(acos(-1));
    const int inf(0x3f3f3f3f);
    const int mod(1e9 + 7);
    const int maxn(4e6 + 10);
    const int maxm(1e7 + 10);
    int ecnt, head[maxn], a[maxn];
    int tim, dfn[maxn], low[maxn];
    int scnt, id[maxn];
    bool vis[maxn];
    stack<int> st;
    
    struct edge {
        int to, nxt;
    } edges[maxm];
    
    template<typename T = int>
    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);
    }
    
    inline int getPrev(int x, int n, bool prev1)
    {
        return x + (prev1 ? n * 2 : n * 3);
    }
    
    void addEdge(int u, int v)
    {
        edges[ecnt].to = v;
        edges[ecnt].nxt = head[u];
        head[u] = ecnt++;
    }
    
    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);
        }
    }
    
    bool check(int n)
    {
        for (int i = 1; i <= n; ++i) {
            if (id[i] == id[i + n] or id[getPrev(i, n, true)] == id[getPrev(i, n, false)]) {
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
    #ifdef ONLINE_JUDGE
    #else
        freopen("input.txt", "r", stdin);
    #endif
        memset(head, -1, sizeof head);
        int n = read(), m = read(), k = read();
        while (m--) {
            int u = read(), v = read();
            addEdge(u + n, v);
            addEdge(v + n, u);
        }
        while (k--) {
            int w = read();
            for (int i = 1; i <= w; ++i) {
                a[i] = read();
                addEdge(a[i], getPrev(a[i], n, true));
                addEdge(getPrev(a[i], n, false), a[i] + n);
            }
            for (int i = 2; i <= w; ++i) {
                addEdge(getPrev(a[i - 1], n, true), getPrev(a[i], n, true));
                addEdge(getPrev(a[i], n, false), getPrev(a[i - 1], n, false));
                addEdge(getPrev(a[i - 1], n, true), a[i] + n);
                addEdge(a[i], getPrev(a[i - 1], n, false));
            }
        }
        for (int i = 1; i <= n * 4; ++i) {
            if (not dfn[i]) {
                tarjan(i);
            }
        }
        puts(check(n) ? "TAK" : "NIE");
        return 0;
    }
    
  • 相关阅读:
    用Keytool和OpenSSL生成和签发数字证书
    Maven 的插件和生命周期的绑定
    MySQL 存储过程基本函数
    MySQL 存储过程游标
    MySQL 存储过程控制语句
    MySQL 存储过程
    Shell 编程基础之 && 与 ||
    Shell 编程基础之 [ 与 [[ 的异同
    Linux 任务控制
    《深入理解Java虚拟机》笔记3
  • 原文地址:https://www.cnblogs.com/singularity2u/p/13994759.html
Copyright © 2020-2023  润新知