题解链接:

https://www.lucien.ink/archives/161/


题目链接:

http://exam.upc.edu.cn/problem.php?id=5094


题目:

题目描述

As part of a CS course, Alice just finished programming her robot to explore a graph having n nodes, labeled 1, 2, … , n, and m directed edges. Initially the robot starts at node 1.
While nodes may have several outgoing edges, Alice programmed the robot so that any node may have a forced move to a specific one of its neighbors. For example, it may be that node 5 has outgoing edges to neighbors 1, 4, and 6 but that Alice programs the robot so that if it leaves 5 it must go to neighbor 4.
If operating correctly, the robot will always follow forced moves away from a node, and if reaching a node that does not have a forced move, the robot stops. Unfortunately, the robot is a bit buggy, and it might violate those rules and move to a randomly chosen neighbor of a node (whether or not there had been a designated forced move from that node). However, such a bug will occur at most once (and might never happen).
Alice is having trouble debugging the robot, and would like your help to determine what are the possible nodes where the robot could stop and not move again.
We consider two sample graphs, as given in Figures G.1 and G.2. In these figures, a red arrow indicate an edge corresponding to a forced move, while black arrows indicate edges to other neighbors. The circle around a node is red if it is a possible stopping node.

In the first example, the robot will cycle forever through nodes 1, 5, and 4 if it does not make a buggy move.
A bug could cause it to jump from 1 to 2, but that would be the only buggy move, and so it would never move on from there. It might also jump from 5 to 6 and then have a forced move to end at 7.
In the second example, there are no forced moves, so the robot would stay at 1 without any buggy moves. It might also make a buggy move from 1 to either 2 or 3, after which it would stop.

输入

The first line contains two integers n and m, designating the number of nodes and number of edges such that 1 ≤ n ≤ 103, 0 ≤ m ≤ 104. The next m lines will each have two integers a and b, 1 ≤ |a|, b ≤ n and |a| ≠ b. If a > 0, there is a directed edge between nodes a and b that is not forced. If a < 0, then there is a forced directed edge from −a to b. There will be at most 900 such forced moves. No two directed edges will be the same. No two starting nodes for forced moves will be the same.

输出

Display the number of nodes at which the robot might come to a rest.

样例输入

7 9
1 2
2 3
-1 5
2 6
5 1
-4 1
5 6
-6 7
-5 4

样例输出

2

题意:

  给你一个n个点m条边的有向图,初始时机器人在1这个顶点,默认情况下机器人只能沿着红色的边走。但是这个机器人可能会出BUG,至多出BUG一次,出BUG时机器人会从当前点随机地沿着有向图的边走到一个相邻的点,忽略这条边的颜色。问这个机器人可能最终停留的点的数目。


思路:

  从节点1开始按照正常路径走,枚举在每个点发生BUG时所有可能的最终归属,加到set里,最后输出set.size()即可。需要注意的是有可能在1这个点就出BUG,所以要先check(1)一下,因为这个WA了一次。


实现:

#include <bits/stdc++.h>
const int maxn = int(1e4) + 7, maxm = int(1e5) + 7;
bool red[maxn], vis[maxn], checked[maxn];
struct {int next, to;bool col; // true: red
} edge[maxm];
int n, m, head_edge[maxn], cnt_edge;void addedge(int u, int v, bool col) {edge[cnt_edge] = {head_edge[u], v, col};head_edge[u] = cnt_edge++;
}std::set<int> ans;void check(int u) { // check以u这个点为起点时不出BUG机器人最终会停在哪里checked[u] = true;if (!red[u] || head_edge[u] == -1) {ans.insert(u);return ;}for (int i = head_edge[u]; ~i; i = edge[i].next) {int v = edge[i].to;if (edge[i].col && !checked[v]) check(v);}
}void dfs(int u) {vis[u] = true;for (int i = head_edge[u]; ~i; i = edge[i].next) {int v = edge[i].to;if (!edge[i].col && !checked[v]) check(v);if (edge[i].col && !vis[v]) dfs(v);}
}int main() {
//    freopen("in.txt", "r", stdin);memset(head_edge, 0xff, sizeof(head_edge));scanf("%d%d", &n, &m);for (int i = 0, u, v; i < m; i++) {scanf("%d%d", &u, &v);int tmp = abs(u);if (u < 0) red[tmp] = true;addedge(tmp, v, u < 0);}check(1);dfs(1);printf("%d\n", (int)ans.size());return 0;
}

UPC-5094 - Faulty Robot - 搜索相关推荐

  1. upc 8377: Playoff(搜索-dfs)

    8377: Playoff 题目描述: The Minato Mirai Football Association hosts its annual championship as a single ...

  2. JAVA工具篇--java.awt.Robot模拟微信批量添加好友

    前言:java.awt.Robot可以控制鼠标和键盘,本文基于此通过模拟认为添加微信好友的过程实现批量添加微信好友,并最终输出微信号/手机号是否有好友及好友的基本信息,本文代码示例禁用学习交流使用: ...

  3. 漏洞介绍及修复建议(漏洞汇总,建议收藏后期会不断更新)

    目录 未分类 Host 头攻击(高危) 域名访问限制不严格(高危) URL 重定向(中危) 会话劫持漏洞(中危) 会话固定漏洞(中危) DNS 域传送漏洞(中危) 检测到网站被黑痕迹(高危) 传输层保 ...

  4. 2019河北省大学生程序设计竞赛(部分题解)

    2019河北省大学生程序设计竞赛 B.Icebound and Sequence B题题解 G.点我 签到题 H.天神的密码 签到题 #include <iostream> #includ ...

  5. 基于个性化信息服务技术的研究

    基于个性化信息服务技术的研究 黄 堃,林旭云 (福建金融职业技术学院,福建 福州 350007) [摘 要]传统信息检索技术满足了人们一定的需要,但由于其通用的性质,仍然不能满足不同背景.不同目的和不 ...

  6. 2020-08-27 RPA uipath入门demo,超级鹰实现验证码自动登录,自动搜索和拉取数据到excel,robot和Orchestrator共同实现定时安排机器人执行任务。

    资源地址:https://download.csdn.net/download/u012742970/12773335 本文主要讲解uipath studio,uirobot,Orchestrator ...

  7. upc组队赛5 Election of Evil【搜索】

    Election of Evil 题目描述 Dylan is a corrupt politician trying to steal an election. He has already used ...

  8. 【DFS反向建图记忆化搜索】UPC Contest2592 - 2020年秋季组队训练赛第十四场 问题 D: Mysterious Treasure

    问题 D: Mysterious Treasure 时间限制: 1 Sec 内存限制: 128 MB 题目描述 WNJXYK and DIDIDI is playing a game. DIDIDI ...

  9. 【upc】2020年秋季组队训练赛第十四场 Get Strong | 折半搜索、二分

    题目描述 WNJXYK and DIDIDI are friends. They are thinking about getting strong all the time. They are pl ...

  10. upc 8377 Playoff(搜索)

    8377: Playoff 时间限制: 2 Sec  内存限制: 128 MB 题目描述 The Minato Mirai Football Association hosts its annual ...

最新文章

  1. 【Android 性能优化】应用启动优化 ( 方法追踪代码模板 | 示例项目 | SD 卡访问权限 | 示例代码 | 获取 Trace 文件 | Android Studio 查看文件)
  2. 如何看待蒂姆·库克在苹果的地位
  3. windows版本下的 redis 集群配置
  4. Android Serializable与Parcelable原理与区别
  5. DeepMind给人工智能搞了一套IQ测试题
  6. 01.学习笔记-linux操作系统常用命令
  7. 现实竞争激烈,学透才能在职场做个风云人物,单片机存储器篇
  8. [Python3] 023 面向对象 第三弹
  9. 快速生成大量测试数据
  10. Windows下 LaTeX安装教程 TeX Live 2021版
  11. 笔记本AutoCAD启动时闪退怎么办_autocad2010打开闪退怎么办?autocad2010闪退解决方法...
  12. 《C语言》2022山西专升本C语言知识点
  13. 共享内存,信号,信号灯集
  14. JAVA文章相似度对比(hanlp)
  15. 新西兰计算机科学硕士哪所大学最好,2020年新西兰哪些大学计算机科学专业比较好及其优势介绍...
  16. DX11 游戏开发笔记 (一) 资源介绍
  17. Centos指令mysql安装报错为“ Failing package is: mysql-community-client-5.7.38-1.el7.x86_64”
  18. java if经典程序_java经典程序题15道(另附自己做的答案)
  19. 零粉丝直播带货,日赚两万是真是假?最新抖音直播玩法!
  20. 达梦数据库删除用户_干货分享丨DM8用户管理

热门文章

  1. 几款入夏品牌包包可以看看
  2. 关乎未来40年企业生存,这些食品饮料巨头都在干这件事儿! | 商研局 Cool Business...
  3. 妖精为什么吃不到唐僧肉
  4. 结合MVC.NET相关知识实现在线卖酒销售项目(一)
  5. Cell综述:代谢控制中的脑肠轴
  6. Android仿淘宝物流时间轴
  7. R语言使用rbind函数将两个dataframe数据纵向合并起来(vertically)
  8. Mysql数据库版本高低引起的group by问题
  9. linux 改变输出端口,linux – 更改ssh端口后的Fail2ban设置
  10. 细说匿名内部类引用方法局部变量时为什么需要声明为final