【Lightoj 1251 Forming the Council】& Tarjan & 2

来源:互联网 发布:python 按键精灵 编辑:程序博客网 时间:2024/06/06 20:13

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

+1 -3 the voter wants member 1 to be kept in the council or member 3 to be thrown out
+2 +3 the voter wants member 2 to be kept in the council or member 3 to be kept in the council
-1 -2 the voter wants member 1 to be thrown out or member 2 to be thrown out
-4 +1 the voter wants member 4 to be thrown out or member 1 to be kept in the council

A voter will be satisfied if at least one of his wishes becomes true. Now your task is to form the council such that all the voters are happy.

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

Each case starts with a line containing two 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 is yes, print another line containing the number of members in the council followed by the members in ascending order. And print a single space between two numbers. There can be many solutions. Any valid one will do.

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
Sample Output
Case 1: Yes
2 2 3
Case 2: No
Case 3: Yes
0
Hint
This is a special judge problem. Wrong output format may cause wrong answer.

题意 : 给出 n 对票,+a 代表留, -a 代表不留,问最后 m 个人中最多留下几人,或的关系,对于每对数,只能满足其中的一个

思路: 2 - SAT ,又是模板题?(我之前学了假图论~QAQ

AC代码:

#include<bits/stdc++.h>using namespace std;const int MAX = 2e4 + 10,INF = 0x3f3f3f3f;struct node{    int to,next;}st[MAX * 10],stt[MAX * 10];int nl, head[MAX],nll, Head[MAX];int d[MAX],lo[MAX],sc[MAX], sp[MAX], top, num, pl;int p[MAX], color[MAX], in[MAX],ans[MAX];bool vis[MAX];int n, m;void add(int v, int u){    st[nl].to = u, st[nl].next = head[v], head[v] = nl++;}void Add(int v, int u){    stt[nll].to = u, stt[nll].next = Head[v], Head[v] = nll++;}void init(){    top = num = pl = nl = nll = 0;    memset(head, -1, sizeof head);    memset(d, -1, sizeof d);    memset(vis, 0, sizeof vis);    memset(Head, -1, sizeof Head);    memset(in, 0, sizeof in);    memset(color, 0, sizeof color);}void Tarjan(int x){    d[x] = lo[x] = ++pl;    vis[x] = true, sp[top++] = x;    for(int i = head[x]; i != -1; i = st[i].next){        int o = st[i].to;        if(d[o] == -1){            Tarjan(o);            lo[x] = min(lo[x], lo[o]);        }        else if(vis[o]) lo[x] = min(lo[x], d[o]);    }    if(d[x] == lo[x]){        int o;        num++;        do{            o = sp[--top], vis[o] = false, sc[o] = num;        }        while(o != x);    }}void Top(){    queue<int> q;    for(int i = 1; i <= num; i++)        if(in[i] == 0) q.push(i);    while(! q.empty()){        int x = q.front(); q.pop();        if(color[x] == 0) color[x] = 1, color[p[x]] = 2;        for(int i = Head[x]; i != -1; i = stt[i].next){            int o = stt[i].to;            if(--in[o] == 0) q.push(o);        }    }}bool solve(){    for(int i = 1; i <= 2 * n; i++)        if(d[i] == -1) Tarjan(i);    for(int i = 1; i <= n; i++){        if(sc[i] == sc[i+n]) return false;        p[sc[i]] = sc[i+n], p[sc[i+n]] = sc[i];    }    for(int i = 1; i <= 2 * n; i++)        for(int j = head[i]; j != -1; j = st[j].next)            if(sc[i] != sc[st[j].to])                Add(sc[st[j].to], sc[i]), in[sc[i]]++;    Top();    return true;}void input(){    scanf("%d %d", &m, &n);    for(int i = 1; i <= m; i++){        int x,y;        scanf("%d %d", &x, &y);        if(x > 0 && y > 0) add(x + n, y), add(y + n, x);        else if(x > 0 && y < 0) add(x + n, -y + n), add(-y, x);        else if(x < 0 && y > 0) add(-x, y), add(y + n, -x + n);        else add(-x, -y + n), add(-y, -x + n);    }}int main(){    int T,cas = 0;    scanf("%d", &T);    while(T--){        init();        input();        if(solve()){            int ll = 0;            for(int i = 1; i <= n; i++)                if(color[sc[i]] == 1) ans[++ll] = i;            printf("Case %d: Yes\n", ++cas);            printf("%d", ll);            for(int i = 1; i <= ll; i++) printf(" %d", ans[i]);            printf("\n");        }        else printf("Case %d: No\n", ++cas);    }    return 0;}
阅读全文
0 0
原创粉丝点击