Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <queue>
  2. #include <vector>
  3. #include <cstring>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. struct circle {
  9. int x, y, r;
  10. circle() {}
  11. circle(int x, int y, int r) : x(x), y(y), r(r) {
  12.  
  13. }
  14. };
  15.  
  16. vector<circle> a; // 원 정보
  17. vector<int> tree[101]; // 트리 정보
  18. vector<pair<int, int>> p; // 부모의 노드와 반지름
  19. int dist[101]; // bfs를 위한 배열
  20. int main(void) {
  21. ios_base::sync_with_stdio(false);
  22. cin.tie(nullptr);
  23.  
  24. int tc;
  25. cin >> tc;
  26. while (tc--) {
  27. memset(dist, -1, sizeof(dist));
  28. int n;
  29. cin >> n;
  30. a = vector<circle>(n + 1);
  31. p = vector<pair<int, int>>(n + 1);
  32. for (int i = 1; i <= n; i++) {
  33. int x, y, r;
  34. cin >> x >> y >> r;
  35. a[i] = circle(x, y, r);
  36. tree[i] = vector<int>();
  37. }
  38.  
  39. for (int i = 1; i < n; i++) {
  40. for (int j = i + 1; j <= n; j++) {
  41. int x1, y1, r1, x2, y2, r2;
  42. x1 = a[i].x, y1 = a[i].y, r1 = a[i].r;
  43. x2 = a[j].x, y2 = a[j].y, r2 = a[j].r;
  44.  
  45. int d = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  46. if (d > ((r1 + r2) * (r1 + r2))) continue; // 서로 속하지 않는다면
  47. if (!(d < ((r1 - r2) * (r1 - r2)))) continue;
  48.  
  49. if (r1 > r2) {
  50. if (p[j].second != 0) { // 부모 정보가 있지만 저장된 반지름이 현재보다 크다면
  51. if (p[j].second > r1) {
  52. p[j] = make_pair(i, r1);
  53. }
  54. }
  55. else {
  56. p[j] = make_pair(i, r1);
  57. }
  58. }
  59. else {
  60. if (p[i].second != 0) {
  61. if (p[i].second > r2) {
  62. p[i] = make_pair(j, r2);
  63. }
  64. }
  65. else {
  66. p[i] = make_pair(j, r2);
  67. }
  68. }
  69. }
  70. }
  71.  
  72. for (int i = 1; i <= n; i++) { // 업데이트 된 부모 정보를 위해 트리정보 구성
  73. if (p[i].first == 0) { // 부모 정보가 없다면 최외곽 정보
  74. tree[0].push_back(i);
  75. tree[i].push_back(0);
  76. }
  77. else {
  78. tree[p[i].first].push_back(i);
  79. tree[i].push_back(p[i].first);
  80. }
  81. }
  82.  
  83. queue<int> q;
  84. q.push(0);
  85. dist[0] = 0;
  86. int node = 0, mdist = 0;
  87. while (!q.empty()) {
  88. int x = q.front();
  89. q.pop();
  90. for (int k = 0; k < tree[x].size(); k++) {
  91. int y = tree[x][k];
  92. if (dist[y] != -1) continue;
  93. dist[y] = dist[x] + 1;
  94. if ((mdist == 0 || dist[y] > mdist) && y != 0) { // 0번 노드가 아니면서 최장 거리
  95. mdist = dist[y];
  96. node = y;
  97. }
  98. q.push(y);
  99. }
  100. }
  101.  
  102. memset(dist, -1, sizeof(dist));
  103. dist[node] = 0;
  104. q = queue<int>();
  105. q.push(node);
  106. mdist = 0;
  107. while (!q.empty()) {
  108. int x = q.front();
  109. q.pop();
  110.  
  111. for (int k = 0; k < tree[x].size(); k++) {
  112. int y = tree[x][k];
  113. if (dist[y] != -1) continue;
  114. dist[y] = dist[x] + 1;
  115. if ((mdist == 0 || dist[y] > mdist) && y != 0) { // 0번 노드가 아니면서 최장 거리라면
  116. mdist = dist[y];
  117. }
  118. q.push(y);
  119. }
  120. }
  121.  
  122. cout << mdist << '\n';
  123. }
  124.  
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement