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 3 1 2 2 3 1 3
3
3 0
3
4 1 1 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).
题目大意;
给出N个点M条边,问有多少点对(u,v),使得和点u有直接相连的边和v也直接相连,并且和u没有直接相连的点也不能有和点v直接相连的边。
思路:
我们将每个点能够连接的点都放在集合中并且排序处理的话,我们可以视为他是一个字符串
①那么对于两个点,如果其字符串相等,那么答案就可以加1。
那么对于多个点的这样处理我们显然不能O(n^2)枚举,那么我们考虑hash优化一下查找值就行了,这样就变成了O(n+m)的复杂度 。
②另外一种情况就是两个点通过一条边(u,v)相连,那么如果存在ha【u】+h【u】==ha【v】+h【v】的话,答案也可以加1,那么这个过程我们O(m)枚举一条边判等就行了。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
unsigned long long int h[1060000];
unsigned long long int ha[1060000];
int xx[1060000],yy[1060000];
int main()
{
h[0]=1;
for(int i=1;i<=1000000;i++)h[i]=h[i-1]*19260817;
int n,m;
while(~scanf("%d%d",&n,&m))
{
__int64 ans=0;
memset(ha,0,sizeof(ha));
for(int i=1;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
xx[i]=x;yy[i]=y;
ha[x]+=h[y];
ha[y]+=h[x];
}
for(int i=1;i<=m;i++)
{
int x=xx[i],y=yy[i];
unsigned long long int A=ha[x]+h[x];
unsigned long long int B=ha[y]+h[y];
if(A==B)ans++;
}
map<unsigned long long int,__int64>s;
for(int i=1;i<=n;i++)
{
s[ha[i]]++;
}
for(int i=1;i<=n;i++)
{
ans+=(s[ha[i]]*(s[ha[i]]-1)/2);
s[ha[i]]=0;
}
printf("%I64d\n",ans);
}
}