1251 - Forming the Council
Time Limit: 2 second(s) Memory Limit: 32 MB

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

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




题意:一个城市的理事会有M个投票人和N个公民组成,当中公民编号从1——N。

如今要建立一个新的理事会,

每一个投票人都给出了自己的意见,如
1,+i +j      表示 i公民 和 j公民 至少有一个留在理事会
2,+i  -j      表示 i公民留在理事会 和  j公民离开理事会 至少有一个成立
3。-i  +j      表示 i公民离开理事会 和  j公民留在理事会 至少有一个成立
4,-i   -j      表示 i公民 和 j公民 至少有一个离开理事会


如今让你找出一种方案选取若干个公民留在理事会。


若不存在方案输出No。

存在输出Yes,在下一行输出选择的公民总数 并输出被选择公民的编号。



思路:2-sat 推断可行解 + 反向拓扑染色输出可行解。不是非常难的题目,没什么好说的。这里仅仅说下建图。


建图:用 i 表示 i 留在理事会,i + N表示 i 离开理事会。


1。+i +j      表示 i公民 和 j公民 至少有一个留在理事会   
addEdge(j + N, i);// j 离开 那么 i 必然留下

addEdge(i + N, j);// i 离开 那么 j 必然留下

2。+i  -j      表示 i公民留在理事会 和  j公民离开理事会 至少有一个成立
addEdge(j, i);// j 留下  i 必然留下

addEdge(i + N, j + N);// i 离开  j 必然离开

3,-i  +j      表示 i公民离开理事会 和  j公民留在理事会 至少有一个成立
addEdge(j + N, i + N);// j 离开  i 必然离开

addEdge(i, j);// i 留下  j 必然留下

4。-i   -j      表示 i公民 和 j公民 至少有一个离开理事会
addEdge(j, i + N);// j 留下 那么 i 必然离开

addEdge(i, j + N);// i 留下 那么 j 必然离开





AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 16000+10
#define MAXM 40000+10
using 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 和 b 至少一个留下
{
addEdge(b + N, a);
addEdge(a + N, b);
}
else if(a > 0 && b < 0)//a留 和 b走 至少成立一个
{
b = -b;
addEdge(b, a);
addEdge(a + N, b + N);
}
else if(a < 0 && b > 0)//a走 和 b留 至少成立一个
{
a = -a;
addEdge(b + N, a + N);
addEdge(a, b);
}
else//a 和 b 至少走一个
{
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(low));
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()//反向建图
{
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];//建立SCC到SCC的映射
int color[MAXN];//染色
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();//反向拓扑
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, 2*N);
solve();
}
return 0;
}

Light oj 1251 - Forming the Council 【2-sat】【推断是否存在可行解 + 反向拓扑输出可行解】的更多相关文章

  1. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  2. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  3. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  4. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  5. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  6. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  7. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

随机推荐

  1. Appium + python - get_attribute获取value操作

    from appium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.w ...

  2. Python 31 TCP协议 、socket套接字

    1.TCP协议 可靠传输,TCP数据包没有长度限制,理论上可以无限长,但是为了保证网络的效率,通常TCP数据包的长度不会超过IP数据包的长度,以确保单个TCP数据包不必再分割. (1)三次握手建链接( ...

  3. html body中的标签

    HTML中的标签有两类 一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup> ...

  4. E - Perfect Number

    Problem description We consider a positive integer perfect, if and only if the sum of its digits is ...

  5. inline-block默认间距解决方法

    方法一: 父元素设置font-size: 0;  行内块元素有文字时再在该元素上设置font-size 方法二: 父元素设置word-spacing为负 方法三: Inline-block   元素浮 ...

  6. android黑科技系列——爆破一款应用的签名验证问题

    一.前言 在之前的文章中说过Android中的安全和破解是相辅相成的,为了防止被破解,很多应用做了一些防护策略,但是防护策略也是分等级,一般简单的策略就是混淆代码和签名校验,而对于签名校验很多应用都是 ...

  7. Android 解析XML—pull解析方式

    在Android中,常见的XML解析器分别为SAX解析器.DOM解析器和PULL解析器,其中PULL解析器小巧轻便,解析速度快,简单易用,非常适合在Android移动设备中使用,Android系统内部 ...

  8. javascript中计算点击多少次

    点击事件:onclick <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  9. centos开机运行级别更改

    1.使用命令切换运行级别/目标 # systemctl isolate multi-user.target //切换到运行级别3,该命令对下次启动无影响,等价于telinit 3 # systemct ...

  10. 我的C++笔记(数据的共享与保护)

    *数据的共享与保护: * .作用域: * 作用域是一个标识符在程序正文中有效的区域.C++中标识符的作用域有函数原型作用域.局部作用域(块作用域).类作用域和命名空间作用域. * ().函数原型作用域 ...