思路:三元环。将无向图转换成有向无环图,详见
#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#include using namespace std;#define y1 y11#define fi first#define se second#define pi acos(-1.0)#define LL long long//#define mp make_pair#define pb emplace_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair #define pli pair #define pii pair #define piii pair #define pdd pair #define mem(a, b) memset(a, b, sizeof(a))#define debug(x) cerr << #x << " = " << x << "\n";#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//headconst int N = 2e5 + 5;vector g[N];struct Node {int u, v;}e[N];int n, m, d[N], vis[N], cnt[N];int main() { while(~scanf("%d %d", &n, &m)) { for (int i = 1; i <= m; ++i) scanf("%d %d", &e[i].u, &e[i].v), d[e[i].u]++, d[e[i].v]++; for (int i = 1; i <= m; ++i) { if(d[e[i].u] < d[e[i].v]) swap(e[i].u, e[i].v); if(d[e[i].u] == d[e[i].v] && e[i].u > e[i].v) swap(e[i].u, e[i].v); g[e[i].u].pb(i); } for (int u = 1; u <= n; ++u) { for (int id : g[u]) { int to = u^e[id].u^e[id].v; vis[to] = id; } for (int id : g[u]) { int to = u^e[id].u^e[id].v; for (int ii : g[to]) { int tt = to^e[ii].u^e[ii].v; if(vis[tt]) { cnt[id]++; cnt[ii]++; cnt[vis[tt]]++; } } } for (int id : g[u]) { int to = u^e[id].u^e[id].v; vis[to] = 0; } } LL ans = 0; for (int i = 1; i <= m; ++i) ans += cnt[i]*1LL*(cnt[i]-1)/2; printf("%lld\n", ans); for (int i = 1; i <= n; ++i) g[i].clear(), d[i] = 0; for (int i = 1; i <= m; ++i) cnt[i] = 0; } return 0;}