Advertisement
a53

SaseG

a53
Dec 25th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. // Costin Oncescu
  2. #include<bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. const int maxN = 1e6 + 100, maxM = maxN;
  7. int N, M, type, a[maxN], b[maxN], x[maxM], y[maxM];
  8. bool isNecessary[maxN];
  9. typedef pair < int, int > atMost2;
  10. atMost2 whereFrom[maxN];
  11.  
  12. atMost2 merge (atMost2 a, atMost2 b) {
  13. if (a.first == 0) return b;
  14. if (b.first == 0) return a;
  15. if (b.second != 0) return b;
  16. if (a.second == 0 && b.first != a.first)
  17. a.second = b.first;
  18. return a;
  19. }
  20.  
  21. int main () {
  22. scanf ("%d\n%d %d", &type, &N, &M);
  23. for (int i=1; i<=N; i++)
  24. scanf ("%d", &b[i]), a[i] = b[i];
  25. for (int i=1; i<=M; i++)
  26. scanf ("%d %d", &x[i], &y[i]);
  27. for (int i=M; i>=1; i--)
  28. if (!(a[x[i]] & a[y[i]]))
  29. a[x[i]] = a[y[i]] = 0;
  30. if (type == 1) {
  31. for (int i=1; i<=N; i++)
  32. printf ("%d", a[i]);
  33. printf ("\n");
  34. return 0;
  35. }
  36. for (int i=1; i<=N; i++)
  37. if (a[i] != 0)
  38. whereFrom[i].first = i;
  39. for (int i=1; i<=M; i++)
  40. whereFrom[x[i]] = whereFrom[y[i]] = merge (whereFrom[x[i]], whereFrom[y[i]]);
  41. for (int i=1; i<=N; i++) {
  42. assert (b[i] == 0 || whereFrom[i].first != 0);
  43. if (b[i] == 1 && whereFrom[i].second == 0)
  44. isNecessary[whereFrom[i].first] = 1;
  45. }
  46. for (int i=1; i<=N; i++)
  47. printf ("%d", a[i] == 0 || (!isNecessary[i]));
  48. printf ("\n");
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement