fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct employee {
  5. int id;
  6. char first_name[32];
  7. char last_name[32];
  8. int boss_id;
  9. };
  10.  
  11. int find(int *parent, int x, struct employee e[]) {
  12. int temp=parent[x];
  13. if (e[temp].id != e[x].id) {
  14. temp = find(parent, temp, e);
  15. }
  16. return temp;
  17. }
  18.  
  19. void unite(int *parent, int x, int y, struct employee e[]) {
  20. int rootX = find(parent, x, e);
  21. int rootY = find(parent, y, e);
  22. if (rootX != rootY) {
  23. parent[rootX] = rootY;
  24. }
  25. }
  26.  
  27. int check(int *parent, int x, int y, struct employee e[]) {
  28. int temp=parent[x];
  29. if (e[temp].id != e[x].id) {
  30. printf("%d %d\n",x,y);
  31. if (e[y].id == e[x].id) {
  32. return -1;
  33. }
  34. temp = check(parent, temp, y, e);
  35. }
  36.  
  37. if (e[y].id == e[x].id) {
  38. return -1;
  39. }
  40. return temp;
  41. }
  42.  
  43. int main() {
  44. int n, m;
  45. scanf("%d", &n);
  46. struct employee e[n];
  47. int parent[n];
  48.  
  49. // Read employee data
  50. for (int i = 0; i < n; i++) {
  51. scanf("%d %s %s %d", &e[i].id, e[i].first_name, e[i].last_name, &e[i].boss_id);
  52. parent[i] = i;
  53. }
  54.  
  55. // Build hierarchy
  56. for (int i = 0; i < n; i++) {
  57. for (int j = 0; j < n; j++) {
  58. if (e[j].id == e[i].boss_id) {
  59. unite(parent, i, j, e);
  60. }
  61. }
  62. }
  63.  
  64. // Process relationship queries
  65. scanf("%d", &m);
  66. for (int i = 0; i < m; i++) {
  67. char al[32], af[32], bl[32], bf[32];
  68. int a = -1, b = -1;
  69. scanf("%s %s %s %s", af, al, bf, bl);
  70.  
  71. // Find indices of queried employees
  72. for (int j = 0; j < n; j++) {
  73. if (strcmp(al, e[j].last_name) == 0 && strcmp(af, e[j].first_name) == 0) {
  74. a = j;
  75. }
  76. if (strcmp(bl, e[j].last_name) == 0 && strcmp(bf, e[j].first_name) == 0) {
  77. b = j;
  78. }
  79. }
  80.  
  81. if (a == -1 || b == -1) {
  82. printf("Employee not found\n");
  83. continue;
  84. }
  85. printf("%d\n",check(parent, a,b, e));
  86. if (find(parent, a, e) == find(parent, b, e)) {
  87. if (check(parent, a, b, e)==-1) {
  88. printf("subordinate\n");
  89. } else if (check(parent, b, a, e)==-1) {
  90. printf("supervisor\n");
  91. } else {
  92. printf("colleague\n");
  93. }
  94. } else {
  95. printf("unrelated\n");
  96. }
  97. }
  98. return 0;
  99. }
Success #stdin #stdout 0s 5284KB
stdin
6
100 John Smith 200
200 Adam Joshson 300
300 Jane Washington 400
400 Mary Miller 400
500 Eric Page 500
600 James Clark 500
4
John Smith Jane Washington
Jane Washington Mary Miller
Adam Joshson Mary Miller
Mary Miller James Clark
stdout
0 2
1 2
2 2
-1
0 2
1 2
2 2
subordinate
2 3
-1
2 3
subordinate
1 3
2 3
-1
1 3
2 3
subordinate
3
unrelated