Faulty Robot

题目描述

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个点,从1到n中有m条有向连接线段,从1作为初始点出发,默认情况下只能沿着红色的有
向线段走。可以在行走的任意一点发生BUG,即可以在任意一点沿着黑线走一步,在行走的路径中,最多
出现一次bug。因为是有向连接线段,最终的状态只能是停止在某一点,或者在卡在一个循环体。问最终
可以停留的点的数目。
解题思路:因为在行走过程中的每个点都有可能发生bug,所以先bfs枚举出无bug 时的所能够到达的点,
这些点就是可以出现bug 的点。然后对这些点第一次走黑线,再dfs。最终查看叶子结点的种类。(循环体
不要加进去)
给出c++代码:
#include<stdio.h>
#include<string.h>
#include<vector>   //采用动态数组储存
#include<queue>
#include<algorithm>
using namespace std;
int vis[1010];                    // 对走过的所有点做标记 
int flag[1010];                   //dfs时的标记数组
int endd[1010];                 //记录叶子结点的种类
vector<int> red[1010];          //储存红色有向线段
vector<int> black[1010];        //储存黑色有向线段
int bac[1010]={0};             //来记录没有bug时,沿红边所能够到达的点,即可以发生bug 的点
int t,countt;
int n,m;
void dfs(int indx)
{int i;if(vis[indx]==1||flag[indx]==1)return ;if(red[indx].size()==0){//countt++;             //可能通过不同的路径到达同一个点,不能累加,只能记录种类数endd[indx]=1;}flag[indx]=1;for(i=0;i<red[indx].size();i++){dfs(red[indx][i]);flag[indx]=1;}}
void bfs()
{int now,i;queue<int> q;memset(vis,0,sizeof(vis));q.push(1);t=0;bac[t++]=1;  vis[1]=1;     while(!q.empty()){now=q.front();q.pop();if(red[now].size()==0){countt++;vis[now]=1;continue;}for(i=0;i<red[now].size();i++){if(vis[red[now][i]]==0){vis[red[now][i]]=1;bac[t++]=red[now][i];q.push(red[now][i]);}}}}
int main()
{   int i,j,u,v;memset(flag,0,sizeof(flag));memset(endd,0,sizeof(endd));scanf("%d %d",&n,&m);countt=0;t=0;for(i=0;i<m;i++){scanf("%d %d",&u,&v);if(u<0)red[-u].push_back(v);elseblack[u].push_back(v);}bfs();for(i=0;i<t;i++){if(black[bac[i]].size()==0)continue;for(j=0;j<black[bac[i]].size();j++){//if(vis[black[bac[i]][j]]==0)dfs(black[bac[i]][j]);}}for(i=1;i<=n;i++){if(endd[i]==1)countt++;}printf("%d\n",countt);return 0;
}

解题心得:额.........这次没什么心得,要走的路还很远。。。

Faulty Robot相关推荐

  1. Robot Framework(十八) 支持工具

    5支持工具 5.1库文档工具(libdoc) libdoc是一种用于为HTML和XML格式的测试库和资源文件生成关键字文档的工具.前一种格式适用于人类,后者适用于RIDE和其他工具.Libdoc也没有 ...

  2. Java Robot对象实现服务器屏幕远程监视

    Java Robot对象实现服务器屏幕远程监视2006-01-16 17:33 作者: xiepan110 出处: BLOG 责任编辑:方舟 摘要: 有时候,在Java应用程序开发中,如:远程监控或远 ...

  3. java robot类自动截屏

    直接上代码: package robot; import java.awt.Rectangle; import java.awt.Robot; import java.awt.event.InputE ...

  4. robot framework安装问题排查

    现象:小朋友电脑启动ride总会报错 探讨方向:重新安装ride 对应包无法解决 解决思路: 1.完全卸载python及对应库 2.安装RFS 64对应文件,发现安装各种库都报错,最后得到提示pip版 ...

  5. java robot 控制鼠标_Java-探索Robot类:[2]自动控制鼠标

    Robot系列的上一篇经验我介绍了一下Robot类的基本情况与使用,以及运用Robot类对屏幕进行截图操作.这一篇经验我将介绍如果用Robot类模拟鼠标操作. 1.Robot类的4个基本鼠标操作函数 ...

  6. LeetCode 489. Robot Room Cleaner--C++,Python解法

    题目地址:Robot Room Cleaner - LeetCode Given a robot cleaner in a room modeled as a grid. Each cell in t ...

  7. LeetCode 657 : Robot Return to Origin

    题目地址:Robot Return to Origin - LeetCode Acceptance:70.8% Difficulty:Easy There is a robot starting at ...

  8. pip install robot framework出现字符集编码错误

    在cmd下:pin install robot framework 报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in po ...

  9. Mac环境下用Java(Sikuli+Robot)实现页游自动化

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ Sikulix(以前叫Sikuli)在Mac电脑的环境配置步骤如下: 1.从官网上下载Sikuli ...

  10. POJ -1573 Robot Motion

    题目链接:POJ 1573 Robot Motion 一个小模拟,非常easy,依照提示一步步走就是了 #include <iostream> #include <cstdio> ...

最新文章

  1. 解决vue打包后静态资源路径错误的问题
  2. 通过栈(Stack)实现对树的遍历
  3. 第三节:深度剖析各类数据结构(Array、List、Queue、Stack)及线程安全问题和yeild关键字
  4. python subprocess阻塞
  5. lisp 读取样条曲线座标点_MATLAB插值绘制曲线
  6. 【LeetCode】盛最多水的容器【双指针+贪心 寻找最大面积】
  7. Ubuntu18.04 从头开始编译 Android Native WebRTC
  8. dos命令集--江南技术联盟
  9. 网易邮箱服务器怎么注册,免费网易域名邮箱申请教程
  10. 教你用illustrator画十二色环
  11. 远程访问及控制工具SSH
  12. vue ie浏览器兼容
  13. Java项目:基于Jsp实现网上订餐系统
  14. php网页转html网页怎么弄
  15. photoshop图像合成教程之将人物和风景合成一张图片
  16. 并不是越好的院校,考研专业课越难。
  17. Android中对menu、home、back键的监听
  18. Java根据奖品权重计算中奖概率实现抽奖(适用于砸金蛋、大转盘等抽奖活动)
  19. 【博主推荐】html好看的音乐播放器(附源码)
  20. 从码力到算力的“狂飙”,探元宇宙的未来风向

热门文章

  1. HttpMessageNotReadableException: Required request body is missing
  2. Element is missing end tag
  3. 串行通信又称为点对点通信(PtP),其中点对点如何理解
  4. 【随手写】JS过滤所有script正则
  5. c语言格式字符二进制,C语言printf如何输出二进制数格式?将十进制数转为二进制输出...
  6. python小白进阶之路三——七段数码管的绘制+做一个酷炫的倒计时(函数的复用)
  7. 计算机前置usb应用,usb前面不能用,详细教您解决电脑前置USB接口不能使用
  8. 13.2 RS编码和纠错算法
  9. html小吃制作,最简单易学的12种小吃的做法|小吃|简单易学
  10. MATLAB深度学习代码详细分析(一)__nnff.m