hdu 6184 Counting Stars

给定一张$n$个点的无向图,其中有$m$条边,找出如下图形的个数

$2 \le n \le 10^5$

$1 \le m \le \min(2 \times 10^5,\frac{n(n-1)}{2})$

枚举三元环,对于每条边,查询经过它的三元环的个数,设为$cnt_i$,答案为$\sum C_{cnt_i}^{2}$

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 3e5 + 10;
 4 vector<pair<int, int> > g[N];
 5 int deg[N], vis[N], n, m, u[N], v[N], cnt[N];
 6 queue<int> q;
 7 int main() {
 8     while(scanf("%d%d", &n, &m) == 2) {
 9         for(int i = 1 ; i <= m ; ++ i) {
10             scanf("%d%d", &u[i], &v[i]);
11             ++ deg[u[i]], ++ deg[v[i]];
12         }
13         for(int i = 1 ; i <= m ; ++ i) {
14             int x = u[i], y = v[i];
15             if(deg[x] < deg[y]) swap(x, y);
16             if(deg[x] == deg[y] && x > y) swap(x, y);
17             g[x].push_back({ y, i });
18         }
19         for(int u = 1 ; u <= n ; ++ u) {
20             for(auto v: g[u]) vis[v.first] = v.second;
21             for(auto v: g[u])
22                 for(auto w: g[v.first])
23                     if(vis[w.first])
24                         ++ cnt[v.second], ++ cnt[w.second], ++ cnt[vis[w.first]];
25             for(auto v: g[u]) vis[v.first] = 0;
26         }
27         long long ans = 0;
28         for(int i = 1 ; i <= m ; ++ i) {
29             ans += 1ll * cnt[i] * (cnt[i] - 1) / 2;
30             cnt[i] = 0;
31         }
32         printf("%lld\n", ans);
33         for(int i = 1 ; i <= n ; ++ i) g[i].clear(), deg[i] = 0;
34     }
35 }
hdu 6184 Counting Stars
posted @ 2018-08-13 08:13  KingSann  阅读(358)  评论(0编辑  收藏  举报