传送门

题目大意:

给定一张无向图,求满足以下条件的点对 (x,y) 数目:对任意点 z (z!=x,y),边 (x,z) 和 (y,z) 同时存在或同时不存在。

题目分析:

首先要分析的出如果xy满足要求,那么x和y的连边点集应该是相同的,这里又分为两种情况:

  • xy之间有边,加上自己的hash值后求集合哈希值出现的次数
  • xy之间没有边,直接hash求不包含自己的集合哈希值出现的次数

bzoj加上srand(time(0))就会RE,有毒

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<ctime>
using namespace std;
typedef long long ll;
namespace IO{
inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0' && ch <= '9'; ch = getchar()) i = (i << 3) + (i << 1) + (ch - '0');
return i * f;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
}using namespace IO; const int N = 1e6 + 5, M = 1e6 + 5;
int n, m;
typedef unsigned long long ull; const int Mod = 233333;
ull val[N], sum[N];
ll ans, degree[N];
typedef pair<ull, ll> P;
typedef pair<int, int> EP;
vector<EP> edges;
vector<P> hashMap[Mod + 5]; inline void insert(ull x, ll c){
int key = x % Mod;
for(int i = 0; i < hashMap[key].size(); i++)
if(hashMap[key][i].first == x) {hashMap[key][i].second+=c;return;}
hashMap[key].push_back(P(x, c));
} inline ll query(ull x){
int key = x % Mod;
for(int i = 0; i < hashMap[key].size(); i++)
if(hashMap[key][i].first == x) return hashMap[key][i].second;
return 0;
} int main(){
n = read(), m = read();
for(int i = 1; i <= n; i++) val[i] = 1ull * rand() * rand() + rand() ;
for(int i = 1; i <= m; i++){
int x = read(), y = read(); edges.push_back(EP(x, y)); degree[x]++, degree[y]++;
sum[x] += val[y], sum[y] += val[x];
} for(int i = 1; i <= n; i++) insert(sum[i],1);
for(int i = 1; i <= n; i++){
ll x = query(sum[i]); insert(sum[i], -x);
ans += x * (x - 1) / 2;
}
for(int i = 0; i < edges.size(); i++){
int x = edges[i].first, y = edges[i].second;
if(degree[x] == degree[y] && sum[x] + val[x] == sum[y] + val[y]) ans++;
}
wr(ans), putchar('\n');
return 0;
}

BZOJ 4264 小c找朋友 - hash的更多相关文章

  1. BZOJ 4264 小C找朋友 哈希+脑子

    好吧我觉得是脑子,别人觉得是套路$qwq$ 这道题相当于是求除了$u,v$两点互相连接,所连的点相同的点对$(u,v)$ 我们首先每个点一个随机权值,对于$u$点记为$w[u]$,然后记与$u$点相连 ...

  2. 【BZOJ4264】小C找朋友 随机化

    [BZOJ4264]小C找朋友 Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不 ...

  3. 刷题总结——小c找朋友(bzoj4264 集合hash)

    题目: Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不同的)小Ci和j,如果其 ...

  4. bzoj4264: 小C找朋友

    hash大法好 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...

  5. 【bzoj4264】小C找朋友

    题解 $a$和$b$是好*友说明除了这两个人以外的邻接集合相同: 做两次$hash$,分别都处理和$a$相邻的点排序$hash$,①$a$要算进$a$的相邻集合,②$a$不算进: 当两个人不是好*友, ...

  6. 「bzoj4264 小C找朋友」

    权限题 就是一个集合\(hash\) 集合\(hash\)可以用于判断两个集合是否相等,具体做法就是给每个随机一个值,之后异或起来就是可以了 这个题就是这样,处理出每个点直接相连的点集的\(hash\ ...

  7. hunnu--11548--找啊找啊找朋友

    找啊找啊找朋友 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 14,  ...

  8. bzoj 3437 小p的农场

    bzoj 3437 小p的农场 思路 \(f[i]=min(f[j]+\sum\limits_{k=j+1}^{i}{b[k]*(i-k)}+a[i])\) \(f[i]=min(f[j]+\sum\ ...

  9. 51nod 1463 找朋友 (扫描线+线段树)

    1463 找朋友  基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 难度:5级算法题  收藏  关注 给定: 两个长度为n的数列A .B 一个有m个元素的集合K 询问Q次 每次询 ...

随机推荐

  1. 在CentOS7 开发与部署 asp.net core app笔记

    原文:在CentOS7 开发与部署 asp.net core app笔记 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/lihongzhai/art ...

  2. 网络博客 VC\图案像处理

    http://blog.csdn.net/lvwx369/article/category/1185452 http://blog.csdn.net/lyy289065406/article/deta ...

  3. 洛古——P1433 吃奶酪

    https://www.luogu.org/problem/show?pid=1433 题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输 ...

  4. 新版本的AutoCAD2018 怎样删除 A360 Drive盘符

    通常的做法,如下: (1)点击开始菜单的“运行”(Win+R或者Win+X快捷选择运行),在弹出的对话框输入“regedit”,回车,进入注册表编辑器. (2)找到HKEY_LOCAL_MACHINE ...

  5. nginx源代码分析--事件模块 &amp; 琐碎

    通过HUP信息使得NGINX实现又一次读取配置文件,使用USR2信号使得NGINX实现平滑升级. 在nginx中有模块这么一说,对外全部的模块都是ngx_module_t类型,这个结构体作为全部模块的 ...

  6. Netty系列之Netty可靠性分析--转载

    原文地址:http://www.infoq.com/cn/articles/netty-reliability 1. 背景 1.1. 宕机的代价 1.1.1. 电信行业 毕马威国际(KPMG Inte ...

  7. 【LCS】POJ1458Common Subsequence

    题目链接:http://poj.org/problem?id=1458 这是一道最长公共子序列的模板题: #include<iostream> #include<string> ...

  8. (转)Windows Server 2008 R2 域控制器部署指南

    转自:https://technet.microsoft.com/zh-cn/cloud/gg462955.aspx 一.域控制器安装步骤: 1.装 Windows Server 2008 R2并配置 ...

  9. hcharts实现堆叠柱形图

    <!DOCTYPE > <html> <head> <meta charset="utf-8"><link rel=" ...

  10. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...