[Codeforces 1239D]Catowise City(2-SAT)
[Codeforces 1239D]Catowise City(2-SAT)
题面
有n个主人,每个主人都有一只猫。每个主人认识一些猫(包括自己的猫)。现在要选出一些人和一些猫,个数均大于0且总共为n,且所有人和所有猫都互不认识。判断是否有解,有解输出任意一组方案.
\(n \leq 10^6\)
分析
如果选择人i参加,那么i认识的猫一定不能参加。那么i认识的猫的主人一定要参加。这样就可以保证少了认识的猫,但是多了认识猫的主人,人数和猫数之和仍为n.
因此可以这样建图:对于人i认识的每只猫的主人j,由i向j连一条有向边,表示选了i就一定要选j。
根据2-SAT的套路,选了一个点-双联通分量中的任意一个点,那么这个v-DCC里的所有点都要被选择。
那么就可以缩点。如果只有一个v-DCC,说明没有猫被选择,不符合题意。
否则选缩点后的图里任意一个出度为0的v-DCC即可。这里面的全选人,剩下的点全选猫。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#define maxn 1000000
using namespace std;
int t,n,m;
vector<int>E[maxn*2+5];
void add_edge(int u,int v){
E[u].push_back(v);
}
int dfn[maxn*2+5];
int low[maxn*2+5];
stack<int>st;
int cnt=0,tim=0;
int bel[maxn*2+5];
bool ins[maxn*2+5];
vector<int>bcc[maxn*2+5];
void tarjan(int x){
dfn[x]=low[x]=++tim;
st.push(x);
ins[x]=1;
for(int y : E[x]){
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}else if(ins[y]){
low[x]=min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]){
cnt++;
int y;
do{
y=st.top();
st.pop();
ins[y]=0;
bel[y]=cnt;
bcc[cnt].push_back(y);
}while(y!=x);
}
}
bool is_jury[maxn*2+5];
void ini(){
for(int i=1;i<=n*2;i++){
E[i].clear();
bcc[i].clear();
bel[i]=dfn[i]=low[i]=0;
is_jury[i]=0;
}
cnt=tim=0;
}
int main(){
int u,v;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
ini();
for(int i=1;i<=m;i++){
scanf("%d %d",&u,&v);
if(u==v) continue;
E[u].push_back(v);
}
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
if(cnt==1) printf("No\n");
else{
printf("Yes\n");
printf("%d %d\n",bcc[1].size(),n-bcc[1].size());
for(int x : bcc[1]) {
is_jury[x]=1;
printf("%d ",x);
}
printf("\n");
for(int i=1;i<=n;i++){
if(!is_jury[i]) printf("%d ",i);
}
printf("\n");
}
}
}
[Codeforces 1239D]Catowise City(2-SAT)的更多相关文章
- Codeforces 1239D. Catowice City
传送门 如果选择 $i$ 当陪审团成员,那么 $i$ 认识的猫一定不能参加 又因为总人数和猫数要为 $n$ ,那么 $i$ 认识的猫 的主人也一定要当陪审团成员(不然总数不够) 所以可以考虑这样构图, ...
- Codeforces Gym 100015C City Driving 离线LCA
City Driving 题目连接: http://codeforces.com/gym/100015/attachments Description You recently started fre ...
- Codeforces 521E - Cycling City(点双连通分量+分类讨论)
Codeforces 题面传送门 & 洛谷题面传送门 大家都是暴力找生成树然后跳路径,代码不到 50 行(暴论)的一说--好,那本蒟蒻决定提供一种代码 150 行,但复杂度也是线性的分类讨论做 ...
- Codeforces Round #328 (Div. 2) D. Super M 虚树直径
D. Super M Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/592/problem/D ...
- Codeforces 592D - Super M - [树的直径][DFS]
Time limit 2000 ms Memory limit 262144 kB Source Codeforces Round #328 (Div. 2) Ari the monster is n ...
- CodeForces - 592D: Super M(虚树+树的直径)
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...
- 【27.66%】【codeforces 592D】Super M
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces 821D Okabe and City
Okabe and City 题解: 将行和列也视为一个点. 然后从普通的点走到行/列的点的话,就代表这行/列已经被点亮了. 然后将费用为0的点建上边. 注意讨论(n,m)非亮的情况下. 代码: #i ...
- codeforces 821 D. Okabe and City(最短路)
题目链接:http://codeforces.com/contest/821/problem/D 题意:n*m地图,有k个位置是点亮的,有4个移动方向,每次可以移动到相邻的点亮位置,每次站在初始被点亮 ...
随机推荐
- c++linux多线程基础知识
http://blog.csdn.net/lovecodeless/article/details/24929273 http://blog.csdn.net/Jiangweihll/article/ ...
- toJSON() 方法,将 Date 对象转换为字符串,并格式化为 JSON 数据格式。
JavaScript toJSON() 方法 定义和用法 toJSON() 方法可以将 Date 对象转换为字符串,并格式化为 JSON 数据格式. JSON 数据用同样的格式就像x ISO-8601 ...
- XML 简介 – 什么是 XML?
XML 指可扩展标记语言(eXtensible Markup Language). XML 被设计用来传输和存储数据. XML 很重要,也很容易学习. <?xml version="1 ...
- 【javascript】生成二维码
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 < ...
- MongoDB操作:update()
@Override public boolean update(String dbName, String collectionName, DBObject oldValue, DBObject ne ...
- input样式去掉苹果手机的默认样式
/*<!---->去掉苹果短的样式*/ input[type="button"], input[type="submit"], input[type ...
- osm(Openstreetmap)数据下载并导入arcgis
https://3nice.cc/2018/07/18/arcgisosm/ https://blog.csdn.net/zimojiang/article/details/80409139 http ...
- Scrapy终端(Scrapy shell)
1.介绍文档:http://scrapy-chs.readthedocs.io/zh_CN/latest/topics/shell.html# 2.终端的启用方式:scrapy shell url u ...
- nmon性能监控
1.nmon下载地址 2../nmon_x86_rhel52 3.根据上面提示的快捷键进行输入即可显示相应的资源耗用情况,如输入:c.m.d(显示cpu.内存.磁盘使用情况) 4.输入数据到文件 ./ ...
- gitblit 数据迁移(复制)
gitblit 数据迁移 完全拷贝方式: 将原服务器上的gitblit的安装目录.数据目录等相关目录拷到另一台服务器上即可,这样启动方式和使用端口及数据和原服务上的一模一样.(因为gitblit是不用 ...