Live2d Test Env

CodeForces - 154C:Double Profiles (hash+排序)

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

Examples

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

题意:给定无向图,问有多少对点对,在不考虑它们之间的连边情况下,满足它们的直接连边的情况相同。

思路:用hash保存每个点的连边情况,然后排序得到hash值,相同的一并处理。

有两类:第一类是点对间有边,那么我们需要向自己加一条边,那么它们的连边情况是相同的。

第二类是点对间无边,直接处理即可。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define uint unsigned int
using namespace std;
const int maxn=2000010;
const int seed=1331;
const int seed2=131;
uint p[maxn],p2[maxn];long long ans;
pair<uint,uint>h[maxn];
int main()
{
    int N,M,u,v;
    scanf("%d%d",&N,&M); p[0]=1; p2[0]=1;
    rep(i,1,N) p[i]=p[i-1]*seed;
    rep(i,1,N) p2[i]=p2[i-1]*seed2;
    rep(i,1,N) h[i].first=p[i],h[i].second=p2[i];
    rep(i,1,M){
        scanf("%d%d",&u,&v);
        h[u].first+=p[v]; h[v].first+=p[u];
        h[u+N].first+=p[v]; h[v+N].first+=p[u];
        h[u].second+=p2[v]; h[v].second+=p2[u];
        h[u+N].second+=p2[v]; h[v+N].second+=p2[u];
    }
    sort(h+1,h+N+N+1);
    for(int i=1;i<=N+N;i++){
        int j=i;
        while(j+1<=N+N&&h[j+1]==h[i]) j++;
        ans+=(long long)(j-i+1)*(j-i)/2;
        i=j;
    }
    printf("%I64d\n",ans);
    return 0;
}

 

posted @ 2018-08-21 22:10  nimphy  阅读(339)  评论(0编辑  收藏  举报