Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: compounding
- * created: 2024-12-15 16:52:14
- **/
- #include <bits/stdc++.h>
- using namespace std;
- mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
- #define NeedForSpeed \
- ios_base::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<bool> vb;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- #define f first
- #define s second
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define endl "\n"
- const int mod = 1000000007;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- const int N = 2005;
- vi friends[N];
- vi enemies[N];
- vi visf(N, 0);
- vi visitednow(N, 0);
- vi vise(N, 0);
- bool check = true;
- int counts, ans, n;
- void dfs1(int node)
- {
- visf[node] = 1;
- visitednow[node] = 1;
- counts++;
- for (auto child : friends[node])
- {
- if (!visf[child])
- {
- dfs1(child);
- }
- }
- }
- void dfs2(int node, int parent)
- {
- vise[node] = 1;
- // if anynode is and its adj neighbour is visited means they are enemy but have been counted as friend
- if (visitednow[node])
- {
- for (auto &enemy : enemies[node])
- {
- if (visitednow[enemy])
- {
- check = false;
- return;
- }
- }
- }
- for (auto &child : friends[node])
- {
- if (child == parent || vise[child])
- {
- continue;
- }
- dfs2(child, node);
- }
- }
- void solve()
- {
- cin >> n;
- int k; // number of pairs of friends
- cin >> k;
- for (int i = 0; i < k; i++)
- {
- int a, b;
- cin >> a >> b;
- friends[a].push_back(b);
- friends[b].push_back(a);
- }
- int m;
- cin >> m; // number of pairs of enemies
- for (int i = 0; i < m; i++)
- {
- int a, b;
- cin >> a >> b;
- enemies[a].push_back(b);
- enemies[b].push_back(a);
- }
- visf = vi(N, 0);
- ans = 0;
- for (int i = 1; i <= n; i++)
- {
- if (visf[i])
- {
- continue;
- }
- vise = vi(N, 0);
- visitednow = vi(N, 0);
- counts = 0, check = true;
- dfs1(i);
- for (int j = 1; j <= n; j++)
- {
- if (visitednow[j])
- {
- dfs2(j, -1);
- }
- }
- if (check)
- {
- ans = max(ans, counts);
- }
- }
- cout << ans << endl;
- return;
- }
- signed main()
- {
- NeedForSpeed;
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment