Codeforces 155 C.Double Profiles


hash+sort

C. Double Profiles
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≠ ik ≠ 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 jcan 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 ≤ 1060 ≤ 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 test(s)
input
3 3
1 2
2 3
1 3
output
3
input
3 0
output
3
input
4 1
1 3
output
2
Note

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


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1001000;

typedef unsigned long long int ull;

int n,m;

ull p[maxn],h[maxn],g[maxn];

int main()
{
	scanf("%d%d",&n,&m);
	p[0]=1;
	for(int i=1;i<=n;i++)
		p[i]=p[i-1]*175;
	for(int i=0;i<m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		h[x]+=p[y]; h[y]+=p[x];
	}
	for(int i=1;i<=n;i++)
		g[i]=p[i]+h[i];
	long long int ans=0,cnt=0;
	sort(h+1,h+1+n);
	for(int i=2;i<=n;i++)
	{
		if(h[i]==h[i-1])
			ans+=++cnt;
		else cnt=0;
	}
	sort(g+1,g+1+n);
	cnt=0;
	for(int i=2;i<=n;i++)
	{
		if(g[i]==g[i-1])
			ans+=++cnt;
		else cnt=0;
	}
	printf("%I64d\n",ans);
	return 0;
}


你可能感兴趣的:(Codeforces 155 C.Double Profiles)