【BZOJ1997】【HNOI2010】Planar(2-SAT,平面图,并查集)

Description

Solution

首先如果边数大于 3n6 3 n − 6 直接输出NO

我们将哈密顿回路看做一个圆,一对边如果全部在圆内相连会相交,那么它们如果全部在圆外连边也会相交。也就是如果其中一条在圆内,那么另一条一定在圆外。这就是个2-SAT问题了。

直接建图后求SCC即可

当然这题也可以用并查集水过。

Code

/************************************************
 * Au: Hany01
 * Date: Apr 5th, 2018
 * Prob: [BZOJ1997][HNOi2010] Planar
 * Email: [email protected]
************************************************/

#include

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

#define op(x) (x + tot)

const int maxn = 203, maxm = 30003, maxk = 200005;

int n, m, u[maxn], beg[maxm], nex[maxk], v[maxk], es, incir[maxn][maxn], co[maxm], dfn[maxm], low[maxm], cosum, tot, tim, isin[maxm], stk[maxm], top, cir[maxn];
PII e[maxm];

inline void add(int uu, int vv) {
    v[++ es] = vv, nex[es] = beg[uu], beg[uu] = es;
}

void Tarjan(int u)
{
    dfn[u] = low[u] = ++ tim;
    stk[++ top] = u, isin[u] = 1;
    for (register int i = beg[u]; i; i = nex[i])
        if (!dfn[v[i]]) Tarjan(v[i]), chkmin(low[u], low[v[i]]);
        else if (isin[u]) chkmin(low[u], dfn[v[i]]);
    if (dfn[u] == low[u])
    {
        ++ cosum;
        while (stk[top] != u) isin[stk[top]] = 0, co[stk[top --]] = cosum;
        -- top, co[u] = cosum, isin[u] = 0;
    }
}

int main()
{
#ifdef hany01
    File("bzoj1997");
#endif

    for (static int T = read(); T --; )
    {
        n = read(), m = read();
        For(i, 1, m) {
            e[i].x = read(), e[i].y = read();
            if (e[i].x > e[i].y) swap(e[i].x, e[i].y);
        }
        Set(cir, 0), Set(incir, 0);
        For(i, 1, n) {
            u[cir[i] = read()] = i;
            if (i > 1) incir[min(cir[i - 1], cir[i])][max(cir[i - 1], cir[i])] = 1;
        }
        if (m > 3 * n - 6) { puts("NO"); continue; }
        incir[min(cir[n], cir[1])][max(cir[n], cir[1])] = 1;
        tot = 0;
        For(i, 1, m)
            if (!incir[e[i].x][e[i].y]) e[++ tot] = e[i];
        Set(beg, 0), es = 0;
        For(i, 1, tot - 1) For(j, i + 1, tot) {
            register int u1 = u[e[i].x], v1 = u[e[i].y], u2 = u[e[j].x], v2 = u[e[j].y];
            if (u1 > v1) swap(u1, v1);
            if (u2 > v2) swap(u2, v2);
            if (u1 > u2) swap(u1, u2), swap(v1, v2);
            if (u1 < u2 && v1 > u2 && v1 < v2 || u1 > u2 && u1 < v2 && v1 > v2)
                add(i, op(j)), add(op(i), j), add(j, op(i)), add(op(j), i);
        }
        Set(dfn, 0), tim = cosum = 0;
        For(i, 1, tot << 1) if (!dfn[i]) Tarjan(i);
        register int mark = 1;
        For(i, 1, tot)
            if (co[i] == co[op(i)]) { mark = 0; break; }
        puts(mark ? "YES" : "NO");
    }

    return 0;
}
//黄鸟翩翩杨柳垂,春风送客使人悲。
//    -- 高适《东平别前卫县李寀少府》

你可能感兴趣的:(BZOJ,省选,2-SAT,平面图)