summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-03-10 20:50:43 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-03-10 20:51:35 +0100
commit8bad05892517601c7161b34a5ab775290d254938 (patch)
treed22a161d17165cf0ff0beb8d9292f520d702136f
parentcd870938f6cf266292e0e5cecb2d96d604b2ad9d (diff)
parentdb64838afe7f333d45f3c76c8e72aea89ccbf52a (diff)
Merge mzuenni changes
-rw-r--r--graph/2sat.cpp16
-rw-r--r--graph/reroot.cpp59
-rw-r--r--graph/virtualTree.cpp25
3 files changed, 89 insertions, 11 deletions
diff --git a/graph/2sat.cpp b/graph/2sat.cpp
index 8fb3d39..75e54e6 100644
--- a/graph/2sat.cpp
+++ b/graph/2sat.cpp
@@ -2,7 +2,7 @@ struct sat2 {
int n; // + scc variablen
vector<int> sol;
- sat2(int vars) : n(vars*2), adj(vars*2) {};
+ sat2(int vars) : n(vars*2), adj(n) {}
static int var(int i) {return i << 1;} // use this!
@@ -18,20 +18,14 @@ struct sat2 {
void addAnd(int a, int b) {addTrue(a); addTrue(b);}
void addNand(int a, int b) {addOr(1^a, 1^b);}
- bool solvable() {
+ bool solve() {
scc(); //scc code von oben
+ sol.assign(n, -1);
for (int i = 0; i < n; i += 2) {
if (idx[i] == idx[i + 1]) return false;
+ sol[i] = idx[i] < idx[i + 1];
+ sol[i + 1] = !sol[i];
}
return true;
}
-
- void assign() {
- sol.assign(n, -1);
- for (int i = 0; i < sccCounter; i++) {
- if (sol[sccs[i][0]] == -1) {
- for (int v : sccs[i]) {
- sol[v] = 1;
- sol[1^v] = 0;
- }}}}
};
diff --git a/graph/reroot.cpp b/graph/reroot.cpp
new file mode 100644
index 0000000..eeca43e
--- /dev/null
+++ b/graph/reroot.cpp
@@ -0,0 +1,59 @@
+// Usual Tree DP can be broken down in 4 steps:
+// - Initialize dp[v] = identity
+// - Iterate over all children w and take a value for w
+// by looking at dp[w] and possibly the edge label of v -> w
+// - combine the values of those children
+// usually, this operation should be commutative and associative
+// - finalize the value of dp[v] after iterating over all children
+struct Reroot{
+ using T = ll;
+
+ // identity element
+ T E(){}
+ // x: dp value of child
+ // e: index of edge going to child
+ T takeChild(T x, int e){}
+ T combine(T x, T y){}
+ // called after combining all dp values of children
+ T finalize(T x, int v){}
+
+ vector<vector<pair<int, int>>> g;
+ vector<int> ord, pae;
+ vector<T> dp;
+
+ T dfs(int v){
+ ord.push_back(v);
+ for(auto [w, e] : g[v]){
+ g[w].erase(find(all(g[w]), pair(v, e^1)));
+ pae[w] = e^1;
+ dp[v] = combine(dp[v], takeChild(dfs(w), e));
+ }
+ return dp[v] = finalize(dp[v], v);
+ }
+
+ vector<T> solve(int n, vector<pair<int, int>> edges){
+ g.resize(n);
+ for(int i = 0; i < n-1; i++){
+ g[edges[i].first].emplace_back(edges[i].second, 2*i);
+ g[edges[i].second].emplace_back(edges[i].first, 2*i+1);
+ }
+ pae.assign(n, -1);
+ dp.assign(n, E());
+ dfs(0);
+ vector<T> updp(n, E()), res(n, E());
+ for(int v : ord){
+ vector<T> pref(sz(g[v])+1), suff(sz(g[v])+1);
+ if(v != 0) pref[0] = takeChild(updp[v], pae[v]);
+ for(int i = 0; i < sz(g[v]); i++){
+ pref[i+1] = suff[i] = takeChild(dp[g[v][i].first], g[v][i].second);
+ pref[i+1] = combine(pref[i], pref[i+1]);
+ }
+ for(int i = sz(g[v])-1; i >= 0; i--)
+ suff[i] = combine(suff[i], suff[i+1]);
+ for(int i = 0; i < sz(g[v]); i++)
+ updp[g[v][i].first] = finalize(combine(pref[i], suff[i+1]), v);
+ res[v] = finalize(pref.back(), v);
+ }
+ return res;
+ }
+}; \ No newline at end of file
diff --git a/graph/virtualTree.cpp b/graph/virtualTree.cpp
new file mode 100644
index 0000000..f7a3cb1
--- /dev/null
+++ b/graph/virtualTree.cpp
@@ -0,0 +1,25 @@
+// needs dfs in- and out- time and lca function
+vector<int> in, out;
+
+void virtualTree(const vector<int>& a) { // takes indices of used nodes
+ auto ind = a;
+ sort(all(ind), [&](int x, int y) {return in[x] < in[y];});
+
+ for (int i=0; i<sz(a)-1; i++) {
+ ind.push_back(lca(ind[i], ind[i+1]));
+ }
+ sort(all(ind), [&](int x, int y) {return in[x] < in[y];});
+ ind.erase(unique(all(ind)), ind.end());
+
+ int n = ind.size();
+ vector<vector<int>> tree(n);
+ stack<int> st{{0}};
+ for (int i=1; i<n; i++) {
+ while (in[ind[i]] >= out[ind[st.top()]]) st.pop();
+ tree[st.top()].push_back(i);
+ st.push(i);
+ }
+
+ // virtual directed tree with n nodes, original indices in ind
+ // weights can be calculated if necessary, e.g. with binary lifting
+}