码迷,mamicode.com
首页 > 其他好文 > 详细

搜索---吝啬的国度

时间:2016-05-24 00:03:08      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>
#include <memory.h>

int map[100005];

void Adjust(int currentCity) //定义函数(递归)
{
int priorCity = map[currentCity];
if (priorCity != 0)
{
Adjust(priorCity);
map[priorCity] = currentCity;
}
}

int main()
{
int i, testNum, cityNum, startCity, cityA, cityB;
scanf("%d", &testNum);
while (testNum-- != 0)
{
scanf("%d%d", &cityNum, &startCity);
memset(map, 0, sizeof(int)*cityNum + 1);//meset的用法
for (i = 1; i < cityNum; i++)
{
scanf("%d%d", &cityA, &cityB);
if (map[cityB] == 0)
{
map[cityB] = cityA;
}
else
{
Adjust(cityA);
map[cityA] = cityB;
}
}
Adjust(startCity);
map[startCity] = - 1;
for (i = 1; i < cityNum; i++)
{
printf("%d ", map[i]);
}
printf("%d\n", map[i]);
}
return 0;
}

搜索---吝啬的国度

标签:

原文地址:http://www.cnblogs.com/CAOYR/p/5521693.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!