传送门

题目大意:

给定一张无向图,求满足以下条件的点对 (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. 【前端图表】echarts实现散点图x轴时间轴

    话不多说,老规矩,先上图,实现echarts实现散点图,x轴数据为时间年月日. 实现代码如下: <!DOCTYPE html> <html> <head> < ...

  2. Mycat 读写分离+分库分表

    上次进过GTID复制的学习记录,已经搭建好了主从复制的服务器,现在利用现有的主从复制环境,加上正在研究的Mycat,实现了主流分布式数据库的测试 Mycat就不用多介绍了,可以实现很多分布式数据库的功 ...

  3. Fragment事务管理源代码分析

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/53132952 本文出自: [HansChen的博客] 概述 在Fragment使用中 ...

  4. 机房收费 &amp; 廊院食堂

    做机房收费系统时.常常想这个一般用户指的是谁?我当初以为是学生......可能是被数据库中的student带跑偏了...... 事实上把我们的系统联系一下实际,就会非常easy想到一般用户指的是谁的位 ...

  5. 多校连萌15-8-12#A

    #include <cstdio> #include <iostream> #include <algorithm> #include <queue> ...

  6. HTML、XHTML、css速记

    一.HTML 下面内容记录经常使用的html元素.可另存为html文件以查看效果: <!doctype html> <html lang="zh-cn"> ...

  7. 手机用appnium,web自动化用eclips+webdriver2

    手机用appnium,web自动化用eclips+webdriver2 吴建清 pycharm 1.安装环境2.pycharm类似eclipse,写脚本,运行脚本3.uiautomatorviewer ...

  8. 【AtCoder ABC 075 B】Minesweeper

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟,把#换成1 八个方向加一下就好. [代码] #include <bits/stdc++.h> using name ...

  9. 【Codeforces Round #434 (Div. 2) A】k-rounding

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 转换一下就是求n和10^k的最小公倍数. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++ ...

  10. 安装spark1.3.1单机环境 分类: B8_SPARK 2015-04-27 14:52 1873人阅读 评论(0) 收藏

    本文介绍安装spark单机环境的方法,可用于测试及开发.主要分成以下4部分: (1)环境准备 (2)安装scala (3)安装spark (4)验证安装情况 1.环境准备 (1)配套软件版本要求:Sp ...