nyoj 吝嗇的國度 http://blog.csdn.net/code_pang/article/details/7719221

 1659人閱讀 評論(0) 收藏 舉報

吝嗇的國度

時間限制: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

很明顯,這個些城市和道路構成了一個極小連通子圖,也就是生成樹。而出發的城市S就相當於這棵樹的樹根,一種較易想到的解法就是從出發城市開始,對整個地圖進行深度搜索,過程中記錄下前一個城市的編號,如下,採用鄰接表存儲地圖:

  1. #include <stdio.h>  
  2.   
  3. struct node  
  4. {  
  5.     int num;  
  6.     node *next;  
  7. };  
  8.   
  9. struct data_type  
  10. {  
  11.     int priorCity;  
  12.     node *linkedCity;  
  13. }map[100005];  
  14.   
  15. void MyDelete(int cityNum)  
  16. {  
  17.     int i;  
  18.     node *p, *q;  
  19.     for (i = 1; i <= cityNum; i++)  
  20.     {  
  21.         p = map[i].linkedCity;  
  22.         while (p != NULL)  
  23.         {  
  24.             q = p->next;  
  25.             delete p;  
  26.             p = q;  
  27.         }  
  28.     }  
  29. }  
  30.   
  31. void Travel(int currentCity, int priorCity)  
  32. {  
  33.     map[currentCity].priorCity = priorCity;  
  34.     node *linkedCity = map[currentCity].linkedCity;  
  35.     while (linkedCity != NULL)  
  36.     {  
  37.         if (map[linkedCity->num].priorCity == 0)  
  38.         {  
  39.             Travel(linkedCity->num, currentCity);  
  40.         }  
  41.         linkedCity = linkedCity->next;  
  42.     }  
  43. }  
  44.   
  45. int main()  
  46. {  
  47.     int i, testNum, cityNum, startCity, cityA, cityB;  
  48.     node *p;  
  49.     scanf("%d", &testNum);  
  50.     while (testNum-- != 0)  
  51.     {  
  52.         scanf("%d%d", &cityNum, &startCity);  
  53.         for (i = 0; i <= cityNum; i++)  
  54.         {  
  55.             map[i].priorCity = 0;  
  56.             map[i].linkedCity = NULL;  
  57.         }  
  58.         for (i = 1; i < cityNum; i++)  
  59.         {  
  60.             scanf("%d%d", &cityA, &cityB);  
  61.             p = new node;  
  62.             p->num = cityB;  
  63.             p->next = map[cityA].linkedCity;  
  64.             map[cityA].linkedCity = p;  
  65.             p = new node;  
  66.             p->num = cityA;  
  67.             p->next = map[cityB].linkedCity;  
  68.             map[cityB].linkedCity = p;  
  69.         }  
  70.         Travel(startCity, -1);  
  71.         for (i = 1; i < cityNum; i++)  
  72.         {  
  73.             printf("%d ", map[i].priorCity);  
  74.         }  
  75.         printf("%d\n", map[i].priorCity);  
  76.         MyDelete(cityNum);  
  77.     }  
  78.     return 0;  
  79. }  

上面的地圖相當於一個無向圖,而在深度搜索時,需要的只是一個以出發城市爲中心,向四周輻射的有向圖。改進算法是在輸入數據的同時,就進行搜索地圖,因爲數據輸入未完成,所以輸入時得到的是一個子圖,這個子圖分兩種情況,一種是子圖中包含出發城市,子圖是一個有向圖,所以可以根據輸入的兩個城市哪一個離出發城市更近,確定結果;另一種子圖中不包含出發城市,此時,無法確定哪個城市離出發城市更近,所以先用鄰接表將這個無向子圖存儲起來,等到它與出發城市相連時,在對這個子圖進行深度搜索。


例如輸入的測試數據爲:10 18 1010 33 710 41 91 88 61 29 5則首先得到一個不包含出發城市的子圖:

在輸入數據1  8時之後,上面的子圖與出發城市相連,圖中紅色方塊代表出發城市,虛線箭頭代表並未在鄰接表中建立此聯繫:


  1. #include <stdio.h>  
  2.   
  3. struct node  
  4. {  
  5.     int num;  
  6.     node *next;  
  7. };  
  8.   
  9. struct data_type  
  10. {  
  11.     int priorCity;  
  12.     bool start;  
  13.     node *linkedCity;  
  14. }map[100005];  
  15.   
  16. void InitMap(int cityNum, int startCity)  
  17. {  
  18.     int i;  
  19.     for (i = 0; i <= cityNum; i++)  
  20.     {  
  21.         map[i].priorCity = 0;  
  22.         map[i].start = false;  
  23.         map[i].linkedCity = NULL;  
  24.     }  
  25.     map[startCity].start = true;  
  26.     map[startCity].priorCity = -1;  
  27. }  
  28.   
  29. void MyDelete(int cityNum)  
  30. {  
  31.     int i;  
  32.     node *p, *q;  
  33.     for (i = 1; i <= cityNum; i++)  
  34.     {  
  35.         p = map[i].linkedCity;  
  36.         while (p != NULL)  
  37.         {  
  38.             q = p->next;  
  39.             delete p;  
  40.             p = q;  
  41.         }  
  42.     }  
  43. }  
  44.   
  45. void Travel(int currentCity, int priorCity)  
  46. {  
  47.     map[currentCity].priorCity = priorCity;  
  48.     map[currentCity].start = true;  
  49.     node *linkedCity = map[currentCity].linkedCity;  
  50.     while (linkedCity != NULL)  
  51.     {  
  52.         if (map[linkedCity->num].priorCity == 0)  
  53.         {  
  54.             Travel(linkedCity->num, currentCity);  
  55.         }  
  56.         linkedCity = linkedCity->next;  
  57.     }  
  58. }  
  59.   
  60. int main()  
  61. {  
  62.     int i, testNum, cityNum, startCity, cityA, cityB;  
  63.     node *p;  
  64.     scanf("%d", &testNum);  
  65.     while (testNum-- != 0)  
  66.     {  
  67.         scanf("%d%d", &cityNum, &startCity);  
  68.         InitMap(cityNum, startCity);  
  69.         for (i = 1; i < cityNum; i++)  
  70.         {  
  71.             scanf("%d%d", &cityA, &cityB);  
  72.             if (map[cityA].start)  
  73.             {  
  74.                 if (map[cityB].linkedCity != NULL)  
  75.                 {  
  76.                     Travel(cityB, cityA);  
  77.                 }  
  78.                 else  
  79.                 {  
  80.                     map[cityB].priorCity = cityA;  
  81.                     map[cityB].start = true;  
  82.                 }  
  83.             }  
  84.             else if (map[cityB].start)  
  85.             {  
  86.                   
  87.                 if (map[cityA].linkedCity != NULL)  
  88.                 {  
  89.                     Travel(cityA, cityB);  
  90.                 }  
  91.                 else  
  92.                 {  
  93.                     map[cityA].priorCity = cityB;  
  94.                     map[cityA].start = true;  
  95.                 }  
  96.             }  
  97.             else  
  98.             {  
  99.                 p = new node;  
  100.                 p->num = cityB;  
  101.                 p->next = map[cityA].linkedCity;  
  102.                 map[cityA].linkedCity = p;  
  103.                 p = new node;  
  104.                 p->num = cityA;  
  105.                 p->next = map[cityB].linkedCity;  
  106.                 map[cityB].linkedCity = p;  
  107.             }  
  108.         }  
  109.         for (i = 1; i < cityNum; i++)  
  110.         {  
  111.             printf("%d ", map[i].priorCity);  
  112.         }  
  113.         printf("%d\n", map[i].priorCity);  
  114.         MyDelete(cityNum);  
  115.     }  
  116.     return 0;  
  117. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章