[2016-03-18] [POJ] [1733] [Parity Game]

Source: Internet
Author: User

    • Time: 2016-03-18-09:55:52
    • Title Number: [2016-03-18][poj][1733][parity game]
    • Question: Given a number of intervals within the range of the number of the and, ask from which sentence start is wrong
    • Analysis:
      • Take the right and check the set
      • Interval lengths up to 1000000000 it is not possible to create an array directly, but only 5,000 times is found, that is, at most 5000*2 points, discretization can solve the problem.
        • Relation[i] I is the left end of the interval, fa[i] is the right endpoint of the interval, relation[i] maintenance (i,fa[i]) The parity state of the interval, 0 for even, 1 for odd
        • F (A, B) f (b,c) indicates the state of the interval, then f (a,c) = (f (A, B) + f (b,c))%2;
  
 
  1. #include <map>
  2. #include <cstdio>
  3. using namespace std;
  4. typedef long long LL;
  5. #define for ( x y z ) for ( int ( x ) = ( y );( x z x
  6. const int maxn = 5000*2 + 100;
  7. int fa[maxn],relation[maxn];
  8. map<LL ,int > m;
  9. void ini(){
  10. m.clear();
  11. FOR(i,0,maxn) fa[i] = i;
  12. }
  13. int fnd(int x){
  14. if(x == fa[x]) return x;
  15. int tmp = fa[x];
  16. FA [ = FND ( fa [ x
  17. relation[x] = (relation[x] + relation[tmp])&1;
  18. return fa[x];
  19. }
  20. int uni(int x,int y,int type){
  21. int fax = fnd(x),fay = fnd(y);
  22. if(fax == fay) return 0;
  23. fa[fax] = fay;
  24. relation[fax] = (relation[x] + relation[y] + type)&1;
  25. return 1;
  26. }
  27. int main(){
  28. //freopen("in.txt","r",stdin);
  29. //freopen("out.txt","w",stdout);
  30. int len,n,cur = 0,ans = 0,flg = 1;
  31. LL u,v;
  32. char str[10];
  33. scanf("%d%d",&len,&n);
  34. ini();
  35. FOR(i,0,n){
  36. if(flg){
  37. scanf("%I64d%I64d%s",&u,&v,str);
  38. --u;
  39. if(!m.count(u)) u = m[u] = cur++;
  40. else u = m[u];
  41. if(!m.count(v)) v = m[v] = cur++;
  42. else v = m[v];
  43. int type = (str[0] == ‘o‘?1:0);
  44. if(!uni(u,v,type)){
  45. int t = (relation[ u ] + relation[ v ])&1;
  46. if(t != type){
  47. flg = 0;
  48. continue;
  49. }
  50. }
  51. ++ans;
  52. }else scanf("%*I64d%*I64d%*s");
  53. }
  54. printf("%d\n",ans);
  55. return 0;
  56. }


From for notes (Wiz)

[2016-03-18] [POJ] [1733] [Parity Game]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.