zoukankan      html  css  js  c++  java
  • poj 1733Parity game解题报告

    链接:http://poj.org/problem?id=1733

    hash+并查集,和食物链那道题比较相似的确定关系的方法,[a,b]之间的1的个数可以通过[0,a-1],[0,b]来确定,所以,node[i].w存的是[0,w]的情况,因为区间比较大,而且问题的个数很少,所以用hash散射在一个数组上,这几道并查集的题都有些似懂非懂,看来这些好多东西弄到一块的题自己还是做的太少啊

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #define N 50005
     5 int hash[N];
     6 int a[N],b[N],c[N];
     7 int temp[2*N];
     8 struct NODE
     9 {
    10     int f,w;
    11     NODE()
    12     {
    13         f=-1;
    14         w=0;
    15     }
    16 };
    17 int cmp(const void *a,const void *b)
    18 {
    19     return *(int *)a-*(int *)b;
    20 }
    21 NODE node[N];
    22 int k;
    23 void init()
    24 {
    25     int i;
    26     for(i=0;i<N;i++)
    27     node[i]=NODE();
    28 }
    29 int myfind(int x)
    30 {
    31     if(node[x].f==-1)
    32     return x;
    33     int f;
    34     f=myfind(node[x].f);
    35     node[x].w+=node[node[x].f].w;
    36     node[x].w%=2;
    37     node[x].f=f;
    38     return node[x].f;
    39 }
    40 void join(int x,int y,int z)
    41 {
    42     int sx=myfind(x);
    43     int sy=myfind(y);
    44     node[sy].f=sx;
    45     node[sy].w=(z+node[x].w-node[y].w+2)%2;
    46 }
    47 void myhash()
    48 {
    49     int cnt=0,i;
    50     qsort(temp,2*k,sizeof(int),cmp);
    51     hash[temp[0]]=cnt++;
    52     for(i=1;i<2*k;i++)
    53     if(temp[i]!=temp[i-1])
    54     hash[temp[i]]=cnt++;
    55 }
    56 int main()
    57 {
    58     int n;
    59     scanf("%d%d",&n,&k);
    60     int i,j=0;
    61     char str[2][10]={"odd","even"};
    62     char s[10];
    63     init();
    64     for(i=0;i<k;i++)
    65     {
    66         scanf("%d%d%s",&a[i],&b[i],s);
    67         a[i]--;
    68         temp[j++]=a[i];
    69         temp[j++]=b[i];
    70         if(strcmp(s,str[0]))
    71         c[i]=1;
    72         else
    73         c[i]=0;
    74     }
    75     if(k)
    76     myhash();
    77     for(i=0;i<k;i++)
    78     {
    79         a[i]=hash[a[i]];
    80         b[i]=hash[b[i]];
    81         int sa=myfind(a[i]);
    82         int sb=myfind(b[i]);
    83         if(sa!=sb)
    84         join(a[i],b[i],c[i]);
    85         else
    86         {
    87             if((node[a[i]].w-node[b[i]].w+2)%2!=c[i])
    88             break;
    89         }
    90     }
    91     printf("%d\n",i);
    92     return 0;
    93 }
  • 相关阅读:
    pandas DataFrame 数据处理常用操作
    host文件配置 了解
    安装Spring+搭建Spring开发环境
    java 环境配置 maven 环境配置
    Hive SQL 常见问题(转载)
    小结
    SQL 将两个结构相同的表合并到成一个表
    用户画像--初步了解
    hive 提取用户第一次浏览/购买 某商品的 时间
    ES2017 keys,values,entries使用
  • 原文地址:https://www.cnblogs.com/caozhenhai/p/2493585.html
Copyright © 2011-2022 走看看