Faulty Robot(DFS+BFS)

题目描述

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

题意:机器人会按照红线来走,但是会出一次bug,出bug这次按照黑线来走,每次运行机器人,只会出现一次bug,出现bug的位置不确定,也可能不出现,最终机器人会停止在某些点,也可能无限循环,问你alice设计的地图中有几个这样的点。
思路:首先bfs,考虑不会出bug,看看机器人可能会停在哪些点和能到达的那些点,停止的点标记一下,res++,会到达的点用vis标记一下,然后对于bfs能到达点,进行dfs,首次dfs要用黑线的地图(出bug只会出现一次),之后的dfs用红线的地图。
 
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
vector<int> maze1[1100];
vector<int> maze2[1100];
int vis[1100];
int vist[1100];
int ans[1100];
int bck[1100];/*bfs能到达的点*/
int cnt=0;
int res=0;
int bfs()
{
    queue<int> q;
    vis[1]=1;
    bck[cnt++]=1;
    q.push(1);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();//放后面会超时
        if(maze1[x].size()==0)
        {
            res++;//机器人停在红线链接的地图上(即这个点不指向其他点 可能会存在点但是此点与其他点的连线为黑线)
            vis[x]=1;
            continue;
        }
        for(int i=0;i<maze1[x].size();i++)// 二维vector 表示 x点指向多少个点
        {
            if(vis[maze1[x][i]])
                continue;
            vis[maze1[x][i]]=1;
            q.push(maze1[x][i]);
            bck[cnt++]=maze1[x][i];

        }
        
    }

}
void dfs(int x)
{
    if(vist[x] || vis[x])
        return ;
    if(maze1[x].size()==0)//表示经过一次bug后又一次经过红线链接的点
    {
        ans[x]=1;//标记停止的点
        return;
    }
    vist[x]=1;
    for(int i=0;i<maze1[x].size();i++)
    {

        dfs(maze1[x][i]);
        vist[x]=0;
    }

}
int main()
{
    int x,y;
    int n,m;
    scanf("%d%d",&n,&m);
    ms(vis,0);
    ms(vist,0);
    ms(ans,0);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&x,&y);
        if(x<0)
            maze1[-x].push_back(y);//红线地图
        else
            maze2[x].push_back(y);//黑线地图

    }
    bfs();
    for(int i=0;i<cnt;i++)
    {
        for(int j=0;j<maze2[bck[i]].size();j++)
        {
            dfs(maze2[bck[i]][j]);//首次dfs按照黑线地图(因为只能有一个bug)传入的是与红点通过黑点相连的另一个点 (也可能没有点) 
        }
    }
    for(int i=1;i<=n;i++)
        if(ans[i])
            res++;
    printf("%d\n",res);
    return 0;
}


 
 

猜你喜欢

转载自blog.csdn.net/qq_41021816/article/details/80062173