直接DFS会导致超时TLE

http://www.cnblogs.com/staginner/archive/2011/09/07/2170348.html

中写到,如果首先求出与着火点相连的顶点再DFS可以AC,是由于测试数据恰好解决了这个问题

即存在起点与终点不连通的数据,与起点相连刚好又是稠密图,所以导致超时

一般DFS写法

//TLE
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std;
///
#define INF 0xffffff7
#define maxn 30
///
int streetcorners;
int G[maxn][maxn];
int path[maxn];
bool vis[maxn];
int cnt;
int nodes;
void dfs(int cur, int tot)
{
int i, j;
if (cur == streetcorners)
{
cnt++;
bool first = true;
for (i = 0; path[i] != 0; i++)
{
if (first)
{
printf("%d", path[i]);
first = false;
}
else
printf(" %d", path[i]);
}
printf("\n");
return;
}
else
{
for (i = 0; i <= nodes; i++)
{
if (G[cur][i] && !vis[i])
{
path[tot] = i;
vis[i] = true;
dfs(i, tot + 1);
path[tot] = 0;
vis[i] = false;
}
}
}
}
int main()
{
freopen("F:\\input.txt","r",stdin );
///
int i, j;
int temp1, temp2;
int cases = 1;
while (scanf("%d", &streetcorners) != EOF)
{
memset(path, 0, sizeof(path));
memset(vis, false, sizeof(vis));
memset(G, 0, sizeof(G));
cnt = 0;
//读入交通图
while(1)
{
cin >> temp1 >> temp2;
if (temp1 == 0 || temp2 == 0)
break;
if (temp1 > nodes)
nodes = temp1;
if (temp2 > nodes)
nodes = temp2;
G[temp1][temp2] = G[temp2][temp1] = 1;
}
printf("CASE %d:\n", cases);
path[0] = 1;
vis[1] = true;
dfs(1, 1);
printf("There are %d routes from the firestation to streetcorner %d.\n", cnt, streetcorners);
cases++;
}
///
return 0;
}

先求出对于起点和着火点的双连通分量,然后DFS中对双连通分量进行搜索

// Program 2
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int G[25][25],vis[25],path[25],p;
int A[25],n,fire,conect;
int dfn[25],low[25],time,s[25],top;
int cmp(const void *_p,const void *_q)
{
int *p=(int *)_p;
int *q=(int *)_q;
return *p-*q;
}
//解法核心,通过构造22结点与1与着火点相连,用tarjan算法求是否存在双连通分量,存入栈中
void tarjan(int fa,int u)
{
int v;
dfn[u]=++time;
low[u]=dfn[u];
for(v=0;v<25;v++)
if(G[u][v]&&v!=fa)
{
if(!dfn[v])
{
s[top++]=v;
tarjan(u,v);
if(low[v]<low[u])
low[u]=low[v];
else if(low[v]>dfn[u])
for(s[top]=-1;s[top]!=v;)
top--;
}
else if(dfn[v]<low[u])
low[u]=dfn[v];
}
}
void dfs(int cur,int now)
{
int i;
if(now==fire)
{
n++;
for(i=0;i<cur;i++)
{
if(i)
printf(" ");
printf("%d",A[i]);
}
printf("\n");
return;
}
for(i=0;i<p;i++)
if(G[now][path[i]]&&!vis[path[i]])
{
vis[path[i]]=1;
A[cur]=path[i];
dfs(cur+1,path[i]);
vis[path[i]]=0;
}
}
int main()
{
int i,j,k,u,v,t;
t=0;
while(scanf("%d",&fire)==1)
{
printf("CASE %d:\n",++t);
memset(G,0,sizeof(G));
while(1)
{
scanf("%d%d",&u,&v);
if(u==0)
break;
G[u][v]=G[v][u]=1;
}
top=time=0;
memset(dfn,0,sizeof(dfn));
G[1][22]=G[22][1]=G[fire][22]=G[22][fire]=1;
s[top++]=1;
tarjan(-1,1);
G[1][22]=G[22][1]=G[fire][22]=G[22][fire]=0;
conect=p=0;
while(top>0)
{
top--;
if(s[top]==fire)
conect=1;
path[p++]=s[top];
}
if(!conect)
{
printf("There are 0 routes from the firestation to streetcorner %d.\n",fire);
continue;
}
qsort(path,p,sizeof(path[0]),cmp);
p--;
memset(vis,0,sizeof(vis));
vis[1]=1;
A[0]=1;
n=0;
dfs(1,1);
printf("There are %d routes from the firestation to streetcorner %d.\n",n,fire);
}
return 0;
}

UVA 208 救火车相关推荐

  1. UVA - 208 Firetruck

    UVA - 208 Firetruck 题目大意 构造出一张图,给出一个点,让你按字典序输出所有从1到该点的路径 一开始直接DFS超时了 后面看到大佬的优化,大意就是很多起点与终点不相连,需要一开始剪 ...

  2. Firetruck UVA - 208

    DFS+并查集 如果只用DFS的话会超时,用并查集剪枝,和起点终点不联通的点就不用跑了 这题有好多人写了博客,但是我觉得我的代码写的比较通俗易懂所以就贴上来了,我觉得我写代码的目标就是让任何人都能看懂 ...

  3. UVa 208 - Firetruck (回溯)

    题意 给出一个无向图和终点的编号 按字典序枚举出从1到终点的路径 思路 要事先判断结点1是否可以到达结点k, 用一个bool judge()函数判断一下从终点能否回到1点即可. 如果无解直接输出有0种 ...

  4. UVa 208 Firetruck【回溯】

    题意:给出一个n个节点的无向图,以及某个节点k,按照字典序从小到大输出从节点1到节点k的所有路径 看的题解 http://blog.csdn.net/hcbbt/article/details/975 ...

  5. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  6. UVA 208 划水记录2

    题目:题目描述 中心城市消防部门与运输部门合作,维护反映城市街道现状的城市地图.消防员需要能够选择从火警站到火警的路线. 中心城市分为不重叠的消防区.当报告发生火灾时,中央调度员通知火灾发生地区最近的 ...

  7. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  8. linux睿频是自动的吗,Linux限制cpu睿频限制频率

    as3.0 [Embed]标签嵌入外部资源 1.[Embed]嵌入资源 ActionScript代码的顺序非常重要.你必须在声明变量前添加[Embed]元数据标签,而且这个变量的类型会是Class; ...

  9. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

最新文章

  1. 获取汉字的首字母(转)
  2. boost::r_c_shortest_paths用法的测试程序
  3. 微信小程序最新开发资源汇总,对学习微信小程序的新手有一定帮助
  4. leetcode326. 3的幂 如此6的操作你想到了吗
  5. 渗透测试工具——Metasploit
  6. AI头发笔刷_这么棒的AI插件,一定要偷偷藏好了不让总监知道……
  7. 厦门大学国家奖学金答辩PPT模板
  8. 手把手教你部署Docker(手撸官网)
  9. 练习:身高出现的频次
  10. springboot+FreeMarker制作word模板
  11. rockchip rk3566 android11 网口log报错: DMA engine initialization failed
  12. 光华股份深交所上市:市值51亿 应收账款余额超5亿
  13. 页面js打开 qq客户端对话框(已测试)
  14. OSChina 周三乱弹 —— 同志,你这个书签掉毛严重啊
  15. linux卸载informatica,【Informatica从零开始】第一弹之Informatica在linux下安装搭建
  16. 按开机计算机屏幕数字是,电脑开机后屏幕全都是英文字母及数字跳动,什么处理?...
  17. 智能视频分析监控技术
  18. POCO库学习教程(一) windows POCO库编译(64位)
  19. 小米手机浏览器的input、textarea底被遮挡
  20. 我的黑群晖学习之旅(1)系统安装篇

热门文章

  1. android7 截屏保存,手机屏幕截图无法保存该怎么解决?
  2. java 修改mac地址_Win10系统修改通过注册表修改网卡mac地址
  3. vue开发中<div>内<el-button>无法触发点击事件
  4. Alexa排名猛升21,956位(www.mobi123.cn,手机网址之家)
  5. 6000 多款 App,看我如何搞定她们并将其洗白白~
  6. 1、hadoop安装及其文件结构
  7. Android之手机通知栏Notification的使用(二)
  8. 上海python什么时候考试_上海2016年计算机二级考试Python考试大纲修订
  9. 淘宝网店一变相日期计算器
  10. yum工具卸载和重新安装