吝啬的国度
时间限制: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
前两天刚学过图论中的最短路,今天做这道题,首先想到了用最短路的思想做。写了一个Bellman-Ford,超时了,原因是顶点数*边数太大。然后用广搜写了一下,AC。存储边时用了vector,记得每次都要清空vector。
#include#include #include #include using namespace std; const int N = 100010; int pre[N], vis[N], n, s; vector vec[N]; void Init() { for(int i = 1; i <= n; i++) pre[i] = i, vis[i] = 0; pre[s] = -1; } void bfs() { queue Q; Q.push(s); while(!Q.empty()) { int t = Q.front(); Q.pop(); if(vis[t]) continue; vis[t] = 1; for(int i = 0; i < vec[t].size(); i++) { int p = vec[t][i]; if(!vis[p]) { pre[p] = t; Q.push(p); } } } } int main() { int t, i; scanf("%d",&t); while(t--) { memset(vec, 0, sizeof(vec)); scanf("%d%d",&n,&s); int a, b; for(i = 0; i < n - 1; i++) { scanf("%d%d",&a,&b); vec[a].push_back(b); vec[b].push_back(a); } Init(); bfs(); printf("%d",pre[1]); for(i = 2; i <= n; i++) printf(" %d",pre[i]); printf("\n"); } return 0; }
后来看讨论区,出题人说是把无根树转化为有根树,仔细一想,还真是。下面是代码:/*无根树转有根树,s即为根*/ #include#include #include using namespace std; const int N = 100010; int pre[N], n, s; vector vec[N]; void Init() { for(int i = 1; i <= n; i++) pre[i] = i; pre[s] = -1; } void dfs(int u, int fa) { int k = vec[u].size(); for(int i = 0; i < k; i++) { int v = vec[u][i]; if(v != fa) { pre[v] = u; dfs(v, u); } } } int main() { int t, i; scanf("%d",&t); while(t--) { memset(vec, 0, sizeof(vec)); scanf("%d%d",&n,&s); int a, b; for(i = 0; i < n - 1; i++) { scanf("%d%d",&a,&b); vec[a].push_back(b); vec[b].push_back(a); } Init(); dfs(s,-1); //从根节点开始搜 printf("%d",pre[1]); for(i = 2; i <= n; i++) printf(" %d",pre[i]); printf("\n"); } return 0; }
- <script type="text/java script">BAIDU_CLB_fillSlot("771048");
- 点击复制链接 与好友分享! 回本站首页 <script> function copyToClipBoard(){ var clipBoardContent=document.title + '\r\n' + document.location; clipBoardContent+='\r\n'; window.clipboardData.setData("Text",clipBoardContent); alert("恭喜您!复制成功"); }
<script type="text/java script" id="bdshare_js" data="type=tools&uid=12732"> <script type="text/java script" id="bdshell_js"> <script type="text/java script"> var bds_config = {'snsKey':{'tsina':'2386826374','tqq':'5e544a8fdea646c5a5f3967871346eb8'}}; document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js cdnversion=" + Math.ceil(new Date()/3600000) - 您对本文章有什么意见或着疑问吗?请到 论坛讨论您的关注和建议是我们前行的参考和动力
- 上一篇: 二叉树中节点的最大的距离(编程之美3.8)
- 下一篇: 最后一页
- 相关文章
- <script type="text/java script">BAIDU_CLB_fillSlot("182716");
- <script type="text/java script">BAIDU_CLB_fillSlot("517916");
- 图文推荐
<iframe src="http://www.2cto.com/uapi.php tid=279685&catid=339&title=TllPSiAyMCDB39jEtcS5 rbIIKOoy9HL96Op&forward=http://www.2cto.com/kf/201402/279685.html" width="100%" height="100%" id="comment_iframe" name="comment_iframe" frameborder="0" scrolling="no">
- <script type="text/java script">BAIDU_CLB_fillSlot("771057");



