【bzoj4264】小C找朋友
-
题解
- $a$和$b$是好*友说明除了这两个人以外的邻接集合相同;
- 做两次$hash$,分别都处理和$a$相邻的点排序$hash$,①$a$要算进$a$的相邻集合,②$a$不算进;
- 当两个人不是好*友,一定不会统计,当是且两个人不相邻,会仅被②统计,当是且相邻会仅被①统计;
- 枚举所有的$hash$值统计答案;
- %了$Claris$后学会了对每个点随机生成一个较大值,异或起来$hash$的方法
- 这样用于集合$hash$不用排序,并且删除一个元素直接异或即可
-
1 #include<cstdio> 2 #include<map> 3 #include<iostream> 4 #include<vector> 5 #include<algorithm> 6 #define ull unsigned long long 7 using namespace std; 8 const int N=1000010; 9 int n,m; 10 vector<int>s[N]; 11 map<ull,int>mp; 12 map<ull,int>::iterator it; 13 int main(){ 14 freopen("bzoj4264.in","r",stdin); 15 freopen("bzoj4264.out","w",stdout); 16 scanf("%d%d",&n,&m); 17 for(int i=1,x,y;i<=m;i++){ 18 scanf("%d%d",&x,&y); 19 s[x].push_back(y); 20 s[y].push_back(x); 21 } 22 ull ans=0; 23 for(int i=1;i<=n;i++){ 24 sort(s[i].begin(),s[i].end()); 25 unique(s[i].begin(),s[i].end()); 26 ull x=0; 27 for(int j=0;j<(int)s[i].size();j++){ 28 x = x * (ull)1234567891 + s[i][j]; 29 } 30 mp[x]++; 31 } 32 for(it = mp.begin();it!=mp.end();it++){ 33 ans += (ull)it -> second * (it -> second - 1) / 2; 34 } 35 mp.clear(); 36 for(int i=1;i<=n;i++){ 37 s[i].push_back(i); 38 sort(s[i].begin(),s[i].end()); 39 unique(s[i].begin(),s[i].end()); 40 ull x=0; 41 for(int j=0;j<(int)s[i].size();j++){ 42 x = x * (ull)1234567891 + s[i][j]; 43 } 44 mp[x]++; 45 } 46 for(it = mp.begin();it!=mp.end();it++){ 47 ans += (ull)it -> second * (it -> second - 1) / 2; 48 } 49 cout << ans << endl; 50 return 0; 51 }