nyoj题目20吝啬的国度【深搜】

吝啬的国度

时间限制:1000 ms  |  内存限制:65535 KB

难度:3

描述
在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来。现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设你不走重复的路)。

输入
第一行输入一个整数M表示测试数据共有M(1<=M<=5)组
每组测试数据的第一行输入一个正整数N(1<=N<=100000)和一个正整数S(1<=S<=100000),N表示城市的总个数,S表示参观者所在城市的编号
随后的N-1行,每行有两个正整数a,b(1<=a,b<=N),表示第a号城市和第b号城市之间有一条路连通。
输出
每组测试数据输N个正整数,其中,第i个数表示从S走到i号城市,必须要经过的上一个城市的编号。(其中i=S时,请输出-1)
样例输入
1
10 1
1 9
1 8
8 10
10 3
8 6
1 2
10 4
9 5
3 7
样例输出
-1 1 10 10 9 8 3 1 1 8

//vector< vector<int> >map代替邻接矩阵
#include<stdio.h>
#include<iostream>
#include<vector>
using namespace std;
int mark[100001],end[100001],num,n,qi,a,b;
vector< vector<int> >map(100001);
void dfs(int v)
{
	int i;
	mark[v]=1;
	end[v]= num;
	for(i=0;i<map[v].size();i++)
	{
		if(mark[map[v][i]]==0)
		{
			num = v;
			dfs(map[v][i]);
		}
	}
}
int main()
{
	int T,j,i;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&qi);
		for(i=0;i<=n;i++)
		mark[i]=0;
		for(i=0;i<=n;i++)
			map[i].clear();
		for(i=1;i<n;i++)
		{
			scanf("%d%d",&a,&b);
			map[a].push_back(b);
			map[b].push_back(a);
		}
		num=-1;
		dfs(qi);
		for(i=1;i<n;i++)
			printf("%d ",end[i]);
		printf("%d\n",end[i]);
	}
	return 0;
} 
// 学长典型代码
#include <stdio.h>
#include <string.h>
int map[100005];
void Adjust(int a)
{
	int s=map[a];
	if(s)
	{
		Adjust(s);
		map[s]=a;
	}
}
int main()
{
	int m,n,s,i,a,b;
	scanf("%d",&m);
	while(m--)
	{
		scanf("%d%d",&n,&s);
		memset(map,0,sizeof(int)*n+1);
		for(i=1; i<n; i++)
		{
			scanf("%d%d",&a,&b);
			if(!map[b])
				map[b]=a;
			else
			{
				Adjust(a);
				map[a]=b;
			}
		}
		Adjust(s);
		map[s]=-1;
		for(i=1; i<=n; i++)
			printf(i<n?"%d ":"%d\n",map[i]);
	}
	return 0;
}
// 周文坤邻接矩阵代码
#include<stdio.h>
struct Node
{
	int next;
	Node *link;
}v[100001];
int vert,source,num;
int res[100001];
void  DFS(int source)
{
	res[source] = num;
	for(Node *node = v[source].link;node;node = node->link)
	{
		num = source;
		if(!res[node->next])
		DFS(node->next);
	}
}
void Init()
{
	for(int i=0;i<=vert;i++)
	{
		v[i].next = i;
		res[i] = 0;
		v[i].link = NULL;
	}
}
int main()
{
	int n,vert,source,s,e;
	int i,j,k;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&vert,&source);
		for(int i=0;i<=vert;i++)
		{
			v[i].next = i;
			res[i] = 0;
			v[i].link = NULL;
		}
		for(i=1;i<vert;i++)
		{
			scanf("%d%d",&s,&e);
			Node *node = new Node;
			node->next = e;
			node->link = v[s].link;
			v[s].link = node;
			Node *node1 = new Node;
			node1->next = s;
			node1->link = v[e].link;
			v[e].link = node1;
		}
		num = -1;
		DFS(source);
		res[source] = -1;
		printf("%d",res[1]);
		for(i=2;i<=vert;i++)
		{
			printf(" %d",res[i]);
		}
		printf("\n");
	}
	return 0;
}
时间: 2024-08-04 10:20:41

nyoj题目20吝啬的国度【深搜】的相关文章

ZOJ(ZJU) 1002 Fire Net(深搜)

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The fo

acm-邻接矩阵能进行深搜么。现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗

问题描述 邻接矩阵能进行深搜么.现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗 邻接矩阵如果能进行的话.麻烦大神给点代码啊.小白..如题...... 解决方案 void dfs(int i){ visited[i]=1; for(int j=0;j<n;j++){ if(G[i][j]==1&&visited[j]==0){ dfs(j); } } } 解决方案二: 这是用邻接矩阵求最小生成树的,题主看下能否帮的上忙,如果能帮的上,希望可以采纳 #include #include

c++-UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法

问题描述 UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法 这是我的代码,优化过几次,但是还是很慢很慢,求大神给优化途经,就是在我的代码的进行优化 #include #include #include #include using namespace std; bool search(int,set&,int dep,int); int MAX=1; int tempMax; int main() { sets;int n; for(n=2;n!=1000;++n) { s.cle

穷举搜索:回溯与深搜

计算机常用算法大致有两大类,一类叫蛮力算法,一类叫贪心算法,前者常使用的手段就是搜索,对全部解空间进行地毯式搜索,直到找到指定解或最优解. [建立解空间] 问题的解应该如何描述,如何建立?借助图论的思想,我们可以用图来描述,图的定义为G,由顶点集和边集构成,顶点即实实在在的数 据.对象,而边可以抽象为关系,即顶点间的关系,这种关系不一定非要在数据结构上表现出来,用数据结构的语言来描述,如果关系是一对一,则为线性表,如果 关系是一对多,则为树,如果关系是多对多,则为图,如果完全没有关系,则为集合.

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

HDOJ/HDU 1015 Safecracker(深搜)

Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

深搜算法实例:老鼠走迷宫(一)

这个是简单的深搜,应该输入深搜中抛砖型的,联系下代码,回顾一下深搜的思想. 本题的要求是,在开始点(1,1)和终点(5,5)放一只老鼠,让老鼠找到一条路径走出去(暂时不考虑最短路径),找到后输出路径. 最简单的想法就是对于上下左右四个进行刨根型的搜索,找到就返回输出,进入死胡同了就原路返回,找最近的有其他路径的点,继续搜索,知道找出为止. 下面是代码部分. #include <stdio.h> #include <stdlib.h> #define SUCCESS 1 #defin

深搜算法:倒油/面向对象的思想来做

题目:有一位厨师要从盛12斤油(a桶)的桶中倒出6斤油来,可是手边只有盛8斤油(b桶)和盛5斤油(c桶)的两个桶,问如何操作才能将6斤取出来呢? 下面为JAVA实现代码: 主类: package cn.hncu.oil.dfs1; import cn.hncu.oil.common.Bucket; import cn.hncu.oil.common.DumpCase; import cn.hncu.oil.common.Myset; public class DumpOilDFS { publi

NYOJ题目21-三个水杯

三个水杯 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只能根据给 出的水杯体积来计算.现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数. 输入 第一行一个整数N(0<N<50)表示N组测试数据 接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水