Poj 1021

传送门:

图论 讨论图的同构

主要是计算图的哈希,当两个图的哈希相同时认为图同构。

关于哈希,网上有各种版本,各个点到与他连通的点的距离的平方,各个点向四个方向可以走的步数(旁边有点则可以走),同一连通图中所有点的距离之和

这里采用比较简单的第二种,不过暂时还不知道证明方法

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 struct pos{
 7     int x;
 8     int y;
 9 };
10 
11 int T;
12 int w,h,n;
13 int map[105][105];
14 int hash[2][10005];
15 pos p[10005];
16 bool flag;
17 
18 int main(){
19     cin>>T;
20     while(T--){
21         cin>>w>>h>>n;
22         memset(hash,0,sizeof(hash));
23         memset(map,0,sizeof(map));
24         for(int i=0;i<n;i++){
25             cin>>p[i].x>>p[i].y;
26             map[p[i].x][p[i].y]=1;
27         }
28         for(int i=0;i<n;i++){
29             int xx=p[i].x;
30             int yy=p[i].y;
31             for(int x=xx+1;x<w&&map[x][yy];x++){hash[0][i]++;}
32             for(int x=xx-1;x>=0&&map[x][yy];x--){hash[0][i]++;}
33             for(int y=yy+1;y<h&&map[xx][y];y++){hash[0][i]++;}
34             for(int y=yy-1;y>=0&&map[xx][y];y--){hash[0][i]++;}
35         }
36         memset(map,0,sizeof(map));
37         for(int i=0;i<n;i++){
38             cin>>p[i].x>>p[i].y;
39             map[p[i].x][p[i].y]=1;
40         }
41         for(int i=0;i<n;i++){
42             int xx=p[i].x;
43             int yy=p[i].y;
44             for(int x=xx+1;x<w&&map[x][yy];x++){hash[1][i]++;}
45             for(int x=xx-1;x>=0&&map[x][yy];x--){hash[1][i]++;}
46             for(int y=yy+1;y<h&&map[xx][y];y++){hash[1][i]++;}
47             for(int y=yy-1;y>=0&&map[xx][y];y--){hash[1][i]++;}
48         }
49         sort(hash[0],hash[0]+n);
50         sort(hash[1],hash[1]+n);
51         flag=true;
52         for(int i=0;i<n;i++){
53             if(hash[0][i]!=hash[1][i]){
54                 flag=false;
55                 break;
56             }
57         }
58         if(flag){
59             cout<<"YES"<<endl;
60         }
61         else{
62             cout<<"NO"<<endl;
63         }
64     } 
65 } 

 

转载于:https://www.cnblogs.com/marlenemizuno/p/6918791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值