-
Notifications
You must be signed in to change notification settings - Fork 31
/
EnumerateTriangles.go
119 lines (104 loc) · 2.3 KB
/
EnumerateTriangles.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// EnumeratingC3
// 求出无向图中的所有三元环(a,b,c) a<b<c
// O(E^1.5)
// https://kopricky.github.io/code/Graph/EnumeratingC3.html
package main
import (
"bufio"
"fmt"
"math"
"os"
)
// 枚举所有的三元环,传递回调函数减少空间开销
func enumerateC3(n int, edges [][]int, cb func(u, v, w int)) {
threshold := 0
adjList := make([][]int, n)
for _, e := range edges {
u, v := e[0], e[1]
// 无向边定向
if u < v {
adjList[u] = append(adjList[u], v)
} else {
adjList[v] = append(adjList[v], u)
}
threshold++
}
threshold = int(math.Sqrt(float64(threshold / 4)))
memo := make([][]int, n)
visited := make([]bool, n)
processHighDegree := func() {
for i := 0; i < n; i++ {
if len(adjList[i]) <= threshold {
continue
}
for _, u := range adjList[i] {
visited[u] = true
}
for _, u := range adjList[i] {
for _, v := range adjList[u] {
if visited[v] {
cb(i, u, v)
}
}
}
for _, u := range adjList[i] {
visited[u] = false
}
}
}
processLowDegree := func() {
for i := 0; i < n; i++ {
if len(adjList[i]) > threshold {
continue
}
for _, u := range adjList[i] {
for _, v := range adjList[i] {
if v > u {
memo[u] = append(memo[u], i*(1<<32)+v)
}
}
}
}
for i := 0; i < n; i++ {
for _, u := range adjList[i] {
visited[u] = true
}
for j := 0; j < len(memo[i]); j++ {
hash := memo[i][j]
a, b := hash>>32, hash&0xffffffff
if visited[b] {
cb(a, i, b)
}
}
for _, u := range adjList[i] {
visited[u] = false
}
}
}
processHighDegree()
processLowDegree()
}
func main() {
// https://judge.yosupo.jp/problem/enumerate_triangles
const MOD int = 998244353
in := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
defer out.Flush()
var n, m int
fmt.Fscan(in, &n, &m)
values := make([]int, n)
for i := 0; i < n; i++ {
fmt.Fscan(in, &values[i])
}
edges := make([][]int, m)
for i := 0; i < m; i++ {
edges[i] = make([]int, 2)
fmt.Fscan(in, &edges[i][0], &edges[i][1])
}
res := 0
enumerateC3(n, edges, func(u, v, w int) {
res += values[u] * values[v] % MOD * values[w] % MOD
res %= MOD
})
fmt.Println(res)
}