CF1248F Catowice City
problem
有\(n\)个人,每个人家有一只猫。每个人都认识一些猫(其中肯定包括自己家的猫)。选出\(j\)个人和\(k\)只猫\((j,k\ge 1)\)。使得\(j+k=n\)且选出的人和猫都互不认识。
solution
一个显然但是重要的推论是:
每个人家都必须去一个人或者一只猫。
这样我们只需要枚举第一个人家去的是人还是猫,就可以根据他们之间的关系推出其他的人家是人还是猫。当无论第一家选择人还是选择猫,都会导致去\(n\)只猫或者\(n\)个人时无解。
code
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 1000100;
ll read() {
ll x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;c = getchar();
}
while(c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
vector<int>e1[N],e2[N];
int vis[N],cnt;
void dfs(int u) {
vis[u] = 1;cnt++;
for(vector<int>::iterator it = e1[u].begin();it != e1[u].end();++it) {
if(vis[*it]) continue;
dfs(*it);
}
}
void dfs2(int u) {
vis[u] = 1;cnt++;
for(vector<int>::iterator it = e2[u].begin();it != e2[u].end();++it) {
if(vis[*it]) continue;
dfs2(*it);
}
}
int main() {
int T = read();
while(T--) {
int n = read(),m = read();
cnt = 0;
for(int i = 1;i <= n;++i) vis[i] = 0;
for(int i = 1;i <= n;++i) e1[i].clear(),e2[i].clear();
for(int i = 1;i <= m;++i) {
int u = read(),v = read();
e1[u].push_back(v);e2[v].push_back(u);
}
dfs(1);
int bz = 1;
if(cnt == n) {
cnt = 0;
for(int i = 1;i <= n;++i) vis[i] = 0;
dfs2(1);
if(cnt == n) {puts("No");continue;}
cnt = n - cnt;
bz = 0;
}
puts("Yes");
printf("%d %d\n",cnt,n - cnt);
for(int i = 1;i <= n;++i) {
if(vis[i] == bz) printf("%d ",i);
}
puts("");
for(int i = 1;i <= n;++i) {
if(vis[i] != bz) printf("%d ",i);
}
puts("");
}
return 0;
}
CF1248F Catowice City的更多相关文章
- Codeforces Round #594 (Div. 1) D. Catowice City 图论
D. Catowice City In the Catowice city next weekend the cat contest will be held. However, the jury m ...
- 并不对劲的CF1239B&C&D Programming Task in the Train to Catowice City
CF1239B The World Is Just a Programming Task 题目描述 定义一个括号序列s是优秀的,当且仅当它是以下几种情况的一种: 1.|s|=0 2.s='('+t+' ...
- 题解-CF1239D Catowice City
CF1239D Catowice City 有 \(n\) 个人和 \(n\) 只猫.有 \(m\) 对人猫友谊,即第 \(u_i\) 个人认识第 \(v_i\) 只猫,保证第 \(i\) 个人和第 ...
- Codeforces 1239D. Catowice City
传送门 如果选择 $i$ 当陪审团成员,那么 $i$ 认识的猫一定不能参加 又因为总人数和猫数要为 $n$ ,那么 $i$ 认识的猫 的主人也一定要当陪审团成员(不然总数不够) 所以可以考虑这样构图, ...
- Codeforces1248F. Catowice City
题意:1e6个人每人有一只猫 每个人认识自己的猫 现给出一些关系表示一个人认识某只猫 要选出一些人和一些猫 使得每个人都不认识选出来的猫 且人+猫=n 题解:答案肯定是 一个人的集合和一个猫的集合 那 ...
- 一句话CF
目录 \(\bf {Round \ \#500 \ (Div. \ 1)}\) \(\bf {Round \ \#589 \ (Div. \ 2)}\) \(\bf {Avito \ Cool \ C ...
- Codeforces Round #594 (Div. 2)
传送门 C. Ivan the Fool and the Probability Theory 题意: 给出一个\(n*m\)的方格,现在要给方格中的元素黑白染色,要求任一颜色最多有一个颜色相同的格子 ...
- 【CodeForces】CodeForcesRound594 Div1 解题报告
点此进入比赛 \(A\):Ivan the Fool and the Probability Theory(点此看题面) 大致题意: 给一个\(n\times m\)的矩阵\(01\)染色,使得不存在 ...
- Codeforces Round #594 (Div. 1)
Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...
随机推荐
- 松软科技web教程:JavaScript Switch 语句
switch 语句用于基于不同条件执行不同动作. JavaScript Switch 语句 请使用 switch 语句来选择多个需被执行的代码块之一. 语法 switch(表达式) { case n: ...
- python yield关键词使用总结
python yield关键词使用总结 by:授客 QQ:1033553122 测试环境 win10 python 3.5 yield功能简介 简单来说,yield 的作用就是把一个函数变成一个 ge ...
- Complete_NGINX_Cookbook
Complete NGINX Cookbook 下载地址:Complete NGINX Cookbook
- java 坐标系运算 判断一个地理坐标是否在电子围栏 圆、矩形、多边形区域内
转载自:https://blog.csdn.net/Deepak192/article/details/79402694 测试没问题,我用的是原始坐标:要注意的是坐标转换问题,要看当前是属于什么坐标系 ...
- github二级域名配置
首先打开GitHub并登上你的GitHub账号 新建仓库 点击settings 接下来就是操作git往这个仓库存文件,该域名会访问index.html文件
- Java类BufferedImage
https://docs.oracle.com/javase/7/docs/api/java/awt/image/BandedSampleModel.html
- TestNG如何用命令行运行
TestNG如何用命令行运行 调用TestNG最简单的方法事下面的: java org.testng.TestNG testng1.xml 以上是我在网上搜索到的最多的答案,但对于第一次接触testn ...
- SpringBoot打包为war包,并在tomcat中运行
先看一下我创建的最简单的项目吧. controller类 @RestController public class TestController { @RequestMapping("ind ...
- 如何解决eclipse远程服务器上面的Rabbitmq连接超时问题?
1.嗯,问题呢,就是一开始安装好RabbitMQ,练习了一下RabbitMQ的使用,但是呢,过了一段时间,我来复习的时候,发现运行出现下面的错误了.后来想想,是自己学习微服务的时候,修改了/etc/h ...
- 手机分辨率DPI怎么计算
长度方向像素数平方加宽度方向像素平方然后开根号,最后除以屏幕大小(英寸)