题目链接:

http://codeforces.com/problemset/problem/155/E

E. Double Profiles

time limit per test 3 seconds
memory 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的更多相关文章

  1. 贪心 Codeforces Round #109 (Div. 2) B. Combination

    题目传送门 /* 贪心:按照能选的个数和点数降序排序,当条件不符合就break,水题啊! */ #include <cstdio> #include <algorithm> # ...

  2. Codeforces Round #109 (Div. 1) 题解 【ABC】

    A - Hometask 题意:给你一个字符串,然后再给你k个禁止挨在一起的字符串,问你最少删除多少个字符串,使得不会有禁忌的字符串对挨在一起.题目保证每个字符最多出现在一个禁忌中. 题解:由于每个字 ...

  3. 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]( ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  8. 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 ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. Storyboard里面的几种Segue区别和视图的切换

    几种segue:push.modal.popover.replace.cutom. 一.视图切换类型介绍 1.在iPhone和iPad中,segue的类型是不同的. 2.在iPhone中,segue有 ...

  2. 详解Oracle临时表的几种用法及意义

    Oracle临时表可以说是提高数据库处理性能的好方法,在没有必要存储时,只存储在Oracle临时表空间中.希望本文能对大家有所帮助. 1 .前言 Oracle Logo 目前所有使用 Oracle 作 ...

  3. 如何消除选定TextBox后的光标但又不失去焦点。

    情景描述: 选择TextBox里的内容 Name:textTile 但是没有光标. 相关实现代码: [DllImport("user32", EntryPoint = " ...

  4. silverlight 退出当前页面、跳转到主页面

    1.退出当前页面 private void imgExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (Message ...

  5. HTML5高防win8风格

    使用HTML5做了一个高防Win8的页面,大家看看怎么样 Java..NET.PHP.Android.iOS.HTML5,CSS3. web前端.视频资料下载网站  http://51pansou.c ...

  6. centos下安装php环境

    centos下安装php环境 安装apache yum install httpd-devel 启动apache /etc/init.d/httpd start 安装mysql yum install ...

  7. 往Android SDCard中读写入数据

    一.用Environment (写) 1.API获取sdcard的路径 File path=Environment.getExternalStorageDirectory(); path=new Fi ...

  8. U盘启动

    2014.4.3修改 其实用U盘制作系统也可以下载一个软碟通UltraISO,就可以很方便的制作. ----以前的版本 用U盘装系统,很方便快捷,下面这个网站介绍的比较详细,于是自己整理了一下,作为收 ...

  9. Timer wheel etc.

    http://code.google.com/p/hierarchal-wheel-timer/ 最小堆的实现(C 语言版) 最小堆的实现(java) Linux 下定时器的实现方式分析 更快bobh ...

  10. c语言结构体保存并输出学生信息

    最近在学习数据结构,巩固下c语言. #include<stdio.h> /*定义结构体student并设置别名stud*/ /*typedef struct student{ int nu ...