标签:using include turn mem ring ast ble ace structure
Problem Description
题意:给定一张无向图,求有公共边的三元环对数。
Solution:
三元环裸题。
直接三元环计数,然后开一个桶记录一下每条边在多少个三元环中出现,最后的答案就是$\sum_\limits{i=1}^{i\leq m}{\frac{tot[i]*(tot[i]-1)}{2}}$。
代码:
/*Code by 520 -- 9.10*/ #include<iostream> #include<cstdio> #include<cstring> #define il inline #define ll long long #define RE register #define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++) #define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--) using namespace std; const int N=100005,M=200005; int n,m,to[M],net[M],h[N],cnt,tot[M],pre[N],vis[N],deg[N]; struct node{ int u,v; }e[M]; ll ans; il void add(int u,int v){to[++cnt]=v,net[cnt]=h[u],h[u]=cnt;} int main(){ while(scanf("%d%d",&n,&m)==2){ For(i,1,m) scanf("%d%d",&e[i].u,&e[i].v),deg[e[i].u]++,deg[e[i].v]++; For(i,1,m) { RE int u=e[i].u,v=e[i].v; if(deg[u]<deg[v]||deg[u]==deg[v]&&u>v) swap(u,v); add(u,v); } For(u,1,n){ for(RE int i=h[u];i;i=net[i]) vis[to[i]]=u,pre[to[i]]=i; for(RE int i=h[u];i;i=net[i]){ RE int v=to[i]; for(RE int j=h[v];j;j=net[j]){ RE int w=to[j]; if(vis[w]==u) ++tot[i],++tot[j],++tot[pre[w]]; } } } For(i,1,cnt) ans+=1ll*tot[i]*(tot[i]-1)/2; printf("%lld\n",ans); memset(h,0,sizeof(h)),memset(deg,0,sizeof(deg)), memset(tot,0,sizeof(tot)),memset(pre,0,sizeof(pre)), memset(vis,0,sizeof(vis)),cnt=0,ans=0; } return 0; }
标签:using include turn mem ring ast ble ace structure
原文地址:https://www.cnblogs.com/five20/p/9623592.html