Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add enumerate_triangles #448

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions graph/enumerate_triangles/checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// https://github.com/MikeMirzayanov/testlib/blob/master/checkers/wcmp.cpp

// The MIT License (MIT)

// Copyright (c) 2015 Mike Mirzayanov

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "testlib.h"

using namespace std;

int main(int argc, char * argv[])
{
setName("compare sequences of tokens");
registerTestlibCmd(argc, argv);

int n = 0;
string j, p;

while (!ans.seekEof() && !ouf.seekEof())
{
n++;

ans.readWordTo(j);
ouf.readWordTo(p);

if (j != p)
quitf(_wa, "%d%s words differ - expected: '%s', found: '%s'", n, englishEnding(n).c_str(), compress(j).c_str(), compress(p).c_str());
}

if (ans.seekEof() && ouf.seekEof())
{
if (n == 1)
quitf(_ok, "\"%s\"", compress(j).c_str());
else
quitf(_ok, "%d tokens", n);
}
else
{
if (ans.seekEof())
quitf(_wa, "Participant output contains extra tokens");
else
quitf(_wa, "Unexpected EOF in the participants output");
}
}
53 changes: 53 additions & 0 deletions graph/enumerate_triangles/gen/erdos_renyi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "random.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <set>
#include <utility>

void erdos_renyi(int n, int m, Random &gen) {
assert(static_cast<long long>(n) * (n - 1) / 2 >= m);

std::vector<int> xs(n);
for (auto &x : xs) {
x = gen.uniform<int>(0, MOD - 1);
}

std::vector<std::pair<int, int>> edges(m);
if (static_cast<long long>(n) * (n - 1) / 2 > m * 2) {
// sparse
std::set<std::pair<int, int>> eset;
for (auto &e : edges) {
do {
e = gen.uniform_pair(0, n - 1);
} while (!eset.insert(std::minmax(e.first, e.second)).second);
}
} else {
// dense
std::vector<std::pair<int, int>> all;
for (int u = 0; u < n; ++u) {
for (int v = 0; v < u; ++v) {
if (gen.uniform_bool()) {
all.emplace_back(u, v);
} else {
all.emplace_back(v, u);
}
}
}
gen.shuffle(all.begin(), all.end());
std::copy(all.begin(), all.begin() + m, edges.begin());
}

std::printf("%d %d\n", n, m);
for (int i = 0; i < n; ++i) {
std::printf("%d", xs[i]);
if (i != n - 1) {
std::printf(" ");
} else {
std::printf("\n");
}
}
for (const auto &e : edges) {
std::printf("%d %d\n", e.first, e.second);
}
}
7 changes: 7 additions & 0 deletions graph/enumerate_triangles/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
4 5
1 2 3 4
0 3
2 0
2 1
2 3
1 3
15 changes: 15 additions & 0 deletions graph/enumerate_triangles/gen/large_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "../params.h"
#include "./erdos_renyi.h"
#include "random.h"
#include <algorithm>

int main(int, char **argv) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = gen.uniform<int>(N_MIN, N_MAX);
int m = gen.uniform<int>(
M_MIN, std::min(M_MAX, static_cast<long long>(n) * (n - 1) / 2));

erdos_renyi(n, m, gen);
}
13 changes: 13 additions & 0 deletions graph/enumerate_triangles/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../params.h"
#include "./erdos_renyi.h"
#include "random.h"

int main(int, char **argv) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int n = N_MAX;
int m = M_MAX;

erdos_renyi(n, m, gen);
}
3 changes: 3 additions & 0 deletions graph/enumerate_triangles/gen/minimum_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2 1
1 2
0 1
18 changes: 18 additions & 0 deletions graph/enumerate_triangles/gen/small_dense.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "../params.h"
#include "./erdos_renyi.h"
#include "random.h"

int main(int, char **argv) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int N_MAX_2 = 1;
while (N_MAX_2 < N_MAX && N_MAX_2 * (N_MAX_2 + 1) / 2 <= M_MAX) {
N_MAX_2 += 1;
}

int n = gen.uniform<int>(N_MIN, N_MAX_2);
int m = gen.uniform<int>(M_MIN, n * (n - 1) / 2);

erdos_renyi(n, m, gen);
}
20 changes: 20 additions & 0 deletions graph/enumerate_triangles/gen/small_sparse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "../params.h"
#include "./erdos_renyi.h"
#include "random.h"
#include <algorithm>

int main(int, char **argv) {
long long seed = atoll(argv[1]);
auto gen = Random(seed);

int N_MAX_2 = 1;
while (N_MAX_2 < N_MAX && N_MAX_2 * (N_MAX_2 + 1) / 2 <= M_MAX) {
N_MAX_2 += 1;
}

int n = gen.uniform<int>(N_MIN, N_MAX_2);
int m =
gen.uniform<int>(M_MIN, std::min<int>({M_MAX, n * 2, n * (n - 1) / 2}));

erdos_renyi(n, m, gen);
}
30 changes: 30 additions & 0 deletions graph/enumerate_triangles/hash.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"example_00.in": "43bd8eff15911662204e476d31b96a8f44884caa0b6754b33273d1d992ba3e3d",
"example_00.out": "a4b2c5db15348c29451e18b8307e5ef81625ea638e807935f39ceaa8d9ac7758",
"large_random_00.in": "564849d9ce38ce19402e0fe93e7c363c87880af9c3d6238f6c27f6c2854a039f",
"large_random_00.out": "60028d69846d40eb9527754dcd93a8e44bffe37356a1df9b0d34d60d41e492b5",
"large_random_01.in": "d89f4b01778842a5b6c21161d65fd4c02bde97950c714d893ff1a61a63230dbb",
"large_random_01.out": "9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa",
"large_random_02.in": "6c4b190a4ce85c633e886cde42130d7b3bb323eb857fccb4a984e836071d1927",
"large_random_02.out": "45611109ae4edd6520091229cbb99e31ea877496799b059ad236daf43c670c9e",
"max_random_00.in": "9e1c3aae5d07a0fd3ba5dfaa8476c393c4279bf1b3777336225fa966bfdee2e6",
"max_random_00.out": "f06d429f13c4b39ae9ae0016f4925c41372312540dfa0a2b2c165c9d6f3fbb83",
"max_random_01.in": "34182bc0eef3c24785c3b31086939e8b2cc3152332a1a995729c0e0ad905288a",
"max_random_01.out": "e2d347c0e1dea1c5cd716303eaf2012104c9ec489d32f4f44daf988e36a1b5ef",
"max_random_02.in": "e0caeabe0d3ee50733011ac5e8143ee7ba9c84814d21c538c03336ead707a076",
"max_random_02.out": "bf26c4a8da5ce761e302fa877cd5c3ef055475ecfa9dc01ae20047cc4734ce58",
"minimum_00.in": "5982244103c9d82ae9b5fcdf1e6fcccecb3da77fee1facd7e8736b0602d35448",
"minimum_00.out": "9a271f2a916b0b6ee6cecb2426f0b3206ef074578be55d9bc94f6f3fe3ab86aa",
"small_dense_00.in": "68e6ae6ebae1a3200468c8ff7c76adb1052cdfc9988fe8bbf4fdd589cfa75917",
"small_dense_00.out": "b88c3096b08cd1d111f4192d8b310354c8b6c7e436d4bde35ab1e0b4d76f419e",
"small_dense_01.in": "49fd90cb207d168932d35d4d4c23912a06c9a7f5ff938f867840e9ab84a839e5",
"small_dense_01.out": "f0a03600f373d006727aa449d008d9c565970906fb079b1f6a724ac7fc87ef9d",
"small_dense_02.in": "933ca0590f078d2bb59c0a0b00a5dcf35656de31b3e51ac092dc3806df3c2079",
"small_dense_02.out": "7ff5c7b1d88fc106b16281afbbadb37a10fc6f368a0cd39738422a387613bbb0",
"small_sparse_00.in": "9ed22f92d638eea978925b95e56d99fdf2c5f90f2c669b8c7b22c821dca256d9",
"small_sparse_00.out": "5f5ce23c41b2c2dca216abfdf9d450ac7df652130f6c4a35a91c43153db6dfbe",
"small_sparse_01.in": "fea9e0d6e35cd652acb3f3c4cdee190b05577818bf2cae6377fe7ce91de549bc",
"small_sparse_01.out": "a9374288152d752966cdeed03f26c2687acaece10bd10a618cb7f71054a65efe",
"small_sparse_02.in": "c18141bfdca75f537997264567ee6e68e421b45a21c9f2da40f21af7cd530e2b",
"small_sparse_02.out": "d8606372ea1350af4fb4905771b4753cd790d783615821767c0e91acc208e9a3"
}
36 changes: 36 additions & 0 deletions graph/enumerate_triangles/info.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
title = 'Enumerate Triangles'
timelimit = 5.0
forum = "https://github.com/yosupo06/library-checker-problems/issues/442"

[[tests]]
name = "example.in"
number = 1
[[tests]]
name = "minimum.in"
number = 1
[[tests]]
name = "small_sparse.cpp"
number = 3
[[tests]]
name = "small_dense.cpp"
number = 3
[[tests]]
name = "large_random.cpp"
number = 3
[[tests]]
name = "max_random.cpp"
number = 3

[[solutions]]
name = 'naive.cpp'
wrong = true
[[solutions]]
name = 'correct_slow.cpp'
wrong = false

[params]
N_MIN = 1
N_MAX = 100_000
M_MIN = 1
M_MAX = 100_000
MOD = 998_244_353
61 changes: 61 additions & 0 deletions graph/enumerate_triangles/sol/correct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <cstdio>
#include <tuple>
#include <utility>
#include <vector>

int main() {
using i64 = long long;
static constexpr i64 mod = 998244353;

int n, m;
std::scanf("%d %d", &n, &m);
std::vector<i64> xs(n);
for (auto &x : xs) {
scanf("%lld", &x);
}
std::vector<std::pair<int, int>> edges(m);
for (auto &e : edges) {
int &u = e.first;
int &v = e.second;
std::scanf("%d %d", &u, &v);
}

std::vector<int> deg(n, 0);
for (const auto &e : edges) {
int u = e.first;
int v = e.second;
deg[u] += 1;
deg[v] += 1;
}

std::vector<std::vector<int>> g(n);
for (const auto &e : edges) {
int u = e.first;
int v = e.second;
if (std::tie(deg[u], u) > std::tie(deg[v], v)) {
std::swap(u, v);
}
g[u].push_back(v);
}

i64 ans = 0;
std::vector<bool> adj(n, false);
for (const auto &e : edges) {
int a = e.first;
int b = e.second;
for (auto c : g[a]) {
adj[c] = true;
}
for (auto c : g[b]) {
if (adj[c]) {
ans += xs[a] * xs[b] % mod * xs[c];
ans %= mod;
}
}
for (auto c : g[a]) {
adj[c] = false;
}
}

std::printf("%lld\n", ans);
}
58 changes: 58 additions & 0 deletions graph/enumerate_triangles/sol/correct_slow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <algorithm>
#include <cstdio>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

int main() {
using i64 = long long;
static constexpr i64 mod = 998244353;

int n, m;
std::scanf("%d %d", &n, &m);
std::vector<i64> xs(n);
for (auto &x : xs) {
scanf("%lld", &x);
}
std::vector<std::pair<int, int>> edges(m);
std::set<std::pair<int, int>> eset;
for (auto &e : edges) {
int &u = e.first;
int &v = e.second;
std::scanf("%d %d", &u, &v);
eset.insert(std::minmax(u, v));
}

std::vector<int> deg(n, 0);
for (const auto &e : edges) {
int u = e.first;
int v = e.second;
deg[u] += 1;
deg[v] += 1;
}

std::vector<std::vector<int>> g(n);
for (const auto &e : edges) {
int u = e.first;
int v = e.second;
if (std::tie(deg[u], u) > std::tie(deg[v], v)) {
std::swap(u, v);
}
g[u].push_back(v);
}

i64 ans = 0;
for (int a = 0; a < n; ++a) {
for (int b : g[a]) {
for (int c : g[a]) {
if (eset.count({b, c})) {
ans += xs[a] * xs[b] % mod * xs[c];
ans %= mod;
}
}
}
}

std::printf("%lld\n", ans);
}