ghorardim

11331 - The Joys of Farming

Feb 9th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<ctype.h>
  5. #include<stdlib.h>
  6. #include<cstdio>
  7. #include<cstring>
  8. #include<string>
  9. #include<cmath>
  10. #include<iostream>
  11. #include<cctype>
  12. #include<map>
  13. #include<stack>
  14. #include<cstdlib>
  15. #include<queue>
  16. #include<vector>
  17. #include<algorithm>
  18. #include<bitset>
  19. #include<algorithm>
  20. #include<set>
  21. #include<limits.h>
  22.  
  23. using namespace std;
  24. typedef long int LD;
  25. typedef long long int LLD;
  26. typedef float F;
  27. typedef double LF;
  28. typedef unsigned int U;
  29. typedef unsigned long int LU;
  30. typedef unsigned long long int LLU;
  31. typedef char C;
  32.  
  33.  
  34. /*----------simple input section ------------ */
  35. #define sf scanf
  36. #define sfi(x) scanf("%d",&x)
  37. #define sfc(x) scanf("%c",&x)
  38. #define sfi2(x,y) scanf("%d%d",&x,&y)
  39. #define sfll2(x,y) scanf("%lld%lld",&x,&y)
  40. #define sfu2(x,y) scanf("%llu%llu",&x,&y)
  41. #define sfl(x) scanf("%ld",&x)
  42. #define sfll(x) scanf("%lld",&x)
  43. #define sfd(x) scanf("%lf",&x)
  44. #define sfu(x) scanf("%llu",&x)
  45. #define sfs(x) scanf("%s",x)
  46.  
  47.  
  48. /*----------simple input section ------------ */
  49. #define pf printf
  50. #define pfi(x) printf("%d\n",x)
  51. #define pfl(x) printf("%ld\n",x)
  52. #define pfll(x) printf("%lld\n",x)
  53. #define pfu(x) printf("%llu\n",x)
  54. #define pfs(x) printf("%s\n",x)
  55. #define pfc(x) printf("%c\n",x)
  56. #define pfd(x) printf("%lf\n",x)
  57.  
  58. /*----------file input section ------------ */
  59.  
  60. #define f_input  freopen("input.txt","r",stdin);
  61. #define f_output freopen("output.txt","w",stdout);
  62.  
  63. #define pb(x) push_back(x)
  64. #define PI acos(-1.0)
  65. #define sq(x) (x)*(x)
  66. #define mem(x,y) memset(x,y,sizeof(x))
  67. #define nn printf("\n")
  68. // xx-> diagonal -> 8 horizontal/vertical->4
  69.  
  70. const int xx[] = {0, 1, 0, -1, -1, 1, -1, 1};
  71. const int yy[] = {1, 0, -1, 0, 1, 1, -1, -1};
  72.  
  73. // KX-> Knight moves
  74. const int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
  75. const int ky[] = {1, 2, 2, 1, -1, -2, -2, -1};
  76.  
  77.  
  78. /******** debug **********/
  79. #define chk1 printf("hi......1  \n")
  80. #define chk2 printf("hi......2  \n")
  81.  
  82.  
  83.  
  84.  
  85. #define mod 1000000007
  86. #define eps 10e-8
  87. #define sz 2005
  88. #define sz1 302
  89. #define sz2 5
  90. /******* start my code ********/
  91. int vis[sz];
  92. int vis1[sz];
  93. vector<int>node[sz];
  94. vector<pair<int,int> >v;
  95. vector<pair<int,int> >stor;
  96. set<int>s;
  97. queue<int>q;
  98. void knap(int fst,int snd,int i,int n)
  99. {
  100.     if(i==n)
  101.     {
  102.         pair<int,int>e;
  103.         e.first=e.second=0;
  104.         e.first=fst;
  105.         e.second=snd;
  106.         stor.pb(e);
  107.         return;
  108.     }
  109.     knap(fst+v[i].first,snd+v[i].second,i+1,n);
  110.     knap(fst+v[i].second,snd+v[i].first,i+1,n);
  111. }
  112. int main()
  113. {
  114.     int i,j,k,b,c,n,x,y,u,l,z;
  115.     LLD T;
  116.     pair<int,int>p;
  117.     bool key;
  118.     sfll(T);
  119.     while(T--)
  120.     {
  121.         mem(vis,0);
  122.         sfi2(b,c);
  123.         sfi(n);
  124.         key=1;
  125.         for(i=0; i<n; i++)
  126.         {
  127.             sfi2(x,y);
  128.             node[x].pb(y);
  129.             node[y].pb(x);
  130.             s.insert(x);
  131.             s.insert(y);
  132.         }
  133.         set<int> :: iterator it;
  134.         for(it=s.begin(); it!=s.end(); it++)
  135.         {
  136.             u=*it;
  137.             if(vis[u]) continue;
  138.             vis[u]=1;
  139.             q.push(u);
  140.             p.first=1;
  141.             p.second=0;
  142.             while(!q.empty())
  143.             {
  144.                 u=q.front();
  145.                 q.pop();
  146.                 l=node[u].size();
  147.                 for(i=0; i<l; i++)
  148.                 {
  149.                     if(vis[node[u][i]]==vis[u])
  150.                     {
  151.                         key=0;
  152.                         break;
  153.                     }
  154.                     if(!vis[node[u][i]])
  155.                     {
  156.                         if(vis[u]==1)
  157.                         {
  158.                             vis[node[u][i]]=2;
  159.                             p.second++;
  160.                         }
  161.                         else
  162.                         {
  163.                             vis[node[u][i]]=1;
  164.                             p.first++;
  165.                         }
  166.                         q.push(node[u][i]);
  167.                     }
  168.                 }
  169.             }
  170.             v.pb(p);
  171.             if(!key) break;
  172.         }
  173.         if(key)
  174.         {
  175.             key=0;
  176.             x=y=0;
  177.             for(i=1; i<=b+c; i++)
  178.                 if(vis[i]==0) x++;
  179.             knap(0,0,0,v.size());
  180.             int l=stor.size();
  181.             for(i=0;i<l;i++)
  182.             {
  183.                 if(x)
  184.                 {
  185.                     if((b>=stor[i].first && c>=stor[i].second)&& (b-stor[i].first)+(c-stor[i].second)==x)
  186.                     {
  187.                         key=1;
  188.                         break;
  189.                     }
  190.                 }
  191.                 else
  192.                 {
  193.                         if(stor[i].first==b && (stor[i].second==c))
  194.                         {
  195.                             key=1;
  196.                             break;
  197.                         }
  198.                 }
  199.                 if(key) break;
  200.             }
  201.             if(key) pfs("yes");
  202.             else pfs("no");
  203.         }
  204.         else
  205.             pf("no\n");
  206.         s.clear();
  207.         for(i=1; i<=b+c; i++)
  208.             node[i].clear();
  209.         stor.clear();
  210.         v.clear();
  211.     }
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment