zoukankan      html  css  js  c++  java
  • nyoj20 吝啬的国度

    吝啬的国度

    时间限制: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 #include<stdio.h>
    #include<malloc.h>
    struct node
    {
     int v;
     struct node *next;
    };
    void DFS(struct node *city,int num)
    {
     struct node *t;
     for(t=city[num].next;t!=NULL;t=t->next)
     {
      if(city[t->v].v==0)
      {
       city[t->v].v=num;
       DFS(city,t->v);
      }
     }
    }
    int main()
    {
     int n,numv,num,i,a,b;
     struct node *city;
     struct node *t1,*t2;
     scanf("%d",&n);
     while(n--)
     {
      scanf("%d%d",&numv,&num);
      city=(struct node*)malloc((numv+1)*sizeof(struct node));
      for(i=1;i<=numv;++i)
      {
       city[i].v=0;
       city[i].next=NULL;
      }
      for(i=1;i<numv;++i)
      {
       scanf("%d%d",&a,&b);
       t1=(struct node*)malloc(sizeof(struct node));
       t1->v=b;
       t1->next=city[a].next;
       city[a].next=t1;
       t2=(struct node*)malloc(sizeof(struct node));
       t2->v=a;
       t2->next=city[b].next;
       city[b].next=t2;
      }
      city[num].v=-1; 
      DFS(city,num);
      for(i=1;i<numv;++i)
       printf("%d ",city[i].v);
      printf("%d\n",city[i].v);
     }
     return 0;
    }
    功不成,身已退
  • 相关阅读:
    iOS开发UI篇—CAlayer简介
    iOS开发UI篇—ios手势识别(双击、捏、旋转、拖动、划动、长按, 上下左右滑动)
    录屏专家
    加载Gif图片方法
    制作酸奶方法
    UITabBar小红点(适配iPad)
    那些著名或非著名的iOS面试题-后编
    iOS学习资源
    实用的Mac软件
    安装iOS企业包流程
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2534492.html
Copyright © 2011-2022 走看看