Codeforces Round #109 (Div. 2) E. Double Profiles hash
题目链接:
http://codeforces.com/problemset/problem/155/E
E. Double Profiles
time limit per test 3 secondsmemory limit per test 256 megabytes
#### 问题描述
> You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user.
>
> The social network contains n registered profiles, numbered from 1 to n. Some pairs there are friends (the "friendship" relationship is mutual, that is, if i is friends with j, then j is also friends with i). Let's say that profiles i and j (i ≠ j) are doubles, if for any profile k (k ≠ i, k ≠ j) one of the two statements is true: either k is friends with i and j, or k isn't friends with either of them. Also, i and j can be friends or not be friends.
>
> Your task is to count the number of different unordered pairs (i, j), such that the profiles i and j are doubles. Note that the pairs are unordered, that is, pairs (a, b) and (b, a) are considered identical.
#### 输入
> The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), — the number of profiles and the number of pairs of friends, correspondingly.
>
> Next m lines contains descriptions of pairs of friends in the format "v u", where v and u (1 ≤ v, u ≤ n, v ≠ u) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself.
#### 输出
> Print the single integer — the number of unordered pairs of profiles that are doubles.
>
> Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator.
#### 样例
> **sample input**
> 4 1
> 1 3
>
> **sample output**
> 2
题意
给你一个n个点m条边的无向图,让你求一些顶点对(无序)(u,v),满足对于任意的k(k!=u,v),k要么到两个点都有边,要么到两个点都没边,对于(u,v)本身可以存在边也可以不存在。
题解
这题如果你想到用hash,思路就会很直观了!
对于顶点u,我们把它的所有相邻的点(不包括它自己)都存起来,hash成一个x进制数ha[u](每个位对应一个节点,用加权的方式加起来)接下来把顶点对分成两类:
1、顶点对之间没有边相邻:
如果两个顶点u,v它们的ha[u]=ha[v](即它们的相邻顶点集完全一样!),那么它们就是满足的顶点对。
2、顶点对之间没有边相邻:
如果ha[u]+u=ha[v]+v(u会出现在ha[v]里面,v会出现在ha[u]里面),同理,它们也满足条件。对于有边的,一条一条统计。没边的吧ha数组排个序,扫一遍就可以了。
代码
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#define X first
#define Y second
#define mp make_pair
using namespace std;
const int maxn=1e6+10;
typedef unsigned __int64 LL;
const LL P=123;
int n,m;
LL pw[maxn];
LL ha[maxn];
void pre(){
memset(ha,0,sizeof(ha));
pw[0]=1;
for(int i=1;i<maxn;i++) pw[i]=pw[i-1]*P;
}
int main(){
pre();
scanf("%d%d",&n,&m);
vector<pair<int,int> > egs;
for(int i=0;i<m;i++){
int u,v;
scanf("%d%d",&u,&v);
egs.push_back(mp(u,v));
ha[u]+=pw[v];
ha[v]+=pw[u];
}
LL ans=0;
for(int i=0;i<m;i++){
int u=egs[i].X,v=egs[i].Y;
if(ha[u]+pw[u]==ha[v]+pw[v]) ans++;
}
sort(ha+1,ha+n+1);
for(int i=1;i<n;i++){
if(ha[i]==ha[i+1]){
int ed=i;
while(ed+1<=n&&ha[ed]==ha[ed+1]) ed++;
LL len=ed-i+1;
ans+=len*(len-1)/2;
i=ed;
}
}
printf("%I64d\n",ans);
return 0;
}
Codeforces Round #109 (Div. 2) E. Double Profiles hash的更多相关文章
- 贪心 Codeforces Round #109 (Div. 2) B. Combination
题目传送门 /* 贪心:按照能选的个数和点数降序排序,当条件不符合就break,水题啊! */ #include <cstdio> #include <algorithm> # ...
- Codeforces Round #109 (Div. 1) 题解 【ABC】
A - Hometask 题意:给你一个字符串,然后再给你k个禁止挨在一起的字符串,问你最少删除多少个字符串,使得不会有禁忌的字符串对挨在一起.题目保证每个字符最多出现在一个禁忌中. 题解:由于每个字 ...
- codeforces水题100道 第十九题 Codeforces Round #109 (Div. 2) A. I_love_%username% (brute force)
题目链接:http://www.codeforces.com/problemset/problem/155/A题意:找到当前最大值或者最小值出现的次数.“当前”的意思差不多是a[i]大于所有a[j]( ...
- Codeforces Round #109 (Div. 2) A. I_love_%username%【打擂台算法/满足当前数字在已经出现的数字的最大值和最小值之间的个数】
A. I_love_%username% time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #215 (Div. 2) D题(离散化+hash)
D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #FF (Div. 2) A. DZY Loves Hash
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the ...
- Codeforces Round #272 (Div. 2) 题解
Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- 合并多个List<T>类型并通过LINQ按指定属性排序
后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...
- HTML自动换行的问题
有时文本文字已经超过所在的区域,但是文字还是不自动换行 可以用强制换行 强制不换行div{ white-space:nowrap;}自动换行div{ word-wrap:break-word; wor ...
- Setup Factory 程序打包
1.检测是否已安装 在On Startup中写入如下代码: result1=Registry.DoesKeyExist(HKEY_LOCAL_MACHINE, "Software\\sohe ...
- 利用脚本修改SQL SERVER排序规则
利用脚本修改SQL SERVER排序规则 编写人:CC阿爸 2014-3-1 l 今年的一项重要工作是对公司所用系统进行繁简的转换,程序转成简体基本很容易解决,但数据库转换成简体,就没那么容易了.经 ...
- win7里开始菜单属性里的隐私项无法选择解决方法
具体问题如下图,其中的隐私项目呈现灰色无法选择: 解决方法如下: win+R 输入gpedit.msc 回车,[用户配置][管理模板][开始菜单和任务栏],在右侧找到[关闭用户跟踪],双击进入设置界面 ...
- DevExpress控件开发常用要点(项目总结版)
使用DevExpress控件来做项目开发已经有很长一段时间了,在摸索开发到客户苛刻要求的过程中,其中碰到过很多问题需要解决的,随着一个个问题的解决,也留下很多对DevExpress控件的使用经验及教训 ...
- SQL语句的执行顺序
一.sql语句的执行顺序 (8)SELECT (9) DISTINCT (11) <TOP_specification> <select_list> (1) FROM < ...
- 【javascript】随手记代码
//js实现的当前界面的刷新.前进.后退 <input type="button" value="刷新" onclick="window.loc ...
- JavaWeb之 Servlet执行过程 与 生命周期
Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ...
- 宝马测试(C++实现)
测试目的:对编辑器放大,缩小性能测试. 测试资源:一匹宝马. 测试结果:良好. 实现方法:通过调用本地保存的宝马文件,逐字逐行的显示在编辑器中,并放大,缩小.对不同的符号进 ...