Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define in f
  4. #define out g
  5.  
  6.  
  7. ifstream f ("disjoint.in");
  8. ofstream g ("disjoint.out");
  9.  
  10.  
  11. int n;
  12. int m;
  13. int x;
  14. int y;
  15. int code;
  16. int father[100010];
  17.  
  18.  
  19. int uni (int x, int y) {
  20.  
  21. if(father[y] == 0) {
  22. father[y] = x;
  23. } else {
  24. uni(x, father[y]);
  25. }
  26.  
  27. }
  28.  
  29. int query (int x, int y) {
  30.  
  31. if((father[x] == 0)&&(father[y] == 0)) {
  32. if(x == y) {
  33. out << "DA" << endl;
  34. } else {
  35. out << "NU" << endl;
  36. }
  37. } else {
  38. if((father[x] == 0)&&(father[y] != 0)) {
  39. query(x, father[y]);
  40. } else {
  41. if((father[x] != 0)&&(father[y] == 0)) {
  42. query(father[x], y);
  43. } else {
  44. if((father[x] != 0)&&(father[y] != 0)) {
  45. query(father[x], father[y]);
  46. }
  47. }
  48. }
  49. }
  50.  
  51. }
  52.  
  53. int main() {
  54.  
  55. in >> n;
  56. in >> m;
  57. for(int i = 1; i <= m; i++) {
  58. in >> code;
  59. in >> x;
  60. in >> y;
  61. if(code == 1) {
  62. uni(x, y);
  63. }
  64. if(code == 2) {
  65. query(x, y);
  66. }
  67. }
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement