BZOJ3495 PA2010 Riddle 【2-sat】

题目链接

BZOJ3495

题解

每个城市都有选和不选两种情况,很容易考虑到2-sat
边的限制就很好设置了,主要是每个郡只有一个首都的限制
我们不可能两两之间连边,这样复杂度就爆炸了

于是乎就有了一个非常厉害的方法:
前缀后缀和
我们令\(1\)表示选,\(0\)表示不选,维护一个郡的前缀和、后缀和
一个点如果作为首都,那么它和它对应前缀后缀和的位置一定为\(1\),且之前的位置一定为\(0\)
然后再加上一些前缀后缀和固有的限制

就可以\(A\)
这样的建图太神了

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 6000005,maxv = 1000005,maxm = 15000005,INF = 1000000000;
inline int read(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    return out * flag;
}
int n,m,K,N,pre[maxv][2],sa[maxv][2],p[maxv][2];
int dfn[maxn],low[maxn],Scc[maxn],st[maxn],top,scci,cnt;
int h[maxn],ne;
struct EDGE{int to,nxt;}ed[maxm];
inline void build(int u,int v){
    ed[++ne] = (EDGE){v,h[u]}; h[u] = ne;
}
void dfs(int u){
    dfn[u] = low[u] = ++cnt;
    st[++top] = u;
    Redge(u){
        if (!dfn[to = ed[k].to]){
            dfs(to);
            low[u] = min(low[u],low[to]);
        }
        else if (!Scc[to]) low[u] = min(low[u],dfn[to]);
    }
    if (dfn[u] == low[u]){
        scci++;
        do{
            Scc[st[top]] = scci;
        }while (st[top--] != u);
    }
}
int main(){
    n = read(); m = read(); K = read();
    REP(i,n) p[i][0] = ++N,p[i][1] = ++N;
    int a,b;
    while (m--){
        a = read(); b = read();
        build(p[a][0],p[b][1]);
        build(p[b][0],p[a][1]);
    }
    int x,u;
    while (K--){
        x = read();
        REP(i,x){
            pre[i][0] = ++N,pre[i][1] = ++N;
            sa[i][0] = ++N,sa[i][1] = ++N;
        }
        REP(i,x){
            u = read();
            build(p[u][1],pre[i][1]); build(pre[i][0],p[u][0]);
            build(p[u][1],sa[i][1]); build(sa[i][0],p[u][0]);
            if (i < x){
                build(p[u][1],sa[i + 1][0]); build(sa[i + 1][1],p[u][0]);
                build(pre[i][1],pre[i + 1][1]); build(pre[i + 1][0],pre[i][0]);
            }
            else build(pre[i][0],pre[i][1]);
            if (i > 1){
                build(p[u][1],pre[i - 1][0]); build(pre[i - 1][1],p[u][0]);
                build(sa[i][1],sa[i - 1][1]); build(sa[i - 1][0],sa[i][0]);
            }
            else build(sa[i][0],sa[i][1]);
        }
    }
    for (int i = 1; i <= N; i++) if (!dfn[i]) dfs(i);
    for (int i = 1; i <= N; i += 2)
        if (Scc[i] == Scc[i + 1]){
            puts("NIE");
            return 0;
        }
    puts("TAK");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Mychael/p/9091629.html
今日推荐