HDU 1824 let ' s go Home (2-sat judgment)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Let ' s go home

Time limit:10000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1616 Accepted Submission (s): 661


Problem description As a child, homesickness is a small stamp, I am in this, mother in the head.
--Yu Guangzhong

Training is hard, the road is bumpy, rest is necessary. After a period of training, LCY decided to let everyone go home to relax, but the training is still going on, LCY came up with the following home rules, each team (three people a team) or the captain left or the remaining two players left at the same time; each pair of players, if player a leaves, then team B must go home to rest, or b leave , a go home. As the number of training team this year to break through the highest record of previous years, management difficulty is quite large, LCY also do not know whether their decision is feasible, so this problem will be handed to you, hehe, benefits ~, FREE * * Rafting day.

The first line of input has two integers, T and m,1<=t<=1000 represent the number of troops, and 1<=m<=5000 represents the logarithm.
Next there are t-lines, three integers per line, indicating the number of team members, and the first player is the team leader.
Then there are m lines, two integers per line, indicating the number of a pair of players.
Each team member belongs to only one team. The player number starts at 0.

Output Yes, otherwise output no, end with EOF.

Sample Input
1 20 1 20 11 22 40 1 23 4 50 30 41 31 4

Sample Output
Yesno

Author whisky
Sourcehdoj Summer Exercise (3)-Hold by wiskey problem-solving ideas: 2-sat is a conflict between the two even side.
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <algorithm> #include <stack> #include <set > #include <map> #define LL Long long using namespace std;const int maxn = 20010;vector<int>g[maxn];int pre[m AXN], LOWLINK[MAXN], SCCNO[MAXN], Dfs_clock, scc_cnt;stack<int> s;void dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock ; S.push (U); int sz = G[u].size (); for (int i=0;i<sz;i++) {int v = g[u][i];if (!pre[v]) {DFS (v); Lowlink[u] = min (Lowlink[u], lowlink[v] );} else if (!sccno[v]) {Lowlink[u] = min (Lowlink[u], pre[v]);}} if (lowlink[u] = = Pre[u]) {scc_cnt++;for (;;) {int x = S.top (); S.pop (); sccno[x] = scc_cnt;if (x = = u) break;}}} void find_scc (int n) {dfs_clock = scc_cnt = 0;memset (sccno, 0, sizeof (SCCNO)), memset (pre, 0, sizeof (PRE)), for (int i=1;i&lt ; =n;i++) if (!pre[i]) DFS (i);} int N, M, T;int main () {while (scanf ("%d%d", &t, &m)!=eof) {int x, y, Z; N =3 * t;for (int i=0;i<=2*n;i++) g[i].clear (); for (int i=1;i<=t;i++) {scanf ("%d%d%d", &x, &y, &z); x + +; y++ ; z++; G[x+n].push_back (y); G[x+n].push_back (z); G[y+n].push_back (x); G[z+n].push_back (x);} for (int i=1;i<=m;i++) {scanf ("%d%d", &x, &y); x + +; y++; G[x].push_back (y + N); G[y].push_back (x + N);} FIND_SCC (2 * N); int flag = 1;for (int i=1;i<=n;i++) {if (sccno[i] = = Sccno[i + N]) {flag = 0;break;}} if (flag) puts ("yes"), Else puts ("no");} 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.