zoukankan      html  css  js  c++  java
  • [51nod1676]无向图同构

      如果一个无向图重标号后与另一个无向图完全一致(即对于任意两点,他们之间的边在两个图中都存在或都不存在),则称两个无向图同构。
      给定两个n个点m条边的无向图,判定两个无向图是否同构。不超过20组数据,n<=200,m<=4000

    题解:初始时设每个点为点权为1,之后进行n次迭代,每次迭代每个点的值更替为与其相邻的点和他本身上一次迭代后的权值排序后计算出的hash值。

      只要hash值相等就好了。。权值排序那部分不用在算每个点的时候都重新排序。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<queue>
     6 #define ll long long
     7 #define ui unsigned int
     8 #define ull unsigned long long
     9 const int maxn=233,inf=1002333333;
    10 struct zs1{int too,pre;}e1[23333],e2[23333];int tot1,last1[maxn],tot2,last2[maxn];
    11 struct zs{int id;ull v;}a1[2][maxn],a2[2][maxn];
    12 int p1[maxn],p2[maxn];
    13 int i,j,k,n,m;
    14 
    15 int ra,fh;char rx;
    16 inline int read(){
    17     rx=getchar(),ra=0,fh=1;
    18     while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();
    19     if(rx=='-')fh=-1,rx=getchar();
    20     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;
    21 }
    22 
    23 inline void ins1(int a,int b){
    24     e1[++tot1].too=b,e1[tot1].pre=last1[a],last1[a]=tot1;
    25     e1[++tot1].too=a,e1[tot1].pre=last1[b],last1[b]=tot1; 
    26 }
    27 inline void ins2(int a,int b){
    28     e2[++tot2].too=b,e2[tot2].pre=last2[a],last2[a]=tot2;
    29     e2[++tot2].too=a,e2[tot2].pre=last2[b],last2[b]=tot2; 
    30 }
    31 
    32 bool operator <(zs a,zs b){return a.v<b.v;}
    33 int main(){
    34     register int i,j;ull k;
    35     for(int T=read();T;T--){
    36         tot1=tot2=0,memset(last1+1,0,n<<2);memset(last2+1,0,n<<2);
    37         n=read(),m=read();
    38         for(i=1;i<=m;i++)ins1(read(),read());
    39         for(i=1;i<=m;i++)ins2(read(),read());
    40         for(i=1;i<=n;i++)a1[0][i]=a2[0][i]=(zs){i,1ull},ins1(i,i),ins2(i,i),a1[1][i].id=a2[1][i].id=p1[i]=p2[i]=i;
    41         bool now=1,pre=0;int p;
    42         for(p=n;p;p--,now^=1,pre^=1){//printf("now,pre:%d %d
    ",now,pre);
    43             for(i=1;i<=n;i++)a1[now][i].v=a2[now][i].v=0;//,printf("    %llu %llu
    ",a1[pre][i].v,a2[pre][i].v);
    44             for(i=1;i<=n;i++)p1[a1[now][i].id]=p2[a2[now][i].id]=i;
    45             for(i=1;i<=n;i++){
    46                 for(j=last1[a1[pre][i].id],k=a1[pre][i].v/*,printf("   %llu
    ",k)*/;j;j=e1[j].pre)
    47                     (a1[now][p1[e1[j].too]].v*=2333ull)+=k;
    48                 for(j=last2[a2[pre][i].id],k=a2[pre][i].v;j;j=e2[j].pre)
    49                     (a2[now][p2[e2[j].too]].v*=2333ull)+=k;
    50             }
    51             std::sort(a1[now]+1,a1[now]+1+n),
    52             std::sort(a2[now]+1,a2[now]+1+n);
    53             for(i=1;i<=n&&a1[now][i].v==a2[now][i].v;i++);//printf("        %llu %llu
    ",a1[now][i].v,a2[now][i].v);
    54             if(i<=n)break;
    55         }puts(!p?"YES":"NO");
    56     }
    57 }
    View Code
  • 相关阅读:
    用PHP如何打造一个高可用高性能的网站
    php 数据批量插入mysql和mysql类
    PHP8新特性
    php 爬取抖音评论数据
    Python学习笔记之7.5
    mysql基本概念
    开发google插件
    php curl 重定向 cookie问题
    git 入门
    git对已经提交过的文件添加到.gitignore
  • 原文地址:https://www.cnblogs.com/czllgzmzl/p/5950970.html
Copyright © 2011-2022 走看看