HDU - 3062 - Party(2-SAT)

链接:

https://vjudge.net/problem/HDU-3062

题意:

有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以列席。在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同时出现在聚会上的。有没有可能会有n 个人同时列席?

思路:

2-SAT模板,考虑两个不能同时存在的夫妻,他们的对象可以同时存在,加到边上。
训练指南的模板

代码:

//#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<map>
#include<stack>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 20071027;
const int MAXN = 1e3+10;
int Next[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};

struct TwoSAT
{
    int n;
    vector<int> G[MAXN*2];
    bool mark[MAXN*2];
    int S[MAXN*2], c;

    bool dfs(int x)
    {
        if (mark[x^1]) return false;
        if (mark[x]) return true;
        mark[x] = true;
        S[c++] = x;
        for (int i = 0;i < (int)G[x].size();i++)
            if (!dfs(G[x][i])) return false;
        return true;
    }

    void init(int n)
    {
        this->n = n;
        for (int i = 0;i < 2*n;i++)
            G[i].clear();
        memset(mark, 0, sizeof(mark));
    }

    void add_clause(int x, int xval, int y, int yval)
    {
        // x = xval or y = yval
        x = x*2+xval;
        y = y*2+yval;
        G[x^1].push_back(y);
        G[y^1].push_back(x);
    }

    bool solve()
    {
        for (int i = 0;i < n*2;i += 2)
        {
            if (!mark[i] && !mark[i^1])
            {
                c = 0;
                if (!dfs(i))
                {
                    while(c > 0) mark[S[--c]] = false;
                    if (!dfs(i^1)) return false;
                }
            }
        }
        return true;
    }
}sat;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n, m;
    while(cin >> n >> m)
    {
        sat.init(n);
        int x1, x2, y1, y2;
        for (int i = 1;i <= m;i++)
        {
            cin >> x1 >> x2 >> y1 >> y2;
            sat.add_clause(x1, y1^1, x2, y2^1);
        }
        if (sat.solve())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/12132790.html
今日推荐