Poj 3905 perfect Election (2-Sat judgment is simple)

Source: Internet
Author: User

[Question link]

Http://poj.org/problem? Id = 3905

[Idea]

2-simple sat question

【Code]

# Include <iostream> # include <queue> # include <cstdio> # include <cstring> using namespace STD; const int maxn = 1010; const int VN = maxn * 2; const int en = Vn * VN * 2; int n, m; struct edge {int V, next ;}; struct graph {int size, head [VN]; edge e [En]; void Init () {size = 0; memset (Head,-1, sizeof (head) ;}; void addedge (int u, int V) {e [size]. V = V; E [size]. next = head [u]; head [u] = size ++;} g; Class two_sat {public: Bool check (const graph & G, const int N) {SCC (G, 2 * n); For (INT I = 0; I <n; ++ I) if (belong [I] = belong [I + N]) return false; return true;} PRIVATE: void Tarjan (const graph & G, const int U) {int V; low [u] = dfn [u] = ++ idx; STA [top ++] = u; instack [u] = true; For (int e = G. head [u]; e! =-1; E = G. E [e]. next) {v = G. E [e]. v; If (dfn [v] =-1) {Tarjan (G, V); low [u] = min (low [u], low [v]);} else if (instack [v]) {LOW [u] = min (low [u], dfn [v]);} if (dfn [u] = low [u]) {++ bcnt; do {v = sta [-- top]; instack [v] = false; belong [v] = bcnt;} while (u! = V) ;}} void SCC (const graph & G, const int N) {bcnt = idx = Top = 0; memset (dfn,-1, sizeof (dfn )); memset (instack, 0, sizeof (instack); For (INT I = 0; I <n; ++ I) if (dfn [I] =-1) tarjan (G, I);} PRIVATE: int idx, top, bcnt; int dfn [VN], low [VN], belong [VN], Sta [VN]; bool instack [VN];} sat; int main () {int A, B; while (~ Scanf ("% d", & N, & M) {G. init (); For (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B ); if (a> 0 & B> 0) {// ++ -- A; -- B; G. addedge (a + N, B); G. addedge (B + N, A) ;}else if (a> 0 & B <0) {// +-B =-B; -- A; -- B; G. addedge (a + N, B + n); G. addedge (B, A);} else if (a <0 & B> 0) {//-+ A =-A; -- B; G. addedge (a, B); G. addedge (B + N, A + n);} else if (a <0 & B <0) {//--A =-A; B =-B; -- A; -- B; G. addedge (A, B + n); G. addedge (B, A + n) ;}} if (SAT. check (G, n) puts ("1"); else puts ("0");} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.