路线冲突问题

题目描述

给出一张地图,地图上有n个点,任意两点之间有且仅有一条路。点的编号从1到n。

现在兵团A要从s1到e1,兵团B要从s2到e2,问两条路线是否会有交点,若有则输出交点个数,否出输出”success”。

输入

多组输入。

对于每组输入。

第一行输入n(1 <= n <= 100000),代表点的个数。

接下来的n-1行,每行包含两个整数u,v(1 <= u,v <= n),代表u,v之间有一条相连。

再接下里的一行包含四个整数s1,e1,s2,e2(1 <= s1,e1,s2,e2 <= n)。

输出

 问两条路线是否会有交点,若有则输出交点个数,否出输出”success”。

示例输入

3
1 2
2 3
1 2 2 3

示例输出

1

算法分析:两次bfs记录下起点到终点的路径就行了,然后比较两条路径有没有重合点。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <algorithm> using namespace std; vector<int>q[100002];
bool vis[100002];
int fa[100002];
bool path[100002]; void BFS(int s, int e)
{
memset(vis, false, sizeof(vis));
memset(fa, 0, sizeof(fa));
vis[s]=true;
queue<int>p;
int i, j, len;
while(!p.empty())
p.pop();
p.push(s);
int flag=0;
while(!p.empty())
{
int dd=p.front();
p.pop();
len=q[dd].size();
for(i=0; i<len; i++)
{
if( vis[q[dd][i]]==false )
{
fa[ q[dd][i] ] = dd; //记录前驱
//printf("%d--%d\n", dd, fa[q[dd][i]] );
if(q[dd][i]==e) //找到终点跳出
{
flag=1; break;
}
p.push( q[dd][i] );
vis[ q[dd][i] ] =true;
}
}
if(flag==1)
break;
}
} int main()
{
int n;
int i, j;
int u, v;
int a, b, x, y; while(scanf("%d", &n)!=EOF)
{
for(i=0; i<=n; i++)
q[i].clear();
for(i=0; i<n-1; i++)
{
scanf("%d %d", &u, &v);
q[u].push_back(v);
q[v].push_back(u);
}
scanf("%d %d %d %d", &a, &b, &x, &y);
BFS(a, b);//fa数组弹出
memset(path, false, sizeof(path));
for(i=fa[b]; i!=0; i=fa[i] )
{
path[i]=true;
}
path[b]=true; BFS(x, y);
int cnt=0;
for(j=fa[y]; j!=0; j=fa[j] )
{
if(path[j]==true)
cnt++;
}
if(path[y]==true)
cnt++; if(cnt==0)
printf("success\n");
else
printf("%d\n", cnt);
}
return 0;
}

sdut oj 3058 路线冲突问题(BFS+记录路径算法,回溯路径 )的更多相关文章

  1. sdut oj 操作系统实验--SSTF磁盘调度算法【操作系统算法】

    操作系统实验--SSTF磁盘调度算法 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 磁盘调度在多道程序设计的计算机系统中,各个进 ...

  2. SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )

    图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...

  3. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...

  4. SDUT OJ 2607

    /*http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607*/ 题目大意:给出一个字符串,求出里 ...

  5. Codeforces-A. Shortest path of the king(简单bfs记录路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  6. SDUT OJ 1221 亲和数 (找出某个数n所有的因子数,只需要暴力:2->sqrt(n) 即可 )

    亲和数 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 如果a的因子和等于b,b的因子和等于a,且a≠b,则称a,b为亲和数对. ...

  7. HDU1026--Ignatius and the Princess I(BFS记录路径)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

  8. POJ 3414--Pots(BFS+回溯路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 4179   Special Ju ...

  9. SVN版本冲突中 Files 的值“ < < < < < < < .mine”无效路径中具有非法字符的解决办法

    .NET 中 SVN版本冲突中 Files 的值“ < < < < < < < .mine”无效路径中具有非法字符的解决办法: 一. 1.将项目逐个进行编译, ...

随机推荐

  1. 51nod 1088 最长回文子串 【中心拓展法/输出长度和路径】

    1088 最长回文子串 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字 ...

  2. Symmetric Tree(DFS,二叉树的构建以及测试代码)

    基础有待加强啊,由该题引发出来一些问题,现在来总结下. 首先是二叉树的结构: struct TreeNode { EleType val; TreeNode *left; TreeNode *righ ...

  3. android TextView 设置字体大小

    package com.example.yanlei.yl4; import android.graphics.Color;import android.os.Bundle;import androi ...

  4. Please configure Spring facet or use 'Create Default Context' to add one including all unmapped files.

    有时候我们刚进入 Intellij IDEA时会出现这样一个情况,原因是IDEA没有找到spring的配置文件,我们需要添加spring文件给idea管理 参考: 1.https://www.jetb ...

  5. MOS简单应用

    高端功率开关驱动的原理非常简单,和低端功率开关驱动相对应,就是负载一端和开关管相连,另外一端直接接地.正常情况下,没有控制信号的时候,开关管不导通,负载中没有电流流过,即负载处于断电状态:反之,如果控 ...

  6. Terminal emulator

    http://en.wikipedia.org/wiki/Terminal_emulator Terminal emulator From Wikipedia, the free encycloped ...

  7. 内核顶层Makefile相关2

    http://www.groad.net/bbs/simple/?f104.html if  函数 if 函数的语法有两种形式: () $(if <condition>, <then ...

  8. 【git】强制覆盖本地代码

    [git]强制覆盖本地代码(与git远程仓库保持一致) 2018年04月27日 23:53:57 不才b_d 阅读数:21145   版权声明:本文为博主不才b_d原创文章,未经允许不得转载. || ...

  9. Cocos2d-X中提高性能的方法

     1)内存使用效率: 使用大纹理 场景切换时,要尽量使用replaceScene 2)用好缓存: CCTextureCache(纹理缓存) CCSpriteFrameCache(精灵帧缓存) CC ...

  10. 不使用while,for,if等实现加法

    不使用if, while,for,switch等实现从1到10的加法 解:这里使用静态函数和静态变量实现,利用类似的方法也能够实现从1打印到1000 class TheSum{ public: The ...