Double Profiles
This problem will be judged on CodeForces. Original ID: 154C
64-bit integer IO format: %I64d Java class name: (Any)
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.
Input
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.
Output
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
3 3
1 2
2 3
1 3
3
3 0
3
4 1
1 3
2
Hint
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).
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
LL _hash[maxn],A[maxn],B[maxn];
int main(){
int n,m,x,y,i;
LL ans = ,cnt1 = ,cnt2 = ,pre1 = -,pre2 = -;
_hash[] = ;
scanf("%d %d",&n,&m);
for(i = ; i <= n; i++)
_hash[i] = _hash[i-]*;
for(i = ; i < m; i++){
scanf("%d%d",&x,&y);
A[x] += _hash[y];
A[y] += _hash[x];
}
for(i = ; i <= n; i++)
B[i] += A[i]+_hash[i];
sort(A+,A+n+);
sort(B+,B+n+);
for(i = ; i <= n; i++){
if(A[i] == pre1){
cnt1++;
ans += cnt1;
}else{
cnt1 = ;
pre1 = A[i];
}
if(B[i] == pre2){
cnt2++;
ans += cnt2;
}else{
cnt2 = ;
pre2 = B[i];
}
}
printf("%I64d\n",ans);
return ;
}