This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_triangles"
#include <bits/stdc++.h>
using namespace std;
#include "../../../lib/graph/enumerate_triangles.hpp"
#include "../../../lib/math/modint.hpp"
using mint = ModInt<998244353>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<mint> x(n);
for(int i = 0; i < n; i++){
cin >> x[i];
}
vector<vector<int>> G(n);
for(int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
mint ans = 0;
for(auto &s : enumerateTriangles(G)){
mint mul = 1;
for(auto &i : s){
mul *= x[i];
}
ans += mul;
}
cout << ans << '\n';
}
#line 1 "test/library_checker/graph/enumerate_triangles.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/enumerate_triangles"
#include <bits/stdc++.h>
using namespace std;
#line 2 "lib/graph/enumerate_triangles.hpp"
/**
* @brief Enumerate Triangles
* @see https://www.slideshare.net/slideshow/trianguler/38443802
*/
vector<array<int, 3>> enumerateTriangles(vector<vector<int>> &G){
int n = G.size();
vector<vector<int>> DAG(n);
for(int i = 0; i < n; i++){
for(auto &j : G[i]){
pair<int, int> p = {(int) G[i].size(), i}, q = {(int) G[j].size(), j};
if(p < q){
DAG[i].push_back(j);
}
}
}
vector<array<int, 3>> res;
vector<int> edge(n);
for(int i = 0; i < n; i++){
for(auto j : DAG[i]){
for(auto k : DAG[i]){
edge[k]++;
}
for(auto k : DAG[j]){
if(edge[k]){
res.push_back({i, j, k});
}
}
for(auto k : DAG[i]){
edge[k]--;
}
}
}
return res;
}
#line 2 "lib/math/modint.hpp"
#line 5 "lib/math/modint.hpp"
/**
* @brief ModInt
* @docs docs/math/modint.md
*/
template <long long Modulus>
struct ModInt{
long long val;
static constexpr int mod() { return Modulus; }
constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
normalize();
}
void normalize(){
val = (val % Modulus + Modulus) % Modulus;
}
inline ModInt &operator+=(const ModInt &rhs) noexcept {
if(val += rhs.val, val >= Modulus) val -= Modulus;
return *this;
}
inline ModInt &operator-=(const ModInt &rhs) noexcept {
if(val -= rhs.val, val < 0) val += Modulus;
return *this;
}
inline ModInt &operator*=(const ModInt &rhs) noexcept {
val = val * rhs.val % Modulus;
return *this;
}
inline ModInt &operator/=(const ModInt &rhs) noexcept {
val = val * inv(rhs.val).val % Modulus;
return *this;
}
inline ModInt &operator++() noexcept {
if(++val >= Modulus) val -= Modulus;
return *this;
}
inline ModInt operator++(int) noexcept {
ModInt t = val;
if(++val >= Modulus) val -= Modulus;
return t;
}
inline ModInt &operator--() noexcept {
if(--val < 0) val += Modulus;
return *this;
}
inline ModInt operator--(int) noexcept {
ModInt t = val;
if(--val < 0) val += Modulus;
return t;
}
inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
inline ModInt inv(void) const { return inv(val); }
ModInt pow(long long n) const {
assert(0 <= n);
ModInt x = *this, r = 1;
while(n){
if(n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
ModInt inv(const long long n) const {
long long a = n, b = Modulus, u = 1, v = 0;
while(b){
long long t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
u %= Modulus;
if(u < 0) u += Modulus;
return u;
}
friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
is >> x.val;
x.normalize();
return is;
}
friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 7 "test/library_checker/graph/enumerate_triangles.test.cpp"
using mint = ModInt<998244353>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<mint> x(n);
for(int i = 0; i < n; i++){
cin >> x[i];
}
vector<vector<int>> G(n);
for(int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
mint ans = 0;
for(auto &s : enumerateTriangles(G)){
mint mul = 1;
for(auto &i : s){
mul *= x[i];
}
ans += mul;
}
cout << ans << '\n';
}