Description
幼儿园里有N个小C,两个小C之间可能是朋友也可能不是。所有小C之间的朋友关系构成了一个无向图,这个无向图中有M条边。
园长ATM发现对于两个(不同的)小Ci和j,如果其他的所有小C要么同时是i,j的朋友,要么同时不是i,j朋友的话,这两个小C就很有可能一起去吃饭,成为一对好友。出于一些未知的原因,ATM需要你帮他求出可能成为好友的小C的对数。
Input
第一行一个数N,M,如题目描述。
接下来M行,每行2个数表示一条无向边。
Output
输出可能成为好*友的小C的对数。
Sample Input
3 3
1 2
2 3
1 3
Sample Output
3
HINT
N,M<=1000000
Source
跟zky学长学习了新姿势:边集Hash
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 1000010
#define GET (ch>='0'&&ch<='9')
#define LL long long
using namespace std;
int n,m;
LL val[MAXN],hash[MAXN];
LL ans,cnt;
struct edge
{
int u,v;
bool operator <(const edge& a)const {return u==a.u?v<a.v:u<a.u;}
bool operator ==(const edge& a)const {return u==a.u&&v==a.v;}
}e[MAXN];
void in(int &x)
{
char ch=getchar();x=0;
while (!GET) ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();
}
int main()
{
in(n);in(m);
for (int i=1;i<=n;i++) val[i]=((LL)rand()<<32)|rand();
for (int i=1;i<=m;i++)
{
in(e[i].u);in(e[i].v);
if (e[i].u>e[i].v) swap(e[i].u,e[i].v);
}
sort(e+1,e+m+1);m=unique(e+1,e+m+1)-e-1;
for (int i=1;i<=m;i++) if (e[i].u!=e[i].v) hash[e[i].u]^=val[e[i].v],hash[e[i].v]^=val[e[i].u];
for (int i=1;i<=m;i++) if (e[i].u!=e[i].v&&((hash[e[i].u]^val[e[i].v])==(hash[e[i].v]^val[e[i].u]))) ans++;
sort(hash+1,hash+n+1);
for (int i=1;i<=n;i++)
{
cnt++;
if (i==n||hash[i]!=hash[i+1]) ans+=cnt*(cnt-1)>>1,cnt=0;
}
cout<<ans<<endl;
}