BZOJ 4264 小c找朋友 - hash
题目大意:
给定一张无向图,求满足以下条件的点对 (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的更多相关文章
- BZOJ 4264 小C找朋友 哈希+脑子
好吧我觉得是脑子,别人觉得是套路$qwq$ 这道题相当于是求除了$u,v$两点互相连接,所连的点相同的点对$(u,v)$ 我们首先每个点一个随机权值,对于$u$点记为$w[u]$,然后记与$u$点相连 ...
- 【BZOJ4264】小C找朋友 随机化
[BZOJ4264]小C找朋友 Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不 ...
- 刷题总结——小c找朋友(bzoj4264 集合hash)
题目: Description 幼儿园里有N个小C,两个小C之间可能是朋友也可能不是.所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边. 园长ATM发现对于两个(不同的)小Ci和j,如果其 ...
- bzoj4264: 小C找朋友
hash大法好 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- 【bzoj4264】小C找朋友
题解 $a$和$b$是好*友说明除了这两个人以外的邻接集合相同: 做两次$hash$,分别都处理和$a$相邻的点排序$hash$,①$a$要算进$a$的相邻集合,②$a$不算进: 当两个人不是好*友, ...
- 「bzoj4264 小C找朋友」
权限题 就是一个集合\(hash\) 集合\(hash\)可以用于判断两个集合是否相等,具体做法就是给每个随机一个值,之后异或起来就是可以了 这个题就是这样,处理出每个点直接相连的点集的\(hash\ ...
- hunnu--11548--找啊找啊找朋友
找啊找啊找朋友 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 14, ...
- 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\ ...
- 51nod 1463 找朋友 (扫描线+线段树)
1463 找朋友 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 关注 给定: 两个长度为n的数列A .B 一个有m个元素的集合K 询问Q次 每次询 ...
随机推荐
- C语言库函数,头文件
参看:https://zhidao.baidu.com/question/328173842.html 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五种类型.一些宏和通用工具函数. ...
- PatentTips - Apparatus and method for a generic, extensible and efficient data manager for virtual peripheral component interconnect devices (VPCIDs)
BACKGROUND A single physical platform may be segregated into a plurality of virtual networks. Here, ...
- iOS8: 企业开发的终结?
iOS 8 的公布(如今是 iOS8.1),并非对全部人来说都是值得高兴的事情. 对那些使用企业部署(不经过商店公布)的 app 开发人员来说,又被苹果坑到了. 由于 iOS 8 的一个Bug.导致企 ...
- Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)
题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...
- JS学习笔记 - 面向对象 - 原型
<script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...
- APP功能点测试
一.移动互联网特点: 1,用户体验至上:精准的用户体验 2,核心竞争力:市场占有率和业务创新能力 3,营销模型:通过口碑传播吸引客户,随之参与互动(如分享等,对接口测试要求高) 二.项目特点: 1,开 ...
- Java反射学习总结二(用反射调用对象的私有属性和方法)
大家都知道正常的调用是不可以访问对象的private修饰的属性和方法的,这也是Java的封装性原则. 但是有没有方法可以强制去访问对象的private修饰的属性和方法呢?那就是用反射!(这个可能在面试 ...
- oracle数据库的备份与恢复
一.备份 方法1: PLSQL中进行导出 对于方式1: 对于导出可执行文件的选择,可通过下面的几个参考位置去查找: 导入imp:F:\app\Administrator\product\11.1 ...
- Android Studio2.0 Beta 2版本号更新说明及注意事项
我们刚刚向canary channel推送了Android Studio2.0 Beta 2版本号 老毕译注: ---------- canary channel: 金丝雀版本号,平均1到2周就会更新 ...
- MyCAT分表初体验
1.mycat二进制包安装 下载地址:http://dl.mycat.io/ tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar ...