题目描述

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题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. IDEA查看第三方jar包的源代码时出现Decompiled.class file, bytecode version:52.0 (Java 8)的解决方案

    IDEA中使用Ctrl+左键查看第三方jar包的源代码时,出现Decompiled.class file, bytecode version:52.0 (Java 8),说明IDEA没找到该类的.ja ...

  2. 关于表格合并span-method方法的补充(表格数据由后台动态返回)

    之前写了一些关于element-ui表格合并的方法,不过用的数据都是确定的数据(死数据),但是很多时候我们的数据都是通过后台获得的,数据不稳定,这个时候使用表格合并就需要先处理一下数据,先看一下一种很 ...

  3. mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)

    1.使用相同索引键值的冲突 由于mysql 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但如果是使用相同的索引键,是会出现锁冲突的.设计时要注意 例如:city表city_ ...

  4. leetcode 862 shorest subarray with sum at least K

    https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/ 首先回顾一下求max子数组的值的方法是:记录一个前缀min值, ...

  5. 请以excel管理你的接口测试用例

    闲话休扯,上需求:自动读取.执行excel里面的接口测试用例,测试完成后,返回错误结果并发送邮件通知. 分析: 1.设计excel表格2.读取excel表格3.拼接url,发送请求4.汇总错误结果.发 ...

  6. pat09-散列1. Hashing (25)

    09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...

  7. Oracle 数据类型详解

    数据类型(datatype)是列(column)或存储过程中的一个属性. Oracle支持的数据类型可以分为三个基本种类:字符数据类型.数字数据类型以及表示其它数据的数据类型. 字符数据类型 CHAR ...

  8. bzoj 5308: [Zjoi2018]胖

    Description Cedyks是九条可怜的好朋友(可能这场比赛公开以后就不是了),也是这题的主人公. Cedyks是一个富有的男孩子.他住在著名的ThePLace(宫殿)中. Cedyks是一个 ...

  9. 【VirtualBox】快照

    一.快照备份 虚拟机系统快照下来,以后就可以恢复到快照之前的系统 右上角->虚拟电脑工具->快照

  10. 深入理解JavaScript系列(30):设计模式之外观模式

    介绍 外观模式(Facade)为子系统中的一组接口提供了一个一致的界面,此模块定义了一个高层接口,这个接口值得这一子系统更加容易使用. 正文 外观模式不仅简化类中的接口,而且对接口与调用者也进行了解耦 ...