题目链接:

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. IOS绘图——简单三角形

    #import <UIKit/UIKit.h> @interface MyView : UIView @end #import "MyView.h" @implemen ...

  2. HTML 5 Canvas

    HTML 5 Canvas   HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. <canvas id="myCanvas" width=&q ...

  3. DevExpress 14.2 批量汉化 以及客户端的汉化

    DXperience汉化方法介绍 运用慧都提供的DXperience汉化包,能将最新版本的DXperience WinForm和ASP.NET控件界面.弹出框.右键菜单等汉化成中文,且能根据自己的需求 ...

  4. 11)Java abstract class 和 interface

    abstract class 和 interface 的区别        含有abstract修饰符的class即为抽象类,abstract 类不能创建实例对象.含有abstract方法的类必须定义 ...

  5. 【转】Spark性能测试报告

    RDD可以很好地适用于支持数据并行的批量分析应用,包括数据挖掘,机器学习,图算法等,因为这些程序通常都会在很多记录上执行相同的操作.RDD不太适合那些异步更新共享状态的应用,例如并行web爬行器.因此 ...

  6. C#创建、读取和修改Excel

    // Namespaces, Variables, and Constants using System; using System.Configuration; using System.Data; ...

  7. android 模拟按键事件

    模拟按键事件可以提高代码的复用性,比如在一个edittext的回车事件里做的一些处理 在该edittext的另一个输入要做相同的处理时,模拟按键事件就非常方便了. 代码很简单,直接上代码: new T ...

  8. Java 为什么使用抽象类和接口

    Java接口和Java抽象类代表的就是抽象类型,就是我们需要提出的抽象层的具体表现.OOP面向对象的编程,如果要提高程序的复用率,增加程序的可维护性,可扩展性,就必须是面向接口的编程,面向抽象的编程, ...

  9. SRF之权限控制

    框架目前提供url访问.菜单和页面元素的权限控制和数据权限,权限基于角色来分配,1个用户可以属于多个角色,权限项分模块.页面.操作3级别,其中模块.页面用于url和菜单的控制,操作是对页面元素的控制. ...

  10. android开发图片分辨率问题解决方案

    dpi是什么呢? dpi是“dot per inch”的缩写,每英寸像素数. 四种密度分类: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (ex ...