Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. void bfs() {
  2. queue<pair<int, int>> q;
  3. forab(i, 1, n + 1) {
  4. d[i] = {N, -1};
  5. if (a[i]) {
  6. q.push({i, a[i]});
  7. d[i] = {0, a[i]};
  8. used[i] = 1;
  9. }
  10. }
  11. while (q.size()) {
  12. int V = q.front().first;
  13. int val = q.front().second;
  14. q.pop();
  15. forn(i, g[V].sz) {
  16. int to = g[V][i];
  17. if (a[to] == 0) {
  18. if (used[to])
  19. continue;
  20. else {
  21. used[to] = 1;
  22. q.push({to, a[to]});
  23. d[to].first = d[V].first + 1;
  24. d[to].second = val;
  25. }
  26. }
  27. else {
  28. if (used[to]) {
  29. if (a[to] != val) {
  30. d[to].first = d[V].first + 1;
  31. d[to].second = val;
  32. }
  33. }
  34. else {
  35. used[to] = 1;
  36. q.push({to, a[to]});
  37. d[to].first = d[V].first + 1;
  38. d[to].second = val;
  39. }
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement