"HDU" 1814 peaceful Commission

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1814

Test instructions: N 2-person group, numbered 2n and 2n+1 respectively, each group selected a person to come out, and give the M-bar relationship (x, y) to select X can not choose Y, ask whether can choose 1 people from each group. And the smallest answer to the output dictionary order. (n<=8000, m<=20000)

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include < iostream>using namespace Std;const int n=8005, m=20005;struct E {int next, to;} E[m<<1];int CNT, ihead[n<<1], top, s[n<<1], N, M;bool vis[n<<1];void Add (int u, int v) {e[++cnt]= E {Ihead[u], v}; ihead[u]=cnt; }bool dfs (int x) {if (vis[x^1]) return 0;if (Vis[x]) return 1;vis[x]=1;s[++top]=x;for (int i=ihead[x]; i; i=e[i].next) if (!d FS (e[i].to)) return 0;return 1;} BOOL Work () {int nn=n<<1;for (int i=0, i<nn; i+=2) if (!vis[i] &&!vis[i+1]) {top=0;if (!dfs (i)) {while (top ) vis[s[top--]]=0;if (!dfs (i+1)) return 0;} for (int i=0; i<nn; ++i) if (Vis[i]) printf ("%d\n", i+1); return 1;} int main () {while (~scanf ("%d%d", &n, &m)) {for (int i=0; i<m; ++i) {int x, y; scanf ("%d%d", &x, &y);-- X --y; Add (x, y^1); Add (y, x^1); }if (!work ()) puts ("NIE"), Cnt=top=0;memset (Vis, 0, sizeof (BOOL) * (n<<1)), memset (ihead, 0, sizeof (int) * (n<<1));} return 0;}

  

The problem is obvious 2-sat question = =

For relationships (x, y) is actually satisfying! (X & Y)

That is, when the x=1 y must be 0, that is, the Edge x->y '

When Y=1 x must be 1, that is, the Edge y->x '

Because the DFS algorithm itself is the dictionary order of the smallest = = directly engage in the line = =

(MOM, this algorithm is O (nm) Well qaq seems to have to write tarjan later = = (although the dictionary order minimum can only use this O (nm) algorithm = =? )

"HDU" 1814 peaceful Commission

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.