Double Profiles CodeForces - 154C (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 ≠ 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.

Example
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 的应用;
    符合题目要求的点只有两种:一是许多点都两两相交, 二是这两个点与其它点都不相交 
    给每个点都赋给一个值,如果两个点i,j相交,就 ha[i] 加上相交点的 hash[j] 值,ha[j]加上hash[i]。
    然后对于每个点都加上自己的 hash 值,这样得到的 hb 值就可以保证满足第一种条件的 hb 都相等。
    对于第二种情况,考虑 ha 的值,如果ha 相等则说明它们满足第二种
    最后对于两种情况都是从这些点中选出 2 两个,求组合数 
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<algorithm>

#define ll __int64
#define inf 0x3f3f3f3f
#define maxn 1000007  // 1e6+7

using namespace std;

const int mod = 10000007;
ll ha[maxn],hb[maxn];
ll hash[maxn];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);

    hash[1] = 1;
    for(int i=2;i<=n;i++)
        hash[i] = hash[i-1] * mod;

    int u,v;
    for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            ha[u] += hash[v];
            ha[v] += hash[u];
        }
    for(int i=1;i<=n;i++)
        hb[i] = ha[i] + hash[i];

    sort(ha+1,ha+1+n);
    sort(hb+1,hb+1+n);

    ll ans = 0;
    ll tmp = ha[1];
    ll cnt = 1;



    for(int i=2;i<=n;i++)  // 第二种情况 
    {
    //  printf("%I64d  %I64d\n",tmp,ha[i]);
        if(ha[i] == tmp)
            cnt++;
        else{
            ans += cnt * (cnt-1) / 2;// 选两个,求组合 
            cnt = 1;
            tmp = ha[i];
        } 
    }

    ans += cnt * ( cnt - 1) / 2;
//  printf("ans= %I64d\n",ans);

    tmp = hb[1];  // 第一种 
    cnt = 1;
    for(int i=2;i<=n;i++)
        if(hb[i] == tmp)
            cnt ++;
        else {
            ans += cnt*(cnt-1) / 2;  // 选两个,求组合 
            cnt = 1;
            tmp = hb[i];
        }
    ans += cnt * (cnt - 1) / 2;

    printf("%I64d\n",ans);

//  for(int i=1;i<=n;i++)
//      printf("%d  ha[i] == %I64d\n",i,ha[i]);
//  printf("\n");
//  for(int i=1;i<=n;i++)
//      printf("%d  hb[i] == %I64d\n",i,hb[i]);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值