Double Profiles - CodeForces 154C 哈希
来源:互联网 发布:angularjs js cdn 编辑:程序博客网 时间:2024/06/10 11:21
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.
3 31 22 31 3
3
3 0
3
4 11 3
2
In the first and second sample any two profiles are doubles.
In the third sample the doubles are pairs of profiles (1, 3) and (2, 4).
思路:哈希得到一个人对应的其他朋友的值,如果两个人可以成为答案的话,那么它们的哈希值是一样的(他们不是朋友),或哈希值加上自己本身是一样的(他们是朋友)。
AC代码如下:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;long long Hash[1000010],A[1000010],B[1000010],ans,pre,ret;int main(){ int n,m,i,j,k,a,b; scanf("%d%d",&n,&m); Hash[1]=1; for(i=2;i<=n;i++) Hash[i]=Hash[i-1]*10000007; for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); A[a]+=Hash[b]; A[b]+=Hash[a]; } for(i=1;i<=n;i++) B[i]=A[i]+Hash[i]; pre=-100000;ret=0; sort(A+1,A+1+n); sort(B+1,B+1+n); for(i=1;i<=n;i++) if(A[i]==pre) { ret++; ans+=ret; } else { ret=0; pre=A[i]; } pre=-100000;ret=0; for(i=1;i<=n;i++) if(B[i]==pre) { ret++; ans+=ret; } else { ret=0; pre=B[i]; } printf("%I64d\n",ans);}
- Double Profiles - CodeForces 154C 哈希
- Codeforces 154C Double Profiles
- codeforces 154C Double Profiles (简单图论+哈希)
- Codeforces 155 C.Double Profiles
- Double Profiles CodeForces
- Double Profiles CodeForces
- Codeforces Round #109 (Div. 1) C. Double Profiles
- Codeforces 155E E. Double Profiles【思维+hash】
- codeforces 113C C. Double Happiness(数论+暴力)
- 【随便做做】Codeforces#86 Problem C Double Happiness
- double.c
- Educational Codeforces Round 1C. Nearest vectors(极角排序+long double 精度)
- CodeForces 82A Double Cola
- Codeforces A. Double Cola 题解
- CodeForces-82A-Double Cola
- Codeforces 101484 E Double Fence
- c/c++: float和double、long double
- c/c++: float和double、long double
- NYOJ 2 括号配对问题
- 【LeetCode】 Rotate List
- 将session的值存储到memcached中去,配置以及方法和注意事项
- Java GC 日志分析
- 按时间回到过去有他速度快
- Double Profiles - CodeForces 154C 哈希
- jquery对表单元素的取值和赋值
- POJ 2421 Constructing Roads
- 【专题】—【数据类型】—【long long 与 __int64 的区别】
- 关于研究生面试系统的探索研究V
- python中.py和.pyw文件的区别
- 物联网管理软件的设计
- jdk环境变量配置
- hdoj 4906 状态dp