Light OJ 1251-forming the Council "2-sat" "Determine if there is a viable solution + reverse topological output feasible solution"

Source: Internet
Author: User

1251-forming the Council
PDF (中文版) Statistics Forum
Time Limit: 2 second (s) Memory Limit: MB

In a city there is N voters, and m people formed the Govt. Council. The council members is numbered from 1 to M. Now, everyone is complaining, the Council is biased. So, they made a plan. The plan is, the voters is given a chance to vote again to form the new council. A vote'll is like ±i±j. ' + ' means the voter wants that member to being in the Council, '-' means the voter doesn ' t want the member To is in the Council. For example, there is 4 voters, they voted like

+1-3 The voter wants member 1 to being kept in the council or member 3 to being thrown out

+2 +3 The voter wants member 2 to being kept in the council or member 3 to being kept in the Council

-1-2 The voter wants member 1 to being thrown out or member 2 to being thrown out

-4 +1 The voter wants member 4 to being thrown out or member 1 to being kept in the Council

A voter'll is satisfied if at least one's wishes becomes true. Now your task was to form the council such, the voters is happy.

Input

Input starts with an integer T (≤20), denoting the number of test cases.

Each case is starts with a line containing, integers n (1≤n≤20000) and m (1≤m≤8000). Each of the next n lines contains a vote in the form ±i±j (1≤i, j≤m).

Output

For each case, print the case number and ' Yes ' if a solution exists, or ' no ' if there is no solution. Then if the result was Yes, print another line containing the number of members in the Council followed by the Scending order. and print a single space between the numbers. There can many solutions. Any valid one would do.

Sample Input Output for Sample Input

3

4 3

+1 +3

+2-1

+2-3

-1-2

4 2

+1-2

+1 +2

-1-2

-1 +2

1 3

+1-3

Case 1:yes

2 2 3

Case 2:no

Case 3:yes

0

Note

This is a special judge problem. Wrong output format may cause wrong answer.

Problem Setter:jane ALAM JAN



Test Instructions: The Council of a city has m voters and N citizens, of which the citizen number is from 1--n. It is time to establish a new council, each voter gave his own opinion, such as 1,+i +j says I citizens and J citizens have at least one left in the Council 2,+i-j represents I citizens stay in Council and J Citizens leave the Council at least one of the established 3,-i +j represents I citizens leave the Council and J citizens stay at the council at least one set up 4,-i-j says I citizens and J citizens have at least one left board
now let's find a way to choose a number of citizens to stay in the council.
If no scheme output is present. There is output yes, the next line outputs the total number of selected citizens and outputs the number of the selected citizen.

idea: 2-sat to determine feasible solution + reverse topological dyeing output feasible solution. Not very difficult topic, there is nothing to say, here only the building map.

map: With I means I stay on board, i + N means I leave the board.
1,+i +j says I citizens and J citizens have at least one left in the Council Addedge (j + N, i); J leave then I must stay
Addedge (i + N, j); I leave then J must stay

2,+i-j represents I citizens stay in Council and J Citizens leave the Council at least one of the establishedAddedge (J, i); J Leave I must stay
Addedge (i + N, j + N); I leave J must leave

3,-i +j represents I citizens leave the Council and J citizens stay at the council at least one set up Addedge (j + N, i + N); J Leave I must leave
Addedge (i, j); I leave J must stay

4,-i-j says I citizens and J citizens have at least one left board Addedge (J, i + N); J left so I must leave
Addedge (i, J + N); I leave so J must leave




AC Code:

#include <cstdio> #include <cstring> #include <queue> #include <stack> #include <vector># Include <algorithm> #define MAXN 16000+10#define maxm 40000+10using namespace std;struct edge{int from, to, next; }; Edge Edge[maxm];int HEAD[MAXN], Edgenum;int LOW[MAXN], Dfn[maxn];int SCCNO[MAXN], Scc_cnt;int dfs_clock;stack<int    > S;bool instack[maxn];int N, m;void init () {edgenum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v) {Edge E = {u, V, Head[u]};    Edge[edgenum] = E; Head[u] = edgenum++;}    void Getmap () {int A, B;        while (m--) {scanf ("%d%d", &a, &b);            if (a > 0 && B > 0)//a and b at least one leaves {Addedge (b + N, a);        Addedge (A + N, b);            } else if (a > 0 && B < 0)//a and b walk at least one {b =-B;            Addedge (b, a);        Addedge (A + N, b + N);        } else if (a < 0 && B > 0)//a go and B stay at least set up a{a = A;            Addedge (b + N, a + N);        Addedge (A, b);            } else//a and B walk at least one {a =-A, B =-B;            Addedge (b, A + N);        Addedge (A, B + N);    }}}void Tarjan (int u, int fa) {int v;    Low[u] = dfn[u] = ++dfs_clock;    S.push (U);    Instack[u] = true;        for (int i = head[u]; I! =-1; i = edge[i].next) {v = edge[i].to;            if (!dfn[v]) {Tarjan (V, u);        Low[u] = min (Low[u], low[v]);    } else if (Instack[v]) low[u] = min (Low[u], dfn[v]);        } if (low[u] = = Dfn[u]) {scc_cnt++;        for (;;) {v = s.top ();            S.pop ();            INSTACK[V] = false;            SCCNO[V] = scc_cnt;        if (v = = u) break;    }}}void find_cut (int l, int r) {memset (low, 0, sizeof);    memset (DFN, 0, sizeof (DFN));    memset (sccno, 0, sizeof (SCCNO));    Memset (Instack, False, sizeof (Instack));    Dfs_clock = scc_cnt = 0;for (int i = l; I <= R; i++) if (!dfn[i]) Tarjan (i,-1);}  Vector<int> g[maxn];int in[maxn];void Suodian ()//reverse build {for (int i = 1; I <= scc_cnt; i++) g[i].clear (), in[i] =    0;        for (int i = 0; i < Edgenum; i++) {int u = Sccno[edge[i].from];        int v = sccno[edge[i].to];    if (U = v) g[v].push_back (u), in[u]++;    }}int k = 1;int fp[maxn];//establishes an SCC to an SCC mapping int color[maxn];//staining void Toposort () {memset (color,-1, sizeof (color));    Queue<int> Q;    for (int i = 1; I <= scc_cnt; i++) if (in[i] = = 0) Q.push (i); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            if (color[u] = = 1) {Color[u] = 1;        Color[fp[u]] = 0;            } for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];        if (--in[v] = = 0) q.push (v);    }}}void Solve () {printf ("Case%d:", k++); for (int i = 1; I <= N; i++) {if (sccno[i] = = Sccno[i+n])        {printf ("no\n");        return;            } else {Fp[sccno[i]] = sccno[i+n];        Fp[sccno[i+n]] = sccno[i];    }} printf ("yes\n");    Suodian ();    Toposort ();//reverse topology int ans = 0;    for (int i = 1; I <= N; i++) {if (color[sccno[i]] = = 1) ans++;    } printf ("%d", ans);    for (int i = 1; I <= N; i++) {if (color[sccno[i]] = = 1) printf ("%d", I); } printf ("\ n");}    int main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &m, &n);        Init ();        Getmap ();        Find_cut (1, N);    Solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Light OJ 1251-forming the Council "2-sat" "Determine if there is a viable solution + reverse topological output feasible solution"

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.