• HDU 3062 Party(2-sat)


    HDU 3062 Party

    题目链接

    2sat模板题

    代码:

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    const int MAXNODE = 2005;
    
    struct TwoSet {
    	int n;
    	vector<int> g[MAXNODE * 2];
    	bool mark[MAXNODE * 2];
    	int S[MAXNODE * 2], sn;
    
    	void init(int tot) {
    		n = tot * 2;
    		for (int i = 0; i < n; i += 2) {
    			g[i].clear();
    			g[i^1].clear();
    		}
    		memset(mark, false, sizeof(mark));
    	}
    
    	void add_Edge(int u, int uval, int v, int vval) {
    		u = u * 2 + uval;
    		v = v * 2 + vval;
    		g[u^1].push_back(v);
    		g[v^1].push_back(u);
    	}
    
    	void delete_Edge(int u, int uval, int v, int vval) {
    		u = u * 2 + uval;
    		v = v * 2 + vval;
    		g[u^1].pop_back();
    		g[v^1].pop_back();
    	}
    
    	bool dfs(int u) {
    		if (mark[u^1]) return false;
    		if (mark[u]) return true;
    		mark[u] = true;
    		S[sn++] = u;
    		for (int i = 0; i < g[u].size(); i++) {
    			int v = g[u][i];
    			if (!dfs(v)) return false;
    		}
    		return true;
    	}
    
    	bool solve() {
    		for (int i = 0; i < n; i += 2) {
    			if (!mark[i] && !mark[i + 1]) {
    				sn = 0;
    				if (!dfs(i)){
    					for (int j = 0; j < sn; j++)
    						mark[S[j]] = false;
    					sn = 0;
    					if (!dfs(i + 1)) return false;
    				}
    			}
    		}
    		return true;
    	}
    } gao;
    
    int n, m;
    
    int main() {
    	while (~scanf("%d", &n)) {
    		gao.init(n);
    		scanf("%d", &m);
    		int u, v, a, b;
    		while (m--) {
    			scanf("%d%d%d%d", &u, &v, &a, &b);
    			gao.add_Edge(u, a, v, b);
    		}
    		printf("%s
    ", gao.solve() ? "YES" : "NO");
    	}
    	return 0;
    }


  • 相关阅读:
    第五章课后练习题
    第四章课后练习
    函数
    变量、常量及类型
    go环境搭建及编辑器安装
    Matplotlib(绘图和可视化)
    Pandas例题(以NBA球队为例)
    Pandas
    Numpy
    jupyter notebook编辑器的用法
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4261510.html
Copyright © 2020-2023  润新知