当前位置:网站首页>[51Nod1676 无向图同构]无向图哈希[通俗易懂]
[51Nod1676 无向图同构]无向图哈希[通俗易懂]
2022-07-25 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
[51Nod1676 无向图同构]无向图哈希
分类:Data StructureHash
1. 题目链接
2. 题意描述
3. 解题思路
对某一个东西进行哈希,一般就选取一些特征点,然后尽可能离散化这些特征点。对于无向图中的每一个联通块来说,他的特征点就是顶点的度。显然这样还不够,那么可以加入深度这个特征,只需要对联通块的每一个顶点bfs求一边单源点最短路。 利用这两个特征点,然后随意构造哈希函数(xjb搞)就可以了。不过代码写得并不优美
4. 实现代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> puu;
typedef pair<lb, lb> pbb;
const int inf = 0x3f3f3f3f;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
template<typename T> inline void umax(T &a, T b) { a = max(a, b); }
template<typename T> inline void umin(T &a, T b) { a = min(a, b); }
template<typename T> inline T randIntv(const T& a, const T& b) { return (T)rand() % (b - a + 1) + a; }
void debug() { cout << endl; }
template<typename T, typename ...R> void debug (T f, R ...r) { cout << "[" << f << "]"; debug (r...); }
const int MAXN = 500;
const int BASE1 = 13331;
const int BASE2 = 100007;
vector<int> G1[MAXN], G2[MAXN];
int dep[MAXN], du1[MAXN], du2[MAXN];
ull qz1[100000], qz2[100000];
ull bfs(int s, vector<int> G[], int du[]) {
memset(dep, -1, sizeof(dep));
ull ret1 = 0, ret2 = 0;
queue<int> q;
q.push(s);
dep[s] = 0;
while (!q.empty()) {
int u = q.front(); q.pop();
ret1 += qz1[du[u]];
ret2 += qz2[dep[u]];
for (auto& v : G[u]) {
ret1 += (qz1[du[u]] * qz1[du[v]]);
ret2 += (qz2[dep[u]] * qz2[dep[v]]);// ^ (qz1[du[u]] * qz1[du[v]]); // * qz2[dep[u]] * qz2[dep[v]];
if (dep[v] != -1) continue;
dep[v] = dep[u] + 1;
q.push(v);
}
}
return ret1 ^ ret2;
}
bool used[MAXN];
vector<int> path;
void dfs(int u, vector<int> G[]) {
if (used[u]) return;
used[u] = true;
path.push_back(u);
for (auto& v : G[u]) {
dfs(v, G);
}
}
void hashV(int n, vector<int> G[], int du[], vector<ull>& ret) {
memset(used, false, sizeof(used));
ret.clear();
for (int i = 1; i <= n; ++i) {
if (used[i]) continue;
path.clear();
dfs(i, G);
ull temp = 0;
for (auto& u : path) temp += bfs(u, G, du);
ret.push_back(temp * qz2[path.size()]);
}
sort(ret.begin(), ret.end());
}
int main() {
#ifdef ___LOCAL_WONZY___
freopen("input.txt", "r", stdin);
#endif // ___LOCAL_WONZY___
qz1[0] = qz2[0] = 1;
for (int i = 1; i < 100000; ++i) {
qz1[i] = qz1[i - 1] * BASE1;
qz2[i] = qz2[i - 1] * BASE2;
}
int T, n, m, u, v;
cin >> T;
while (T --) {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
G1[i].clear();
G2[i].clear();
du1[i] = du2[i] = 0;
dep[i] = -1;
}
for (int i = 1; i <= m; ++i) {
cin >> u >> v;
G1[u].push_back(v);
G1[v].push_back(u);
++ du1[u]; ++ du1[v];
}
for (int i = 1; i <= m; ++i) {
cin >> u >> v;
G2[u].push_back(v);
G2[v].push_back(u);
++ du2[u]; ++ du2[v];
}
vector<ull> v1, v2;
hashV(n, G1, du1, v1);
hashV(n, G2, du2, v2);
bool ans = equal(v1.begin(), v1.end(), v2.begin());
cout << (ans ? "YES" : "NO") << endl;
}
#ifdef ___LOCAL_WONZY___
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << "ms." << endl;
#endif // ___LOCAL_WONZY___
return 0;
}发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127907.html原文链接:https://javaforall.cn
边栏推荐
- Isn't it too much to play Gobang in idea?
- My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000
- Idea resolves the prompt of profile properties disappear
- Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
- 五、品达通用权限系统__pd-tools-xxs(防跨站脚本攻击)
- C#Socket
- Record the transfer of domain names from Alibaba cloud service providers to Huawei cloud
- cuda_ error_ out_ of_ Memory (out of memory)
- Autojs learning - realize 3D perspective
- 【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
猜你喜欢
![PHP zero time task, PHP laravel time task schedule [dry goods]](/img/09/c9a4c83fe23c852aa76a6f5a6cea52.png)
PHP zero time task, PHP laravel time task schedule [dry goods]

IJCAI2022开会了! 微软等《领域泛化Domain Generalization》教程

Lichuang EDA -- creation of devices 01 resistance (II)

Huawei occupies half of the folding mobile phone market, proving its irreplaceable position in the high-end market

Ijcai2022 meeting! Microsoft and other tutorials on domain generalization

Airtest solves the problem that a password needs to be entered in the process of "automatic packaging" (the same applies to random bullet frame processing)
![[redis underlying parsing] string type](/img/a6/47083b033125195ebaf80090919fe2.png)
[redis underlying parsing] string type

Oxford University: many common insomnia drugs lack long-term safety data

五、品达通用权限系统__pd-tools-xxs(防跨站脚本攻击)

Zero basic learning canoe panel (17) -- panel CAPL function
随机推荐
Dear bosses, how can I print the result of Flink SQL to the console and display it completely?
选择的能力
My heart's broken! After being cheated by 30000, a 16-year-old girl was unconvinced and cheated by 50000
C#Socket
Detailed explanation of several ideas for implementing timed tasks in PHP
How to configure and use rocksdb in the flinksql environment
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
SSH private key realizes login to remote target server
All non isomorphic subgraphs of a directed complete graph of order 3 (number of different hook graphs)
Vivo official website app full model UI adaptation scheme
FAW red flag "King fried" is listed, which is safe and comfortable
919. Complete binary tree inserter: simple BFS application problem
Six principles of C program design
【饭谈】测试平台为什么有组件化?模块化?很多看不到的地方设计的很好是种浪费么?
Apache Shenyu admin authentication bypass vulnerability (cve-2021-37580) analysis and protection measures
Autojs learning - Automatic screenshot of the king
Composition of dog food
Detailed explanation of Ag search tool parameters
零基础学习CANoe Panel(17)—— Panel CAPL Function
How to choose sentinel vs. hystrix current limiting?