Beingamanforever

Friends & Enemies, DSU type, CF

Dec 15th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-15 16:52:14
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. const int N = 2005;
  26. vi friends[N];
  27. vi enemies[N];
  28. vi visf(N, 0);
  29. vi visitednow(N, 0);
  30. vi vise(N, 0);
  31. bool check = true;
  32. int counts, ans, n;
  33.  
  34. void dfs1(int node)
  35. {
  36.     visf[node] = 1;
  37.     visitednow[node] = 1;
  38.     counts++;
  39.     for (auto child : friends[node])
  40.     {
  41.         if (!visf[child])
  42.         {
  43.             dfs1(child);
  44.         }
  45.     }
  46. }
  47.  
  48. void dfs2(int node, int parent)
  49. {
  50.     vise[node] = 1;
  51.     // if anynode is and its adj neighbour is visited means they are enemy but have been counted as friend
  52.     if (visitednow[node])
  53.     {
  54.         for (auto &enemy : enemies[node])
  55.         {
  56.             if (visitednow[enemy])
  57.             {
  58.                 check = false;
  59.                 return;
  60.             }
  61.         }
  62.     }
  63.     for (auto &child : friends[node])
  64.     {
  65.         if (child == parent || vise[child])
  66.         {
  67.             continue;
  68.         }
  69.         dfs2(child, node);
  70.     }
  71. }
  72.  
  73. void solve()
  74. {
  75.     cin >> n;
  76.     int k; // number of pairs of friends
  77.     cin >> k;
  78.     for (int i = 0; i < k; i++)
  79.     {
  80.         int a, b;
  81.         cin >> a >> b;
  82.         friends[a].push_back(b);
  83.         friends[b].push_back(a);
  84.     }
  85.     int m;
  86.     cin >> m; // number of pairs of enemies
  87.     for (int i = 0; i < m; i++)
  88.     {
  89.         int a, b;
  90.         cin >> a >> b;
  91.         enemies[a].push_back(b);
  92.         enemies[b].push_back(a);
  93.     }
  94.     visf = vi(N, 0);
  95.     ans = 0;
  96.     for (int i = 1; i <= n; i++)
  97.     {
  98.         if (visf[i])
  99.         {
  100.             continue;
  101.         }
  102.         vise = vi(N, 0);
  103.         visitednow = vi(N, 0);
  104.         counts = 0, check = true;
  105.         dfs1(i);
  106.         for (int j = 1; j <= n; j++)
  107.         {
  108.             if (visitednow[j])
  109.             {
  110.                 dfs2(j, -1);
  111.             }
  112.         }
  113.         if (check)
  114.         {
  115.             ans = max(ans, counts);
  116.         }
  117.     }
  118.     cout << ans << endl;
  119.     return;
  120. }
  121.  
  122. signed main()
  123. {
  124.     NeedForSpeed;
  125.     int t = 1;
  126.     // cin >> t;
  127.     while (t--)
  128.     {
  129.         solve();
  130.     }
  131.     return 0;
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment