Advertisement
a53

rbtree

a53
Feb 27th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. ///autor: Andrei Margeloiu
  2. # include <bits/stdc++.h>
  3. # define INF 99999999
  4. # define NR 100005
  5. using namespace std;
  6. ifstream f("rbtree.in");
  7. ofstream g("rbtree.out");
  8. queue <int> redQueue, blackQueue, rootQueue;
  9. vector <int> v[NR], vt[NR];
  10. int n, RED, BLACK, cnt, x;
  11. int redDist[NR], blackDist[NR], rootDist[NR];
  12.  
  13. void BFS (queue <int>& q, int dist[], const vector <int> (&v) [NR]) {
  14. while (!q.empty()) {
  15. int k = q.front();
  16. q.pop();
  17.  
  18. for (auto &x: v[k]) {
  19. if (dist[x] > dist[k] + 1) {
  20. dist[x] = dist[k] + 1;
  21. q.push(x);
  22. }
  23. }
  24. }
  25. }
  26.  
  27. int main ()
  28. {
  29. f>>n>>RED>>BLACK;
  30.  
  31. for (int i=1; i<=n; ++i)
  32. redDist[i] = blackDist[i] = rootDist[i] = INF;
  33.  
  34. for (int i=1; i<=RED; ++i) {
  35. f>>x; redDist[x] = 0;
  36. redQueue.push(x);
  37. }
  38.  
  39. for (int i=1; i<=BLACK; ++i) {
  40. f>>x; blackDist[x] = 0;
  41. blackQueue.push(x);
  42. }
  43.  
  44. for (int i=1; i<=n; ++i) {
  45. f>>cnt;
  46. for (int j=1; j<=cnt; ++j) {
  47. f>>x;
  48. v[i].push_back(x);
  49. vt[x].push_back(i);
  50. }
  51. }
  52.  
  53. rootDist[1] = 0;
  54. rootQueue.push(1);
  55.  
  56. BFS(redQueue, redDist, vt);
  57. BFS(blackQueue, blackDist, vt);
  58. BFS(rootQueue, rootDist, v);
  59.  
  60. // calculam solutia
  61. int minn = INF;
  62. for (int i=1; i<=n; ++i) {
  63. minn = min(minn, redDist[i] + blackDist[i] + rootDist[i]);
  64. }
  65. if (minn == INF) g<<"impossible\n";
  66. else g<<minn<<"\n";
  67.  
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement