diff --git a/algorithms/geometry/convex_hull.cpp b/algorithms/geometry/convex_hull.cpp index 387467042daf4230d814b610d4e1a5fc3068a34e..33a0f2da16290fe9029ba7e43b66be1a6cb295a4 100644 --- a/algorithms/geometry/convex_hull.cpp +++ b/algorithms/geometry/convex_hull.cpp @@ -15,7 +15,7 @@ struct ConvexHull { /// Finds, among v, the points that form a convex hull /// @param v vector of points - int run(const vector<dd> &v) { + vector<int> run(const vector<dd> &v) { int k = 0; vector<int> ans(v.size() * 2); @@ -39,13 +39,7 @@ struct ConvexHull { // The ans vector contains the indices (relative to the sorted vector!) // of the points belonging to the convex hull - ans.resize(k); - - // Remove duplicates - sort(all(ans)); - ans.erase(unique(all(ans)), ans.end()); - - // Return number of points in the convex hull - return k - 1; + ans.resize(k - 1); + return ans; } }; diff --git a/algorithms/graph/centroid_decomposition.cpp b/algorithms/graph/centroid_decomposition.cpp index d9a34c0559eecf8f8c6d613767cce141a4583a84..6952825c661b323fc50ef70ab8e2a62b048d993a 100644 --- a/algorithms/graph/centroid_decomposition.cpp +++ b/algorithms/graph/centroid_decomposition.cpp @@ -2,21 +2,24 @@ /// /// Complexity (Time): O(V log V) /// Complexity (Space): O(V + E) - -// Must be a tree -vector<int> graph[MAX]; - +/// /// The Centroid Decomposition of a tree is a tree where: /// - Its root is the centroid of the original tree. /// - Its children are the centroid of each tree resulting from the removal /// of the root from the original tree. +/// /// The result is a tree with lg(n) height, where the path from a to b in /// the original tree can be decomposed into the path from a to lca(a,b) and /// from lca(a,b) to b, where lca(a,b) is the lowest common ancestor of a and b /// in the centroid decomposition. +/// /// This is useful because each one of the n^2 paths of the original tree is /// the concatenation of two paths in a set of O(n lg(n)) paths from a node to /// all its ancestors in the centroid decomposition. + +// Must be a tree +vector<int> graph[MAX]; + struct CentroidDecomposition { vector<int> par, size, marked; @@ -24,23 +27,21 @@ struct CentroidDecomposition { /// @param N number of vertices CentroidDecomposition(int N) : par(N), size(N), marked(N) - { - init(); - } + { init(); } void init() { fill(all(marked), 0); // Assuming vertices are 0-indexed - build(0, -1); + build(0); } /// Builds the centroid decomposition of the tree recursively. - /// @param x initial node + /// @param x initial node (root) /// @param p parent node - void build(int x, int p) { - int n = dfs(x, -1); - int centroid = get_centroid(x, -1, n); + void build(int x, int p = -1) { + int n = dfs(x); + int centroid = get_centroid(x, n); marked[centroid] = 1; par[centroid] = p; @@ -53,7 +54,7 @@ struct CentroidDecomposition { /// Calculates size of every subtree (in the original tree). /// @param x initial node /// @param p parent node - int dfs(int x, int p) { + int dfs(int x, int p = -1) { size[x] = 1; for (auto i : graph[x]) if (i != p && !marked[i]) @@ -65,9 +66,9 @@ struct CentroidDecomposition { /// Finds centroid by recursively searching for it in the subtree /// with more than n / 2 nodes in it. /// @param x initial node - /// @param p parent node /// @param n size of initial subtree - int get_centroid(int x, int p, int n) { + /// @param p parent node + int get_centroid(int x, int n, int p = -1) { for (auto i : graph[x]) if (i != p && size[i] > n / 2 && !marked[i]) return get_centroid(i, x, n); diff --git a/contests/CodeJam/2018/Round 1A/A/solve.cpp b/contests/CodeJam/2018/Round 1A/A.cpp similarity index 100% rename from contests/CodeJam/2018/Round 1A/A/solve.cpp rename to contests/CodeJam/2018/Round 1A/A.cpp diff --git a/contests/CodeJam/2018/Round 1A/A/in b/contests/CodeJam/2018/Round 1A/A/in deleted file mode 100644 index 1d7239047690506fa9ec6908899f42ed70c631e9..0000000000000000000000000000000000000000 --- a/contests/CodeJam/2018/Round 1A/A/in +++ /dev/null @@ -1,28 +0,0 @@ -6 -3 6 1 1 -.@@..@ -.....@ -@.@.@@ -4 3 1 1 -@@@ -@.@ -@.@ -@@@ -4 5 1 1 -..... -..... -..... -..... -4 4 1 1 -..@@ -..@@ -@@.. -@@.. -3 4 2 2 -@.@@ -@@.@ -@.@@ -3 4 1 2 -.@.@ -@.@. -.@.@ diff --git a/contests/CodeJam/2018/Round 2/a.out b/contests/CodeJam/2018/Round 2/a.out deleted file mode 100755 index 0151de957bf717487cacbff8a43e59fcc27cff15..0000000000000000000000000000000000000000 Binary files a/contests/CodeJam/2018/Round 2/a.out and /dev/null differ diff --git a/gen_notebook b/gen_notebook new file mode 100755 index 0000000000000000000000000000000000000000..7f12fa6d139bbeb276e5df7db9d5658f7efe4ee5 --- /dev/null +++ b/gen_notebook @@ -0,0 +1,10 @@ +#!/bin/bash + +#tex_file=$(mktemp) +python3 notebook/gen_latex.py # --header=notebook/header.tex --output=$tex_file + +#pdflatex $tex_file -output-directory . && +#pdflatex $tex_file -output-directory . + +#mv tmp.pdf caderno.pdf +#rm tmp* diff --git a/notebook/gen_latex.py b/notebook/gen_latex.py index ef9134ff68e8ea999bb3be3192cffc3eca736dcc..1bdd8505becc2b5b1c95b7389a7d911da980aec7 100644 --- a/notebook/gen_latex.py +++ b/notebook/gen_latex.py @@ -1,96 +1,76 @@ -import os -import sys -import subprocess -import argparse - from pathlib import Path -dirs = [ - 'algorithms', - 'misc', - 'contests', - 'problems' -] - -parser = argparse.ArgumentParser() -parser.add_argument('--header', action='store', type=str, help='The text to parse.') -parser.add_argument('--output', action='store', type=str, help='The text to parse.') -args = parser.parse_args() - -output = open(args.output, 'w') - -# Read Latex header -with open(args.header) as f: - data = f.readlines() - -# Print header to output -for i in data: - output.write(i) - -last_sections = [None] * 2 - -for di in dirs: - path_list = Path(di).glob('**/*.cpp') - - for path in path_list: - file_name = str(path) - sections = file_name.replace('_', '\_').split('/') - - # Sections[0] is [algorithms, contests, problems, misc] (sections) - if sections[0] != last_sections[0]: - output.write('\\newpage\n') - output.write('\\section{' + sections[0].capitalize() + '}\n') - - # Sections[1] is name of constest or category of algorithm (subsections) - if sections[1] != last_sections[1]: - output.write('\\subsection{' + sections[1].capitalize() + '}\n') - - # Parse source code - with open(file_name) as f: - source = f.readlines() - - # Separate into comment and code, and define title - title = "" - in_comment = False - code, comment = [], [] - for line in source: - if '/**' == line[0:3]: - in_comment = True - - if in_comment: - if len(title) == 0 and len(comment) == 1: - title = line[3:] - comment.append(line) - else: - code.append(line) - - if '*/' in line: - in_comment = False - - if len(sections) > 2: - output.write('\\subsubsection{' + title + '}\n') - - in_complexity = 0 - ctime, cspace = [], [] - for line in comment: - if 'Complexity (time)' in line: - in_complexity = 1 - if 'Complexity (space)' in line: - in_complexity = 2 - - if in_complexity: - ctime.append(line) - - - # Remove first \n after header comment - code = code[1:] - - #output.write('\\lstinputlisting[style=customcpp]{' + file_name + '}\n') - output.write('\\begin{lstlisting}[style=customcpp]\n') - for i in code: - output.write(i) - output.write('\\end{lstlisting}\n') - output.write('\\\n') - last_sections = sections - -output.write('\\end{document}') +class Tree: + + # Constructor. + # @param dirs list of directories to build Latex over + def __init__(self, dirs): + self.dirs = dirs; + self.tree = {} + self.build() + + # Sorts the lists on tree leaves. + def sort(self, sub): + if type(sub) == list: + return sorted(sub) + else: + for i in sub: + sub[i] = self.sort(sub[i]) + return sub + + # Builds a tree represented as dict, with structure of files + # and directories. + def build(self): + for di in self.dirs: + path_list = Path(di).glob('**/*.cpp') + + for path in path_list: + branch = str(path).split('/') + curr = self.tree + + for i in branch[:-2]: + if not i in curr: + curr[i] = {} + curr = curr[i] + + if not branch[-2] in curr: + curr[branch[-2]] = [] + curr[branch[-2]].append(str(branch[-1])) + + self.tree = self.sort(self.tree) + + def __getitem__(self, arg): + return self.tree[arg] + + def __iter__(self): + return iter(self.tree) + + +class LatexGenerator: + def __init__(self, tree): + self.tree = tree + self.hierarchy = ['chapter'] + [i*'sub' + 'section' for i in range(3)] + self.gen_latex(tree, 0) + + # Prints elements in arr in Latex format. + def gen_code_latex(self, content, depth): + print('\\' + self.hierarchy[depth] + '{' + content + '}') + + # Generates Latex for entire tree recursively + def gen_latex(self, sub, depth): + if type(sub) == list: + for i in sub: + self.gen_code_latex(i, depth) + else: + for i in sub: + self.gen_code_latex(i, depth) + self.gen_latex(sub[i], depth + 1) + + +def main(): + dirs = [ 'algorithms', 'misc' ] + tree = Tree(dirs) + tex = LatexGenerator(tree) + +if __name__ == "__main__": + main() diff --git a/run b/run deleted file mode 100755 index 99c4c760c51bd0e9be318c465705a671b5125576..0000000000000000000000000000000000000000 --- a/run +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -tex_file=$(mktemp) -python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file - -pdflatex $tex_file -output-directory . && -pdflatex $tex_file -output-directory . - -mv tmp.pdf caderno.pdf -rm tmp* diff --git a/testing.py b/testing.py deleted file mode 100644 index 1280b7fae7ad690a77e2707b9ce0093b36baa25a..0000000000000000000000000000000000000000 --- a/testing.py +++ /dev/null @@ -1,65 +0,0 @@ -from pathlib import Path - -dirs = [ - 'algorithms', - 'misc', - 'contests', - 'problems' -] - -# Returns a tree represented as dict, with structure of files -# and directories -def get_file_tree(): - tree = {} - for di in dirs: - path_list = Path(di).glob('**/*.cpp') - - for path in path_list: - branch = str(path).split('/') - - curr = tree - for i in branch[:-2]: - if not i in curr: - curr[i] = {} - curr = curr[i] - - if not branch[-2] in curr: - curr[branch[-2]] = [] - curr[branch[-2]].append(str(branch[-1])) - - return tree - -# Print elements in arr in Latex format -def gen_code_latex(arr, depth): - for i in arr: - print('\\' + depth + '{' + i + '}') - -# Generate Latex from tree -def gen_latex(tree): - stack = [ (tree, 0) ] - depth = ['chapter', 'section', 'subsection'] - - while len(stack) != 0: - x, h = stack.pop() - print(x) - #if type(x) == list: - # gen_code_latex(x, depth[h]) - - #print('\\' + depth[h] + '{' + i + '}') - for i in x: - stack.append((x[i], h + 1)) - - - -tree = get_file_tree() -for i in tree: - print(i) - for j in tree[i]: - print('\t' + j) - if type(tree[i]) != list: - for k in tree[i][j]: - print('\t\t' + k) - print() - print() -#print(tree) -#gen_latex(tree)