CF549BLooksery Party题解
题目描述
The Looksery company, consisting of nn staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself.
Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates nnnumbers, the i -th of which indicates how many messages, in his view, the i -th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins.
You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated.
(P.s感谢@luogu.org_悠悠我昕 提供翻译)
Looksery 公司由 n 名员工所组成,每个员工都有自己的电话和联系人列表。
最近,公司打算举办一场大型派对,每位到场的员工,都会向他的联系人发送消息,来说明这场派对有多炫酷。由于每个人都想花尽量多的时间来享受派对时光,他们会不假思索地向所有联系人发送消息,甚至包括他们自己。
Looksery 公司的开发者 Igor 和 Max 就每个人收到了多少条消息展开了争论. Igor 钦点了 n 个数字,其中第 i 个数字表示 Igor 认为第 i 个人收到的消息条数。如果 Igor 猜到了至少 1 个数字就算他胜利,否则 Max 胜利。
作为 Max 的支持者,你需要根据员工的联系人列表,确定是否存在 Igor 会输的情况。具体来说,你需要确定哪些人应该参加派对,使得派对结束后,任意一个人收到的消息条数与 Igor 所给出的不同。
输入输出格式
输入格式:
The first line contains a single integer nn ( 1<=n<=100 ) — the number of employees of company Looksery.
Next nn lines contain the description of the contact lists of the employees. The i -th of these lines contains a string of length nn , consisting of digits zero and one, specifying the contact list of the i -th employee. If the j -th character of the ii -th string equals 1, then the j -th employee is in the i -th employee's contact list, otherwise he isn't. It is guaranteed that the ii -th character of the ii -th line is always equal tThe last line contains nn space-separated integers: a1,a2,...,an( 0<=ai<=n), where ai represents the number of messages that the i -th employee should get according to Igor.
第一行一个正整数 n(1≤n≤100) ,表示 Looksery 公司的员工数量。
接下来 n 行描述了所有员工的联系人列表,每行为一个长度为 n 的字符串。若第 i 行的第 j 个字符为 1 ,则第 j名员工在第 i 名员工的联系人列表中,否则就不在。特别地,第 i 个人总是自己的联系人列表中。
最后一行 n 个由空格隔开的数字 a1,a2,...,an(0≤ai≤n) ,表示 Igor 所认为的每个员工会收到的消息数量。
输出格式:
In the first line print a single integer m — the number of employees who should come to the party so that Igor loses the dispute.
In the second line print m space-separated integers — the numbers of these employees in an arbitrary order.
If Igor wins the dispute in any case, print -1.
If there are multiple possible solutions, print any of them.
第一行一个整数 m 表示参加派对的人数。
第二行 m 个正整数,表示参加派对的员工的编号。
如果有多种方案满足条件,输出其中任意一种即可。
如果 Igor 无论如何都会赢,输出 -1 。
题解
对于这道题,我们可以先证明一下没有无解的情况:
【反证】假设存在无解情况,则存在当n个人都进入邀请方案时存在一个a[i] = 0,而这与a[i] = 0时才可以进入邀请方案相矛盾所以不存在无解情况。
由此我们可以考虑一下这道题的贪心策略:
如果不存在任何a[i]==0那么,当所有人都不进入邀请方案时一定满足条件。
如果存在某一a[i]!=0那么,因为如果i进入邀请方案,则他会给每一个他认识的人发送消息(包括他自己),这就可以使他收到的信息不为0,而同样他会更新其他他认识的人所不应该收到的短信数量,则会造成有新的a[k]==0只需要用队列记录一下,依次处理即可。
代码
#include <bits/stdc++.h>
using namespace std; const int MAX = ;
vector<int> v[MAX], worker;
queue<int> q;
int a[MAX], vis[MAX];
int main()
{
// freopen("CF549B.in", "r", stdin);
// freopen("CF549B.out", "w", stdout); int n, x;
char s[MAX];
scanf("%d", &n);
for(int i = ; i <= n; ++ i)
{
scanf("%s", s + );
for(int j = ; j <= n; ++ j)
if(s[j] == '') v[i].push_back(j);
}
for(int i = ; i <= n; ++ i)
{
scanf("%d", &a[i]);
if(a[i] == ) q.push(i);
}
for(;!q.empty();)
{
x = q.front(); q.pop();
worker.push_back(x);
for(int i = ; i < v[x].size(); ++ i)
{
int y = v[x][i];
a[y] --;
if(a[y] == ) q.push(y);
}
}
printf("%d\n", worker.size());
for(int i = ; i < worker.size(); ++ i)
printf("%d ", worker[i]);
printf("\n");
return ;
}
总结与反思
对于一些贪心我们通常的考虑是从一般情况入手,但是这道题他却需要我们构造出一组符合条件的特解,然后把大多数的一般情况都划归到这个特解上来,这是我们所需要了解的一种解题思路。
CF549BLooksery Party题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- Linux-Xshell5
Linux-Xshell5 1.下载 2.安装 3.新建会话(连接) 点击新建 需要知道要连接的 IP, 查看命令 ifconfig 配置 名称可以自己命名,主机写要连接的 IP,其他的不能改 输 ...
- linux运维基础知识
linux运维基础知识大全 一,序言 每一个微不足道的知识,也是未来的铺垫.每一份工作的薪资职位,也是曾经努力的结果. 二,服务器 1,运维人员工作职责: 1)保证数据不丢失:2)保证服务器24小时运 ...
- 学习javscript函数笔记(二)
定义: 函数包含一组语句,他们是JavaScript的基础模块单元,用于代码复用.信息隐藏和组合调用.函数用于指定对象的行为. 1.函数对象 JavaScript中的函数就是对象,函数对象连接到Fun ...
- 阿里云Tomcat运行shutdown.sh命令关闭时遇到的问题
1.安装完成jdk之后,然后安装tomcat. tomcat安装成功后,进入tomcat的安装目录,找到bin所在的目录. 使用./startup.sh,启动tomcat; 使用./shutdown. ...
- 01-spring配置详解
1 bean元素 <!--将User对象交给spring容器进行管理 --> <!-- Bean元素:使用该元素描述需要spring容器管理的对象 class属性:被管理对象的完整类 ...
- 【转】Python中不尽如人意的断言Assertion
原文地址:Python中不尽如人意的断言Assertion Python Assert 为何不尽如人意 Python中的断言用起来非常简单,你可以在assert后面跟上任意判断条件,如果断言失败则会抛 ...
- js 中onclick 事件 点击后指向自己的对象,查找或者添加属性 用关键字this 传入参数 (可以改变原标签css)
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- C# Uploadify 文件上传组件的使用
一.页面的构建 1.要引用的JS和CSS <link href="../css/jquery-ui.css" rel="stylesheet" type= ...
- Linux Tomcat 80端口 Port 80 required by Tomcat v8.5 Server at localhost is already in use.
Port 80 required by Tomcat v8.5 Server at localhost is already in use. The server may already be run ...
- tomcat和应用集成
将tomcat作为应用的一部分集成到应用中,使得应用可以直接开启http服务,对外提供接口.此时应用程序不必再遵守j2ee中的文件目录格式要求. 此种方式改变了以往先部署tomcat容器,再按照j2e ...