Advertisement
Guest User

Hopcroft

a guest
Aug 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.70 KB | None | 0 0
  1. //==================================================================//
  2. // Name        : flash7even                                         //
  3. // Author      : Tarango Khan                                       //
  4. // Codeforces  : flash_7                                            //
  5. // Topcoder    : flash_7                                            //
  6. // Hackerrank  : flash_7                                            //
  7. // Email       : tarangokhan77@gmail.com                            //
  8. // Facebook    : flash7even                                         //
  9. //==================================================================//
  10.  
  11. //==================================================================//
  12. #include <bits/stdc++.h>                                            //
  13. using namespace std;                                                //
  14. #define read(nm)        freopen(nm, "r", stdin)                     //
  15. #define write(nm)       freopen(nm, "w", stdout)                    //
  16. #define pb              push_back                                   //
  17. #define MP              make_pair                                   //
  18. #define ff              first                                       //
  19. #define ss              second                                      //
  20. #define FABS(x)         ((x)+eps<0?-(x):(x))                        //
  21. #define POPCOUNT        __builtin_popcountll                        //
  22. #define RIGHTMOST       __builtin_ctzll                             //
  23. #define LEFTMOST(x)     (63-__builtin_clzll((x)))                   //
  24. #define NUMDIGIT(x,y)   (((int)(log10((x))/log10((y))))+1)          //
  25. #define SQ(x)           ((x)*(x))                                   //
  26. #define pf              printf                                      //
  27. #define sf              scanf                                       //
  28. #define phl             printf("hello\n")                           //
  29. #define SZ(x)           ((int)(x).size())                           //
  30. #define mems(x,v)       memset(x,v,sizeof(x))                       //
  31. #define CLR(x,y)        memset(x,y,sizeof(x))                       //
  32. #define ALL(x)          (x).begin(),(x).end()                       //
  33. #define NORM(x)         if(x>=mod)x-=mod;                           //
  34. #define MOD(x,y)        (((x)*(y))%mod)                             //
  35. #define fills(v,n)      fill(v.begin(), v.end(), n)                 //
  36. #define LL              long long                                   //
  37. #define LLU             long long unsigned int                      //
  38. #define vlong           long long                                   //
  39. #define uvlong          unsigned long long                          //
  40. #define debug1(v1)      cout<<"1@ Debug Val 1 = "<<v1<<endl;        //
  41. #define debug2(v2)      cout<<"   2@ Debug Num 2 = "<<v2<<endl;     //
  42. #define debug3(v3)      cout<<"      3@ Debug Res 3 = "<<v3<<endl;  //
  43. #define UB(v,a)         upper_bound(v.begin(),v.end(),a)-v.begin()  //
  44. #define LB(v,a)         lower_bound(v.begin(),v.end(),a)-v.begin()  //
  45. #define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL)     //
  46. //==================================================================//
  47.  
  48. //==================================================================//
  49. void make_unique(vector<int> &a){ sort(a.begin(), a.end());         //
  50.      a.erase(unique(a.begin(), a.end()), a.end()); }                //
  51. void printDouble(double f,int p){ cout << fixed;                    //
  52.      cout << setprecision(p) << f <<endl; }                         //
  53. int  Set(int N,int cur){ return N = N | (1<<cur); }                 //
  54. int  Reset(int N,int cur){ return N = N & ~(1<<cur); }              //
  55. bool Check(int N,int cur){ return (bool)(N & (1<<cur)); }           //
  56. LL   GCD(LL a,LL b){ if(b == 0) return a; return GCD(b,a%b);}       //
  57. LL   LCM(LL a,LL b){ return a*b/GCD(a,b);}                          //
  58. LL   POW(LL a,LL p){ LL res = 1,x = a;while(p){if(p&1)              //
  59.      res = (res*x); x = (x*x);p >>= 1;} return res;}                //
  60. //==================================================================//
  61.  
  62. //==================================================================//
  63. //int knightDir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},                 //
  64. //                      {2,-1},{-1,-2},{1,-2},{-2,-1}};             //
  65. //int dir8[8][2]      = {{-1,0},{0,1},{1,0},{0,-1},                 //
  66. //                      {-1,-1},{1,1},{1,-1},{-1,1}};               //
  67. //int dir4[4][2]      = {{-1,0},{0,1},{1,0},{0,-1}};                //
  68. //==================================================================//
  69.  
  70. #ifdef forthright48
  71.      #include <ctime>
  72.      clock_t tStart = clock();
  73.      #define debug(args...) {dbg,args; cerr<<endl;}
  74.      #define timeStamp printf("Exc Time: %.2fs\n",(double)(clock()-tStart)/CLOCKS_PER_SEC)
  75. #else
  76.      #define debug(args...)
  77.      #define timeStamp
  78. #endif
  79.  
  80. struct debugger{
  81.     template<typename T> debugger& operator , (const T& v){
  82.         cerr << v << " ";
  83.         return *this;
  84.     }
  85. }dbg;
  86.  
  87. typedef pair <LL,LL> pll;
  88. typedef vector<pll> vll;
  89. typedef vector<LL> vl;
  90.  
  91. const LL inf = 2147383647;
  92. const LL MOD = 1000000007;
  93. const double pi = 2*acos(0.0);
  94. const double eps = 1e-9;
  95.  
  96. //=======// Done With The Shortcut Stuffs! Now Let's Code! //=======//
  97.  
  98.  
  99. #define INF 100000000
  100. #define MAX 100005
  101.  
  102. int N, M, E, R, C;
  103. // N = Maximum number of matching possible from left set.
  104.  
  105. vector< int > Graph[MAX];
  106. int Left[MAX];
  107. int Right[MAX];
  108. int dist[MAX];
  109. int lCnt = 0,rCnt = 0;
  110. // lCnt = is max number of elements in left set.
  111. // rCnt = is max number of elements in right set.
  112.  
  113. bool BFS() {
  114.     queue< int > Q;
  115.     for(int i = 1; i<=N; i++) {
  116.         if(Left[i] == 0) {
  117.             dist[i] = 0;
  118.             Q.push(i);
  119.         } else {
  120.             dist[i] = INF;
  121.         }
  122.     }
  123.     dist[0] = INF;
  124.     while(!Q.empty()) {
  125.         int u = Q.front(); Q.pop();
  126.         if(u != 0) {
  127.             int len = Graph[u].size();
  128.             for(int i = 0; i<len; i++) {
  129.                 int v = Graph[u][i];
  130.                 if(dist[Right[v]] == INF) {
  131.                     dist[Right[v]] = dist[u] + 1;
  132.                     Q.push(Right[v]);
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     return (dist[0] != INF);
  138. }
  139.  
  140. bool DFS(int u) {
  141.     if(u != 0) {
  142.         int len = Graph[u].size();
  143.         for(int i = 0; i<len; i++) {
  144.             int v = Graph[u][i];
  145.             if(dist[Right[v]] == dist[u]+1) {
  146.                 if(DFS(Right[v])) {
  147.                     Right[v] = u;
  148.                     Left[u] = v;
  149.                     return true;
  150.                 }
  151.             }
  152.         }
  153.         dist[u] = INF;
  154.         return false;
  155.     }
  156.     return true;
  157. }
  158.  
  159. int hopcroft_karp() {
  160.     int matching = 0;
  161.     mems(Left,0);
  162.     mems(Right,0);
  163.     N = lCnt;
  164.     while(BFS()){
  165.         for(int i = 1; i<=N; i++){
  166.             if(Left[i] == 0 && DFS(i)){
  167.                 matching++;
  168.             }
  169.         }
  170.     }
  171.     return matching;
  172. }
  173.  
  174. int NN,MM;
  175. int A[100005];
  176. int st[100005];
  177. int ed[100005];
  178.  
  179. void build_Graph(){
  180.     for(int i = 1;i<=NN;i++){
  181.         for(int j = 1;j<=MM;j++){
  182.             LL L = 44LL*st[j];
  183.             LL R = 44LL*ed[j];
  184.             LL cur = 28LL*A[i];
  185.             if(cur>=L && cur<=R){
  186.                 Graph[i].pb(j+NN);
  187.                 //pf("Edge %d-%d\n",i,j);
  188.             }
  189.         }
  190.     }
  191. }
  192.  
  193. int main(){
  194.     //fast_cin;
  195.     #ifdef forthright48
  196.     //read("input.txt");
  197.     //write("output.txt");
  198.     #endif //forthright48
  199.  
  200.     int u,v,nCase;
  201.     sf("%d",&nCase);
  202.     for(int cs = 1;cs<=nCase;cs++){
  203.         sf("%d",&NN);
  204.         lCnt = NN+MM+5;
  205.         for(int i = 1;i<=NN;i++){
  206.             sf("%lld",&A[i]);
  207.         }
  208.         for(int i = 0;i<=lCnt;i++){
  209.             Graph[i].clear();
  210.         }
  211.         sf("%d",&MM);
  212.         for(int i = 1;i<=MM;i++){
  213.             sf("%lld %lld",&st[i],&ed[i]);
  214.             if(st[i]>ed[i]){
  215.                 swap(st[i],ed[i]);
  216.             }
  217.         }
  218.         build_Graph();
  219.         int ans = hopcroft_karp();
  220.         printf("%d\n",ans);
  221.     }
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement