Skip to content

Commit

Permalink
Merge pull request #745 from hotman78/744-permutation_tree
Browse files Browse the repository at this point in the history
add permutation tree
  • Loading branch information
yosupo06 authored Aug 11, 2022
2 parents fb610aa + 495d8ce commit e6baec4
Show file tree
Hide file tree
Showing 20 changed files with 936 additions and 0 deletions.
58 changes: 58 additions & 0 deletions graph/common_interval_decomposition_tree/checker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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");
}
}
44 changes: 44 additions & 0 deletions graph/common_interval_decomposition_tree/gen/almost_straight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "../params.h"
#include "random.h"

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

#include "../params.h"
#include "random.h"

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_MAX);
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
if (gen.uniform_bool())
std::reverse(p.begin(), p.end());
int q = gen.uniform(0LL, SMALL_SWAP_NUM);
while (q--) {
auto [s, t] = gen.uniform_pair<int>(P_MIN, n - 1);
std::swap(p[s], p[t]);
}

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
24 changes: 24 additions & 0 deletions graph/common_interval_decomposition_tree/gen/ascending_order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "../params.h"
#include "random.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_MAX);
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
26 changes: 26 additions & 0 deletions graph/common_interval_decomposition_tree/gen/descending_order.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "../params.h"
#include "random.h"

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_MAX);
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
std::reverse(p.begin(), p.end());

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
2 changes: 2 additions & 0 deletions graph/common_interval_decomposition_tree/gen/example_00.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
3
0 1 2
2 changes: 2 additions & 0 deletions graph/common_interval_decomposition_tree/gen/example_01.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
4
0 2 1 3
2 changes: 2 additions & 0 deletions graph/common_interval_decomposition_tree/gen/example_02.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
10
8 0 9 2 1 4 6 5 7 3
74 changes: 74 additions & 0 deletions graph/common_interval_decomposition_tree/gen/killer_case.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "../params.h"
#include "random.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = N_MAX;
std::vector<int> p(n);
auto dfs = [&](auto dfs, int l, int r, int mn, int mx) -> void {
if (r - l < 7) {
// std::cerr<<l<<" "<<r<<std::endl;
// assert(r>l);
auto tmp = gen.perm<int>(r - l);
for (int i = 0; i < r - l; ++i)
p[l + i] = tmp[i] + mn;
} else {
auto sep = gen.choice(4, mn + 1, mx - 1);
sep.insert(sep.begin(), mn);
sep.emplace_back(mx);
// std::cerr<<sep.size()<<std::endl;
// for(int i=0;i<4;++i){
// assert(sep[i+1]>sep[i]);
// }
// std::cerr<<sep[0]<<sep[2]<<sep[4]<<std::endl;
const int type = gen.uniform(0, 3);
if (type == 0) {
//cut node
std::array<int, 5> v = { 4, 2, 0, 3, 1 };
int m = l;
for (int i = 0; i < 5; ++i) {
dfs(dfs, m, m + sep[v[i] + 1] - sep[v[i]], sep[v[i]], sep[v[i] + 1]);
m += sep[v[i] + 1] - sep[v[i]];
}
assert(m == r);
} else if (type == 1) {
//cut node
std::array<int, 5> v = { 0, 3, 1, 4, 2 };
int m = l;
for (int i = 0; i < 5; ++i) {
dfs(dfs, m, m + sep[v[i] + 1] - sep[v[i]], sep[v[i]], sep[v[i] + 1]);
m += sep[v[i] + 1] - sep[v[i]];
}
assert(m == r);
} else {
// join node
const int sep_size = gen.uniform(2, 5);
auto sep = gen.choice(sep_size - 1, mn + 1, mx - 1);
sep.insert(sep.begin(), mn);
sep.emplace_back(mx);
int m = l;
for (int i = 0; i < sep_size; ++i) {
dfs(dfs, m, m + sep[i + 1] - sep[i], sep[i], sep[i + 1]);
m += sep[i + 1] - sep[i];
}
assert(m == r);
}
}
};
dfs(dfs, 0, n, 0, n);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
40 changes: 40 additions & 0 deletions graph/common_interval_decomposition_tree/gen/killer_straight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "../params.h"
#include "random.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = N_MAX;
std::vector<int> p(n);
auto dfs = [&](auto dfs, int l, int r, int mn, int mx, bool rev) -> void {
if (r - l < 5) {
auto tmp = gen.perm<int>(r - l);
for (int i = 0; i < r - l; ++i)
p[l + i] = tmp[i] + mn;
} else {
int m = (l + r) / 2;
if (rev) {
dfs(dfs, l, m, mn + (r - m), mx, 0);
dfs(dfs, m, r, mn, mn + (r - m), 0);
} else {
dfs(dfs, l, m, mn, mn + (m - l), 1);
dfs(dfs, m, r, mn + (m - l), mx, 1);
}
}
};
dfs(dfs, 0, n, 0, n, 0);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
23 changes: 23 additions & 0 deletions graph/common_interval_decomposition_tree/gen/max_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../params.h"
#include "random.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = N_MAX;
auto p = gen.perm<int>(n);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
23 changes: 23 additions & 0 deletions graph/common_interval_decomposition_tree/gen/random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "random.h"
#include "../params.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_MAX);
auto p = gen.perm<int>(n);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
23 changes: 23 additions & 0 deletions graph/common_interval_decomposition_tree/gen/small_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../params.h"
#include "random.h"

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_SMALL_MAX);
auto p = gen.perm<int>(n);

printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
33 changes: 33 additions & 0 deletions graph/common_interval_decomposition_tree/gen/swap_random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "../params.h"
#include "random.h"

#include <algorithm>
#include <array>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <utility>
#include <vector>

int main(int, char* argv[])
{
long long seed = std::atoll(argv[1]);
auto gen = Random(seed);
const int n = gen.uniform(N_MIN, N_MAX);
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
if (gen.uniform_bool())
std::reverse(p.begin(), p.end());
int q = gen.uniform(0, n);
while (q--) {
auto [s, t] = gen.uniform_pair<int>(P_MIN, n - 1);
std::swap(p[s], p[t]);
}
printf("%d\n", n);
for (int i = 0; i < n; ++i) {
if (i)
printf(" ");
printf("%d", p[i]);
}
printf("\n");
}
Loading

0 comments on commit e6baec4

Please sign in to comment.