Codeforces 155E E. Double Profiles【思维+hash】

本文探讨了在一个大型社交网络中寻找可能属于同一用户的双重资料的方法。通过将每个用户的社交关系视为一个排序后的集合,利用哈希技术进行高效匹配,最终统计出所有符合条件的用户对数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题目大意;


给出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);
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值