Skip to content

Commit babb828

Browse files
committed
Initial commit
0 parents  commit babb828

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2890
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
codebook.aux
2+
codebook.log
3+
codebook.pdf
4+
codebook.toc
5+
a.out
6+
a.exe
7+
a.exe.stackdump
8+
nohup.out
9+
.*.swp

2sat.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// 2-SAT solver based on Kosaraju's algorithm.
2+
// Variables are 0-based. Positive variables are stored in vertices 2n, corresponding negative variables in 2n+1
3+
// TODO: This is quite slow (3x-4x slower than Gabow's algorithm)
4+
struct TwoSat {
5+
int n;
6+
vector<vector<int> > adj, radj, scc;
7+
vector<int> sid, vis, val;
8+
stack<int> stk;
9+
int scnt;
10+
11+
// n: number of variables, including negations
12+
TwoSat(int n): n(n), adj(n), radj(n), sid(n), vis(n), val(n, -1) {}
13+
14+
// adds an implication
15+
void impl(int x, int y) { adj[x].push_back(y); radj[y].push_back(x); }
16+
// adds a disjunction
17+
void vee(int x, int y) { impl(x^1, y); impl(y^1, x); }
18+
// forces variables to be equal
19+
void eq(int x, int y) { impl(x, y); impl(y, x); impl(x^1, y^1); impl(y^1, x^1); }
20+
// forces variable to be true
21+
void tru(int x) { impl(x^1, x); }
22+
23+
void dfs1(int x) {
24+
if (vis[x]++) return;
25+
for (int i = 0; i < adj[x].size(); i++) {
26+
dfs1(adj[x][i]);
27+
}
28+
stk.push(x);
29+
}
30+
31+
void dfs2(int x) {
32+
if (!vis[x]) return; vis[x] = 0;
33+
sid[x] = scnt; scc.back().push_back(x);
34+
for (int i = 0; i < radj[x].size(); i++) {
35+
dfs2(radj[x][i]);
36+
}
37+
}
38+
39+
// returns true if satisfiable, false otherwise
40+
// on completion, val[x] is the assigned value of variable x
41+
// note, val[x] = 0 implies val[x^1] = 1
42+
bool two_sat() {
43+
scnt = 0;
44+
for (int i = 0; i < n; i++) {
45+
dfs1(i);
46+
}
47+
while (!stk.empty()) {
48+
int v = stk.top(); stk.pop();
49+
if (vis[v]) {
50+
scc.push_back(vector<int>());
51+
dfs2(v);
52+
scnt++;
53+
}
54+
}
55+
for (int i = 0; i < n; i += 2) {
56+
if (sid[i] == sid[i+1]) return false;
57+
}
58+
vector<int> must(scnt);
59+
for (int i = 0; i < scnt; i++) {
60+
for (int j = 0; j < scc[i].size(); j++) {
61+
val[scc[i][j]] = must[i];
62+
must[sid[scc[i][j]^1]] = !must[i];
63+
}
64+
}
65+
return true;
66+
}
67+
};

BIT-range.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// BIT with range updates, inspired by Petr Mitrichev
2+
struct BIT {
3+
int n;
4+
vector<int> slope;
5+
vector<int> intercept;
6+
// BIT can be thought of as having entries f[1], ..., f[n]
7+
// which are 0-initialized
8+
BIT(int n): n(n), slope(n+1), intercept(n+1) {}
9+
// returns f[1] + ... + f[idx-1]
10+
// precondition idx <= n+1
11+
int query(int idx) {
12+
int m = 0, b = 0;
13+
for (int i = idx-1; i > 0; i -= i&-i) {
14+
m += slope[i];
15+
b += intercept[i];
16+
}
17+
return m*idx + b;
18+
}
19+
// adds amt to f[i] for i in [idx1, idx2)
20+
// precondition 1 <= idx1 <= idx2 <= n+1 (you can't update element 0)
21+
void update(int idx1, int idx2, int amt) {
22+
for (int i = idx1; i <= n; i += i&-i) {
23+
slope[i] += amt;
24+
intercept[i] -= idx1*amt;
25+
}
26+
for (int i = idx2; i <= n; i += i&-i) {
27+
slope[i] -= amt;
28+
intercept[i] += idx2*amt;
29+
}
30+
}
31+
};

BIT.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Binary indexed tree supporting binary search.
2+
struct BIT {
3+
int n;
4+
vector<int> bit;
5+
// BIT can be thought of as having entries f[1], ..., f[n]
6+
// which are 0-initialized
7+
BIT(int n):n(n), bit(n+1) {}
8+
// returns f[1] + ... + f[idx-1]
9+
// precondition idx <= n+1
10+
int read(int idx) {
11+
idx--;
12+
int res = 0;
13+
while (idx > 0) {
14+
res += bit[idx];
15+
idx -= idx & -idx;
16+
}
17+
return res;
18+
}
19+
// returns f[idx1] + ... + f[idx2-1]
20+
// precondition idx1 <= idx2 <= n+1
21+
int read2(int idx1, int idx2) {
22+
return read(idx2) - read(idx1);
23+
}
24+
// adds val to f[idx]
25+
// precondition 1 <= idx <= n (there is no element 0!)
26+
void update(int idx, int val) {
27+
while (idx <= n) {
28+
bit[idx] += val;
29+
idx += idx & -idx;
30+
}
31+
}
32+
// returns smallest positive idx such that read(idx) >= target
33+
int lower_bound(int target) {
34+
if (target <= 0) return 1;
35+
int pwr = 1; while (2*pwr <= n) pwr*=2;
36+
int idx = 0; int tot = 0;
37+
for (; pwr; pwr >>= 1) {
38+
if (idx+pwr > n) continue;
39+
if (tot + bit[idx+pwr] < target) {
40+
tot += bit[idx+=pwr];
41+
}
42+
}
43+
return idx+2;
44+
}
45+
// returns smallest positive idx such that read(idx) > target
46+
int upper_bound(int target) {
47+
if (target < 0) return 1;
48+
int pwr = 1; while (2*pwr <= n) pwr*=2;
49+
int idx = 0; int tot = 0;
50+
for (; pwr; pwr >>= 1) {
51+
if (idx+pwr > n) continue;
52+
if (tot + bit[idx+pwr] <= target) {
53+
tot += bit[idx+=pwr];
54+
}
55+
}
56+
return idx+2;
57+
}
58+
};

BUILD

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
To build the notebook, you will need the software pdfLaTeX and a number of
2+
LaTeX packages: extsizes, amsmath, multicol, geometry, listings, color.
3+
4+
Building is accomplished by running the command "pdflatex codebook" *twice*.
5+
You should always run this command twice after every edit so that the table of
6+
contents will always be correct.

KMP.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Searches for the string w in the string s (of length k). Returns the
3+
0-based index of the first match (k if no match is found). Algorithm
4+
runs in O(k) time.
5+
*/
6+
7+
typedef vector<int> VI;
8+
9+
void buildTable(string& w, VI& t)
10+
{
11+
t = VI(w.length());
12+
int i = 2, j = 0;
13+
t[0] = -1; t[1] = 0;
14+
15+
while(i < w.length())
16+
{
17+
if(w[i-1] == w[j]) { t[i] = j+1; i++; j++; }
18+
else if(j > 0) j = t[j];
19+
else { t[i] = 0; i++; }
20+
}
21+
}
22+
23+
int KMP(string& s, string& w)
24+
{
25+
int m = 0, i = 0;
26+
VI t;
27+
28+
buildTable(w, t);
29+
while(m+i < s.length())
30+
{
31+
if(w[i] == s[m+i])
32+
{
33+
i++;
34+
if(i == w.length()) return m;
35+
}
36+
else
37+
{
38+
m += i-t[i];
39+
if(i > 0) i = t[i];
40+
}
41+
}
42+
return s.length();
43+
}

LICENSING

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
The editors believe that this notebook may be used, copied, and modified freely
2+
for the purposes of ACM competitions; however, please be aware that it
3+
incorporates material from various sources, which differ in their licensing.
4+
Some parts are not known to have been released under any license, so tread
5+
carefully.
6+
7+
In the LaTeX source (codebook.tex) there is a comment after each subsection
8+
heading indicating the authors of that section. (The notebook itself was
9+
compiled by Brian Bi.) The following licenses apply:
10+
11+
-> If "Stanford" is listed as an author, the code includes content (possibly
12+
modified) from Stanford's ACM notebook, which can be found here:
13+
http://stanford.edu/~liszt90/acm/notebook.html
14+
There is no license for this code, and the original authorship is unclear, so
15+
tread with caution.
16+
17+
-> Code by Frank Chu and Igor Naverniouk is released under a permissive license
18+
(see file Licenses/Igor).
19+
20+
-> Code by Tom Yubing Dong (tomtung) is released under the MIT license (see
21+
file Licenses/MIT-tomtung).
22+
23+
-> Code by Ilya Grebnov is released under the MIT license (see file
24+
Licenses/MIT-Gribok).
25+
26+
-> Code by Jimmy Mårdell is released under the MIT license (see file
27+
Licenses/MIT-Yarin).
28+
29+
-> Brian Bi, Jacob Plachta, Wesley May, Andre Hahn Pereira, and Qiyu Zhu have
30+
agreed to release their contributions under Unlicense (see file
31+
Licenses/Unlicense), effectively placing them into the public domain with no
32+
restrictions on their use. Note that this does not apply to code that was
33+
originally written by someone else and then translated, modified, and/or
34+
expanded by one or more of this set of authors. In that case, the original
35+
copyright and license (or lack thereof) apply.
36+
37+
You are encouraged to Unlicense any further contributions you might make to
38+
this notebook, whether or not you submit them upstream, in order to promote the
39+
free exchange of useful ideas and practices.

Licenses/Igor

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
The code in this archive is provided free of charge to be used by anyone for
2+
any reason whatsoever without any obligations. You can download, copy, modify
3+
and redistribute this code at your will.
4+
5+
If you find the code useful, I would love to hear from you
6+
(abednego[at]gmail(dot)c0m).
7+
8+
Igor Naverniouk © 2002-2006
9+
10+
The code in this archive is provided without any warranty, explicit or implied.
11+
It is not guaranteed to be correct, bug-free or fit for any purpose. The
12+
author will not be held liable for any damage done by executing the code or for
13+
any illegal activities conducted with the help of the code.
14+
15+
If you find a bug, I would love to hear from you.

Licenses/MIT-Gribok

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2008 Ilya Grebnov (Gribok)
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

Licenses/MIT-Yarin

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2002 Jimmy Mårdell (Yarin)
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

Licenses/MIT-tomtung

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright (c) 2007 Tom Yubing Dong (tomtung)
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

Licenses/Unlicense

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org>

README

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
This notebook is intended as reference material for a team participating in the
2+
ACM-ICPC World Finals or any regional competition that allows up to 25 pages of
3+
printed reference material. It incorporates code from various sources and is
4+
based on the notebook used by contestants at the University of Toronto from
5+
2013 to 2014.
6+
7+
Because this version of the notebook is intended for redistribution, it
8+
excludes a small amount of proprietary code present in U. of T.'s official team
9+
notebook.
10+
11+
The philosophy used when compiling this notebook was to include material that
12+
will be most useful for experienced contestants, in terms of reducing the
13+
amount of time spent coding and/or debugging. It therefore excludes algorithms
14+
that serious contestants would be able to code quickly and correctly with a
15+
minimum of effort, such as Dijkstra's algorithm. Most of the material in this
16+
notebook is therefore less common, theoretically nontrivial, or tricky to get
17+
right on the first try.

0 commit comments

Comments
 (0)