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 & ...
随机推荐
- html自定义垂直导航菜单(多级导航菜单,去掉font-awesome图标,添加自己的箭头图标)
这次在原先html自定义垂直导航菜单的基础上做了比较大的改动: 1.去掉了font-awesome图标,上级菜单右边的箭头是自己用css写的,具体参考<css三角箭头>. 2.去掉了初始化 ...
- Oracle 的加减法函数
原文:https://blog.csdn.net/chenghaibing2008/article/details/37873117 加法 select sysdate,add_months(sy ...
- VS下如何建立一个新的MFC程序 网络编程 课设 基于C++ MFC 连接数据库 小应用 小项目浅析展示
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/8191036.html 这里不知道会不会有人是真的新手 新新手 不知道怎么 如何建立一个MFC ...
- 《Python编程从入门到实践》_第八章_函数
一个简单的函数 先看一个简单的函数 def say_hello(): '''打印hello''' print("Hello!") say_hello() #运行结果 Hello! ...
- IIS发布常见错误-HTTP 错误 404.0 - Not-Found
错误信息:HTTP 错误 404.0 - Not-Found 错误代码:0x80070002 原 因:IIS配置错误. 解决方法:我配置IIS时漏掉了下面几项,一定要记得勾选.
- Mysql只Union用法
MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 举例说明: select * from table1 u ...
- shutil模块——高级的文件、文件夹、压缩包处理模块
将文件内容拷贝到另一个文件 shutil.copyfileobj('fsrc', 'fdst', 'length') 方法源码: def copyfileobj(fsrc, fdst, length= ...
- scss-@for 指令
此指令用于循环输出,具有两种循环方式,下面分别做一下介绍. (1).@for $var from <start> through <end>: 此种方式的遍历索引区间是[sta ...
- jQuery Ajax(异步改同步)
在实际使用中,我们经常会用的Ajax(异步加载,在不刷新整个网页的前提下对网页部分内容进行更新) 使用时,偶尔会遇上需要从一个接口中得到一个数组和数据对应的id,在另一个接口上再得到数据,最初写法如下 ...
- CSS实现文本周围插入符号
CSS实现文本周围插入符号的方案 本文要讨论的是如何在文本的周围插入图标,怎么样控制它们之间的位置关系,通过HTML结构合理性与CSS属性的使用来比较不同方案所实现效果的优缺点. 常见设计稿要求 在文 ...