Uva11294-wedding (2-sat)

Source: Internet
Author: User

option=com_onlinejudge&itemid=8&page=show_problem&problem=2269 "> topic Links


Test Instructions: There are n couples participating in a wedding reception.

All of them sit on the left or right side of a long table. All couples are only able to sit in person, including the bride and groom. The bride can only see the people sitting on her different sides. There is M-to-man over the shelf, and the bride does not want to see them sitting on the same side.

Ask if there is a distribution plan to meet the bride's requirements.

Ideas: 2-sat problem. If each couple is a variable XI. If Xi is true, the wife and the bride sit on the same side; When Xi is false, the husband and the bride sit on the same side. When Xi and XJ are husbands, it is necessary to meet ~xi V ~XJ, which means that two husbands have at most only one sitting on a different side of the bride, while Xi and XJ are wives. The XI V XJ is to be met. Indicates that two wives have at most only one sitting on a different side from the bride. When Xi and XJ are heterosexual, one of the ~xi v XJ or XI V ~XJ is required. Indicates that two of the most are seated on a different side than the bride.

Sum up. is to satisfy the husband ~xi, wife XI. Finally, be aware of initializing mark[1] = 1.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <    algorithm>using namespace Std;const int maxn = 1005;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 < 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 < n * 2; i++) g[i].clear ();        memset (mark, 0, sizeof (Mark));    MARK[1] = 1;        } void Add_clause (int x, int xval, int y, int 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; }};            Twosat solver;int N, m;int main () {while (scanf ("%d%d", &n, &m)) {if (n = = 0 && m = = 0)        Break                 Solver.init (n);        Char A, B;        int xval, Yval, u, v;             while (m--) {scanf ("%d%c%d%c", &u, &a, &v, &b); Xval = (A = = ' h ')?

0:1; Yval = (b = = ' h ')? 0:1; Solver.add_clause (U, xval, V, yval); } if (!solver.solve ()) printf ("Bad luck\n"); else {for (int i = 1; i < n; i++) {printf ("%d%c", I, Solver.mark[2*i]? ' H ': ' W '); if (i = = n-1) printf ("\ n"); else printf (""); }}} return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Uva11294-wedding (2-sat)

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.