Advertisement
Saleh127

UVA 10505 / Bipartite Matching

Jun 13th, 2022
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. /***
  2.  created: 2022-06-14-00.09.44
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  13. #define get_lost_idiot return 0
  14. #define nl '\n'
  15.  
  16.  
  17. vector<ll>g[2005];
  18. ll v[2005];
  19.  
  20. ll bipartite(ll in)
  21. {
  22.     ll b=0,w=0;
  23.     bool bicolr=1;
  24.  
  25.     queue<ll>q;
  26.  
  27.     q.push(in);
  28.     v[in]=0;
  29.     b++;
  30.  
  31.     while(q.empty()==false )
  32.     {
  33.         ll u=q.front();
  34.         q.pop();
  35.  
  36.         for(auto d:g[u])
  37.         {
  38.             if(v[d]==v[u])
  39.             {
  40.                 bicolr=0;
  41.             }
  42.             if(v[d]==-1)
  43.             {
  44.                 v[d]=(v[u]+1)%2;
  45.                 if(v[d]==0) b++;
  46.                 else w++;
  47.                 q.push(d);
  48.             }
  49.         }
  50.     }
  51.  
  52.     if(bicolr==0) return 0;
  53.     return max(b,w);
  54.  
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60.     ios_base::sync_with_stdio(0);
  61.     cin.tie(0);
  62.     cout.tie(0);
  63.  
  64.  
  65.     test
  66.     {
  67.         ll n,m,i,j,k,l;
  68.  
  69.         cin>>n;
  70.  
  71.         for(i=0;i<n+2;i++)
  72.         {
  73.             g[i].clear();
  74.             v[i]=-1;
  75.         }
  76.  
  77.         for(i=1;i<=n;i++)
  78.         {
  79.             cin>>m;
  80.             for(j=1;j<=m;j++)
  81.             {
  82.                 cin>>k;
  83.                 g[i].push_back(k);
  84.                 g[k].push_back(i);
  85.             }
  86.         }
  87.  
  88.         l=0;
  89.  
  90.         for(i=1;i<=n;i++)
  91.         {
  92.             if(v[i]==-1)
  93.             {
  94.                 l+=bipartite(i);
  95.             }
  96.         }
  97.  
  98.         cout<<l<<nl;
  99.     }
  100.  
  101.  
  102.     get_lost_idiot;
  103. }
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement