Guest User

b.cpp

a guest
Feb 7th, 2026
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define ll long long
  5. #define pb push_back
  6. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  7. #define mk make_pair
  8. using namespace std;
  9.  
  10. const int MAXN = 2000 + 5;
  11. const int INF = 1e9 + 7;
  12.  
  13. vector <int> vec1, vec2, g[MAXN];
  14. bool was[MAXN];
  15. int mt[MAXN];
  16. bool khun(int x){
  17.     if (was[x])
  18.         return false;
  19.     was[x] = true;
  20.  
  21.     for (int i = 0; i < g[x].size(); i++){
  22.         int to = g[x][i];
  23.         if (mt[to] == -1 || khun(mt[to])){
  24.             mt[to] = x;
  25.             return true;
  26.         }
  27.     }
  28.     return false;
  29.  
  30. }
  31. int was2[MAXN];
  32. void dfs(int x, int y){
  33.     was2[x] = y;
  34.     if (y == 1){
  35.         vec1.pb(x);
  36.     }
  37.     else {
  38.         vec2.pb(x);
  39.     }
  40.  
  41.     for (int i = 0; i < g[x].size(); i++){
  42.         if (!was2[g[x][i]]){
  43.             dfs(g[x][i], 3 - y);
  44.         }
  45.         else if (was2[g[x][i]] == y){
  46.             cout << "NO\n";
  47.             exit(0);
  48.         }
  49.     }
  50. }
  51.  
  52.  
  53. int main() {
  54.     IOS;
  55.     int n, m;
  56.     cin >> n >> m;
  57.     for (int i = 0; i < m; i++){
  58.         int x, y;
  59.         cin >> x >> y;
  60.         g[x].pb(y);
  61.         g[y].pb(x);
  62.     }
  63.     for (int i = 1; i <= n; i++){
  64.         if (!was2[i])
  65.             dfs(i, 1);
  66.     }
  67.  
  68.     for (int i = 0; i < vec2.size(); i++){
  69.         mt[vec2[i]] = -1;
  70.     }
  71.  
  72.     for (int i = 0; i < vec1.size(); i++){
  73.         for (int j = 0; j < vec1.size(); j++){
  74.             was[vec1[j]] = 0;
  75.         }
  76.         khun(vec1[i]);
  77.     }
  78.     int ans = 0;
  79.     for (int i = 0; i < vec2.size(); i++){
  80.         if (mt[vec2[i]] != -1){
  81.             ans++;
  82.             //cout << vec2[i] << ' ' << mt[vec2[i]] << '\n';
  83.         }
  84.     }
  85.     cout << ans;
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment