hdu 4039

  1. #include <stdio.h>
  2. #include <map>
  3. #include <string>
  4. #include <iostream>
  5. #include <queue>
  6. #include <set>
  7. using namespace std;
  8. #define MAX 1500
  9. bool path[MAX][MAX];
  10. bool num[MAX];
  11. map<string, int> list;
  12. map<int , string>list1;
  13. map<string, int>::iterator iter;
  14. map<int , string>::iterator iter1;
  15. int flag;
  16. struct data
  17. {
  18. string str;
  19. int count;
  20. data()
  21. {
  22. count = 0;
  23. }
  24. }peo[MAX];
  25. int flag1;
  26. void input(int n)
  27. {
  28. list.clear();
  29. list1.clear();
  30. flag = 0;
  31. //flag1 = 0;
  32. string str1, str2;
  33. for(int i=0; i<n; i++)
  34. {
  35. cin >> str1 >> str2;
  36. int go, to;
  37. if( (iter = list.find(str1) )!= list.end() )
  38. go = iter->second;
  39. else
  40. {
  41. list[str1] = flag++;
  42. list1[flag-1] = str1;
  43. go = flag-1;
  44. }
  45. if( (iter = list.find(str2) )!= list.end() )
  46. to = iter->second;
  47. else
  48. {
  49. list[str2] = flag++;
  50. list1[flag-1] = str2;
  51. to = flag-1;
  52. }
  53. path[go][to] = path[to][go] = true;
  54. }
  55. }
  56. void bfs(string str)
  57. {
  58. iter = list.find(str);
  59. int start = iter->second;
  60. num[start] = true;
  61. queue<int>Q;
  62. Q.push(start);
  63. int tp = Q.front();
  64. Q.pop();
  65. for(int i=0; i<flag; i++)
  66. {
  67. if(path[tp][i])
  68. {
  69. if(!num[i])
  70. {
  71. Q.push(i);
  72. num[i] = true;
  73. }
  74. }
  75. }
  76. while(!Q.empty() )
  77. {
  78. int tp = Q.front();
  79. Q.pop();
  80. for(int i=0; i<flag; i++)
  81. {
  82. if(path[tp][i])
  83. {
  84. if(path[i][start] == false && i!=start)
  85. {
  86. iter1 = list1.find(i);
  87. bool sign = false;
  88. for(int j=0; j<flag1; j++)
  89. {
  90. if(peo[j].str == iter1->second)
  91. {
  92. peo[j].count++;
  93. sign = true;
  94. break;
  95. }
  96. }
  97. if(!sign)
  98. {
  99. peo[flag1].str = iter1->second;
  100. peo[flag1].count=1;
  101. flag1++;
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. bool cmp(data a, data b)
  109. {
  110. if(a.count != b.count) return a.count > b.count ? 1:0;
  111. else
  112. return a.str < b.str ? 1:0;
  113. }
  114. int main()
  115. {
  116. //freopen("read.txt", "r", stdin);
  117. int T;
  118. scanf("%d", &T);
  119. int co = 1;
  120. while(T--)
  121. {
  122. printf("Case %d:\n", co++);
  123. int n, m;
  124. memset(path, false, sizeof(path) );
  125. scanf("%d%d", &n, &m);
  126. input(n);
  127. string str;
  128. for(int i=0; i<m; i++)
  129. {
  130. flag1 = 0;
  131. memset(num, false, sizeof(num) );
  132. cin >> str;
  133. bfs(str);
  134. if(flag1 != 0)
  135. {
  136. sort(peo, peo+flag1, cmp);
  137. int max = peo[0].count;
  138. int tp = 0;
  139. while(peo[tp].count == max)
  140. {
  141. cout << peo[tp].str << " ";
  142. tp++;
  143. if(tp >= flag1) break;
  144. }
  145. printf("\n");
  146. }
  147. else
  148. printf("-\n");
  149. }
  150. }
  151. return 0;
  152. }





附件列表

     

    posted @ 2015-01-29 11:59  sober_reflection  阅读(112)  评论(0编辑  收藏  举报