【Lightoj 1251 Forming the Council】& Tarjan & 2 - SAT
2017-10-17 08:45
621 查看
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代码:
+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; }
相关文章推荐
- LightOj 1251 Forming the Council(2-sat)
- LightOJ - 1251 Forming the Council(2-SAT)
- LightOJ 1251 - Forming the Council 2-sat输出任意一组解
- LightOJ 1251 - Forming the Council(2-SAT二分图)
- lightOJ 1251 - Forming the Council(2-sat)
- Light OJ 1251 Forming the Council 2-SAT输出任意一组解
- Light oj 1251 - Forming the Council 【2-sat】【推断是否存在可行解 + 反向拓扑输出可行解】
- LightOJ 1251 Forming the Council【2-Sat+逆向拓扑排序输出可行解】好题!
- Light oj 1251 - Forming the Council 【2-sat】【判断是否存在可行解 + 反向拓扑输出可行解】
- Light OJ 1251 - Forming the Council (2-SAT模板题)
- light oj 1251 - Forming the Council (2-SAT + 输出任意解)
- LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)
- LightOJ 1313 Protect the Mines (Convex Hull && Minimum Circle)
- lightoj 1341 - Aladdin and the Flying Carpet / lightoj 1236 - Pairs Forming LCM(算术基本定理)
- 【唯一分解定理 && 状态压缩 && 组合数学】LightOJ - 1236 Pairs Forming LCM
- UVALive - 4452 The Ministers' Major Mess(2-SAT)
- hdu 4115 Eliminate the Conflict (3-sat ??? no! it's a 2-sat pro.)
- HDOJ 3861 - The King’s Problem tarjan求强联通分量&缩点&有向图最小路径覆盖(匈牙利)
- LightOJ 1236 - Pairs Forming LCM (LCM·唯一分解)
- lightoj 1251 (Two_Sat)