• [洛谷P4171][JSOI2010]满汉全席


    题目大意:有$n$个点,每个点可以选或不选,有$m$组约束,形如$a,u,b,v$,表示$u=a,v=b$中至少要满足一个条件,问是否存在一组解,多组询问

    题解:$2-SAT$,感觉是板子题呀,最后判断一下每一个点选与不选是否在同一个强连通分量内即可

    卡点:

    C++ Code:

    #include <algorithm>
    #include <cstdio>
    #include <iostream>
    #define maxn 210
    #define maxm 2010
    
    int head[maxn], cnt;
    struct Edge {
    	int to, nxt;
    } e[maxm];
    inline void addedge(int a, int b) {
    	e[++cnt] = (Edge) { b, head[a] }; head[a] = cnt;
    }
    
    int Tim, n, m, nn;
    inline int getpos(int a, int b) { return a * n + b; }
    inline void addedge(bool a, int b, bool c, int d) {
    	addedge(getpos(!a, b), getpos(c, d));
    	addedge(getpos(!c, d), getpos(a, b));
    }
    
    int DFN[maxn], low[maxn], idx;
    int S[maxn], top, bel[maxn], scc;
    bool inS[maxn];
    void tarjan(int u) {
    	DFN[u] = low[u] = ++idx;
    	inS[S[++top] = u] = true;
    	int v;
    	for (int i = head[u]; i; i = e[i].nxt) {
    		v = e[i].to;
    		if (!DFN[v]) {
    			tarjan(v);
    			low[u] = std::min(low[u], low[v]);
    		} else if (inS[v]) low[u] = std::min(low[u], DFN[v]);
    	}
    	if (DFN[u] == low[u]) {
    		++scc;
    		do {
    			inS[v = S[top--]] = false;
    			bel[v] = scc;
    		} while (v != u);
    	}
    }
    
    int main() {
    	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
    	std::cin >> Tim;
    	while (Tim --> 0) {
    		std::cin >> n >> m; nn = n << 1;
    		for (int i = 0; i < m; ++i) {
    			static int b, d;
    			static char a, c;
    			std::cin >> a >> b >> c >> d;
    //			std::cout << a << b << ' ' << c << d << std::endl;
    			addedge(a == 'h', b, c == 'h', d);
    		}
    		for (int i = 1; i <= nn; ++i) if (!DFN[i]) tarjan(i);
    		bool solution = true;
    		for (int i = 1; i <= n; ++i) if (bel[i] == bel[i + n]) {
    			solution = false;
    			break;
    		}
    //		for (int i = 1; i <= n; ++i) printf("%d: %d %d
    ", i, bel[i], bel[n + i]);
    		std::cout << (solution ? "GOOD" : "BAD") << '
    ';
    		if (Tim) {
    			__builtin_memset(head, 0, sizeof head), cnt = 0;
    			__builtin_memset(DFN, 0, sizeof DFN), idx = 0;
    			scc = 0;
    		}
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    python流行的原因
    shell rename directory
    shell if [ -d filename]
    eclipse文本编码格式修改为UTF-8
    egrep 第几列开始
    Java double 精度
    BigDecimal 两种方式
    使用SecureCRT连接ubuntu
    eclipse快速查找一个变量、方法或者类被引用的地方
    我的互联网金融行业经验总结
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/10329704.html
Copyright © 2020-2023  润新知