Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1562118
  • 博文数量: 327
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 3556
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-05 21:28
个人简介

东黑布衣,流浪幽燕。 真诚善良,值得信赖。

文章分类

全部博文(327)

我的朋友

分类: BSD

2005-06-06 17:43:58

You are to find the closest common ancestor of two vertices in a binary tree.
For example, the common ancestors of vertices 8 and 13 in the figure below ar
e vertices 3 and 1. Among them, vertex 3 is the closest to the vertex 8 and 1
3. And the size of sub-tree (the number of vertices in the sub-tree) rooted b
y vertex 3 is 8.



Given a binary tree and two vertices, write a program that finds the closest
common ancestor and computes the size of the sub-tree rooted by the closest c
ommon ancestor. No input is given where one of the two given vertices is an a
ncestor of the other. For example,
11 and 3 in the above tree is an invalid
input. Therefore, your program does not have to consider this case.

一个节点是另一个节点祖先的情况不会出现,所以不用考虑。

[Constraints]

The number of vertices are from 3 to 10000

 

[Input]
You are given 10 test cases. Each test case has two lines, so the total numbe
r of lines is 20. In each test case, the first line consists of four integers,
V (the number of vertices in the tree), E (the number of edges), and the indi
ces of two vertices. E edges are listed in the next line. Each edge is repres
ented by two vertices; the index of the parent vertex always precedes the ind
ex of the child. For example, the edge connecting vertices 5 and 8 is represe
nted by
5 8, not by 8 5. There is no order in which the edges are given.
Every consecutive integer in the input is separated by a space.


Given 10 test cases,

First 4 test cases contain small number of vertices(3, 5, 7, 10 each).

Next 6 test cases contain same or greater than 50 vertices.


The indices of vertices are integers from 1 to V, and root vertex always has
the index 1.父亲的节点编号总是比孩子节点的编号小。

It is guaranteed that the parent vertex has smaller index than the child vertex.

In this problem, it is not important whether the child is the left child of t
he parent or the right child; so you can decide this arbitrarily.

 

[Output]
Output 10 answers in 10 lines. Each line starts with
#x meaning the index o
f a test case, and writes the answer after a space. The answer has two intege
rs: the index of the closest common ancestor and the size of the sub-tree roo
ted by the closest common ancestor. These two integers are separated by a spa
ce as well.

 

[I/O Example]

Input (20 lines in total)

13 12 8 13 Start of the first input

1 2 1 3 2 4 3 5 3 6 4 7 7 12 5 9 5 8 6 10 6 11 11 13

10 9 1 10 Start of the second input

1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

...

Output (10 lines in total)

#1 3 8

#2 1 10

...

-----------------------------------------------------------------------------
Code Begin
  1. #include <stdio.h>
  2. #include <queue>

  3. #ifdef LOCAL_DEBUG
  4. #pragma warning(disable : 4996)
  5. #endif

  6. #define node_num 10000

  7. struct node
  8. {
  9.     int p, s1, s2;
  10. };

  11. struct node tree[node_num];
  12. int cnt;

  13. int LCA(int p1, int p2)
  14. {
  15.     int pos, len1 = 1, len2 = 1, frt1, frt2;
  16.     std::queue <int> q1, q2;

  17.     pos = p1;
  18.     q1.push(pos);
  19.     while (pos != 1)
  20.     {
  21.         pos = tree[pos].p;
  22.         q1.push(pos);
  23.         len1++;
  24.     }
  25.     pos = p2;
  26.     q2.push(pos);
  27.     while (pos != 1)
  28.     {
  29.         pos = tree[pos].p;
  30.         q2.push(pos);
  31.         len2++;
  32.     }

  33.     while (len1 > len2)
  34.     {
  35.         q1.pop();
  36.         len1--;
  37.     }
  38.     while (len2 > len1)
  39.     {
  40.         q2.pop();
  41.         len2--;
  42.     }

  43.     while (1)
  44.     {
  45.         frt1 = q1.front();
  46.         frt2 = q2.front();
  47.         if (frt1 == frt2)
  48.         {
  49.             pos = frt1;
  50.             break;
  51.         }

  52.         q1.pop();q2.pop();
  53.     }

  54.     return pos;
  55. }

  56. void count(int pt)
  57. {
  58.     cnt++;

  59.     if (tree[pt].s1 != 0)
  60.     {
  61.         count(tree[pt].s1);
  62.     }

  63.     if (tree[pt].s2 != 0)
  64.     {
  65.         count(tree[pt].s2);
  66.     }
  67. }

  68. int main()
  69. {
  70.     int test_case;
  71.     int T;

  72. #ifdef LOCAL_DEBUG
  73.     freopen("test_input.txt", "r", stdin);
  74.     freopen("sample_output.txt", "w", stdout);
  75. #endif

  76.     setbuf(stdout, NULL);
  77.     scanf("%d", &T);

  78.     for (test_case = 1; test_case <= T; test_case++)
  79.     {
  80.         int i, n, e, p1, p2, temp1, temp2, ans;

  81.         scanf("%d %d %d %d", &n, &e, &p1, &p2);
  82.         for (i = 0; i < n; i++)
  83.         {
  84.             tree[i].p = 0;tree[i].s1 = 0; tree[i].s2 = 0;
  85.         }
  86.         for (i = 0; i < e; i++)
  87.         {
  88.             scanf("%d %d ", &temp1, &temp2);
  89.             if (tree[temp1].s1 == 0)
  90.             {
  91.                 tree[temp1].s1 = temp2;
  92.             }
  93.             else
  94.             {
  95.                 tree[temp1].s2 = temp2;
  96.             }

  97.             tree[temp2].p = temp1;
  98.         }

  99.         ans = LCA(p1, p2);
  100.         cnt = 0;
  101.         count(ans);
  102.         
  103.         printf("#%d %d %d\n", test_case, ans, cnt);
  104.     }
  105.     
  106.     return 0;
  107. }
Code End

Code Using My Que
  1. #include <stdio.h>
  2. #define NODE_NB 10000
  3. #define QUE_SZ 10000
  4. #define LOCAL_DEBUG
  5. typedef struct{
  6.     int hd;
  7.     int tl;
  8.     int nd[QUE_SZ];
  9. }QUE;

  10. int que_init(QUE *q);
  11. int que_empty(QUE *q);
  12. int que_full(QUE *q);
  13. int que_head(QUE *q);
  14. int que_tail(QUE *q);
  15. int pop_head(QUE *q);
  16. int push_tail(QUE *q,int n);
  17. int que_clr(QUE *q);
  18. int que_size(QUE *q);
  19. int que_init(QUE *q)
  20. {
  21.     if(NULL==q)
  22.         return -1;
  23.     q->hd=0;
  24.     q->tl=0;
  25.     return 0;
  26. }

  27. /*
  28.  int que_emtpy(QUE *q)
  29.  {
  30.  return q->hd==q->tl;
  31.  }
  32.  */
  33. int que_full(QUE *q)
  34. {
  35.     return (q->tl+1)%QUE_SZ==q->hd;
  36. }

  37. int que_head(QUE *q)
  38. {
  39.     if(q->hd!=q->tl)
  40.         return q->nd[q->hd];
  41.     return -1;
  42. }

  43. int que_tail(QUE *q)
  44. {
  45.     int i;
  46.     if(q->hd!=q->tl){
  47.         i=(q->tl+QUE_SZ-1)%QUE_SZ;
  48.         return q->nd[i];
  49.     }
  50.     return -1;
  51. }

  52. int pop_head(QUE *q)
  53. {
  54.     int data=-1;
  55.     if(q->hd!=q->tl){
  56.         data=q->nd[q->hd];
  57.         q->hd=(q->hd+1)%QUE_SZ;
  58.     }
  59.     return data;
  60. }
  61. int push_tail(QUE *q,int n)
  62. {
  63.     if(que_full(q))
  64.         return -1;
  65.     q->nd[q->tl]=n;
  66.     q->tl=(q->tl+1)%QUE_SZ;
  67.     return 1;
  68. }

  69. int que_clr(QUE *q)
  70. {
  71.     if(q->hd==q->tl)
  72.         return 0;
  73.     q->hd=0;
  74.     q->tl=0;
  75.     return 0;
  76. }

  77. int que_size(QUE *q)
  78. {
  79.     return (q->tl-q->hd+QUE_SZ)%QUE_SZ;
  80. }

  81. typedef struct node
  82. {
  83.     int p, s1, s2;
  84. }NODE;

  85. NODE tree[NODE_NB];
  86. int cnt;

  87. int LCA(int p1, int p2)
  88. {
  89.     int pos, len1 = 1, len2 = 1, frt1, frt2;
  90.     QUE q1, q2;
  91.     
  92.     pos = p1;
  93.     push_tail(&q1,pos);
  94.     while (pos != 1)/* */
  95.     {
  96.         pos = tree[pos].p;
  97.         push_tail(&q1, pos);
  98.         len1++;
  99.     }
  100.     pos = p2;
  101.     push_tail(&q2,pos);
  102.     while (pos != 1)
  103.     {
  104.         pos = tree[pos].p;
  105.         push_tail(&q2,pos);
  106.         len2++;
  107.     }
  108.     
  109.     while (len1 > len2)
  110.     {
  111.         pop_head(&q1);
  112.         len1--;
  113.     }
  114.     while (len2 > len1)
  115.     {
  116.         pop_head(&q2);
  117.         len2--;
  118.     }
  119.     
  120.     while (1)
  121.     {
  122.         frt1 = que_head(&q1);
  123.         frt2 = que_head(&q2);
  124.         if (frt1 == frt2)
  125.         {
  126.             pos = frt1;
  127.             break;
  128.         }
  129.         
  130.         pop_head(&q1);
  131.         pop_head(&q2);
  132.     }
  133.     
  134.     return pos;
  135. }

  136. void count(int pt)
  137. {
  138.     cnt++;
  139.     
  140.     if (tree[pt].s1 != 0)
  141.     {
  142.         count(tree[pt].s1);
  143.     }
  144.     
  145.     if (tree[pt].s2 != 0)
  146.     {
  147.         count(tree[pt].s2);
  148.     }
  149. }

  150. int main(int argc, const char * argv[])
  151. {
  152.     int test_case;
  153.     int T;
  154.     
  155. #ifdef LOCAL_DEBUG
  156.     freopen("test_input.txt", "r", stdin);
  157.     freopen("adv05_o.txt", "w", stdout);
  158. #endif
  159.     
  160.     setbuf(stdout, NULL);
  161.     scanf("%d", &T);
  162.     
  163.     for (test_case = 1; test_case <= T; test_case++)
  164.     {
  165.         int i, n, e, p1, p2, temp1, temp2, ans;
  166.         
  167.         scanf("%d %d %d %d", &n, &e, &p1, &p2);
  168.         for (i = 0; i < n; i++)
  169.         {
  170.             tree[i].p = 0;tree[i].s1 = 0;tree[i].s2 = 0;
  171.         }
  172.         for (i = 0; i < e; i++)
  173.         {
  174.             scanf("%d %d ", &temp1, &temp2);
  175.             if (tree[temp1].s1 == 0)
  176.             {
  177.                 tree[temp1].s1 = temp2;
  178.             }
  179.             else
  180.             {
  181.                 tree[temp1].s2 = temp2;
  182.             }
  183.             
  184.             tree[temp2].p = temp1;
  185.         }
  186.         
  187.         ans = LCA(p1, p2);
  188.         cnt = 0;
  189.         count(ans);
  190.         
  191.         printf("#%d %d %d\n", test_case, ans, cnt);
  192.     }
  193.     
  194.     return 0;
  195. }
Code Using My Que






成大事只要一点勇气
阅读(3004) | 评论(4) | 转发(0) |
0

上一篇:20180428 PRO

下一篇:陶渊明《杂诗》之一

给主人留下些什么吧!~~

zhln2016-09-05 11:53:53

林肯:我父亲在西雅图有一处农场,上面有许多石头。正因如此,父亲才得以以较低的价 格买下。有一天,母亲建议把上面的石头搬走。父亲说,如果可以搬,主人就不会卖给我 们了,它们是一座座小山头,都与大山连着。 有一年,父亲去城里买马,母亲带我们在农场里劳动。母亲说,让我们把这些碍事的 东西搬走好吗?于是我们开始挖那一块块石头。不长时间,就把它们给弄走了,因为它们 并不是父亲想象的山头,而是一块块孤伶伶的石块,只要往下挖一英尺,就可以把它们晃动。 有些事情一些人之所以不去做,只是因为他们认为不可能。其实,有许多不可能,只存在于人的想象之中。

chinaunix网友2009-03-24 12:54:02

徜徉于指尖的犹豫 刻画着淡泊的旋律 音容笑貌 如何继续下去