diff --git a/_posts/2024-01-26-january26.markdown b/_posts/2024-01-26-january26.markdown index 31113ff..73aebf6 100644 --- a/_posts/2024-01-26-january26.markdown +++ b/_posts/2024-01-26-january26.markdown @@ -11,19 +11,21 @@ I have a compact pc for these cases, but I do not have a monitor for it. Now I j I created this docker compose file in a folder:\ ~/myfolder/docker-compose.yml ```yml -version: "3" +version: "1" services: dev: - image: arm64v8/ubuntu - platform: linux/arm64 + image: amd64/ubuntu + platform: linux/amd64 volumes: - - .:/root/myfolder + - .:/root/myfolder2 working_dir: /root + cpus: 2 network_mode: host security_opt: - seccomp:unconfined + privileged: true cap_add: - - SYS_PTRACE + - ALL ``` diff --git a/_posts/2024-01-31-january31.markdown b/_posts/2024-01-31-january31.markdown index 855d141..dc97786 100644 --- a/_posts/2024-01-31-january31.markdown +++ b/_posts/2024-01-31-january31.markdown @@ -1,61 +1,12 @@ --- layout: post -title: "graph coloring and linear programming" +title: "graph coloring" date: 2024-01-31 07:30:00 -0700 --- -## graph coloring and linear programming -related: constraint satisfaction problems\ +## graph coloring -I recently had this idea for formatting graph coloring as an LP problem. -Say, I have a connected graph of three vertices: x1, x2, and x3. I want to find a color assignment that uses the least number of distinct colors such that no two adjacent vertices have the same color. - -My first idea was to have the constraints use absolute differencess: -|x1 - x2| >= 1 - -Then, I had a better idea: - -x1 ---- x2 - \ / - \ / - \ / - x3 - -# example.lp -``` -Minimize - obj: + x1 + x2 + x3 - -Subject To - c1: + x1 - x2 >= 1 - c2: + x1 - x3 >= 1 - c3: + x2 - x3 >= 1 - -Bounds - x1 >= 0 - x2 >= 0 - x3 >= 0 - -End - -``` -The best thing about it is that it worked with real values and still found and integer assignment - -x1 = 2 -x2 = 1 -x3 = 0 - -I will test this more rigorously though. I only tried the complete graph of 3 vertices. - - -``` -apt install glpk -glpsol -lp example.lp > example_output.txt -``` -Soon, I will try this for bigger graphs. - - -Also, here's my Welsh-Powell greedy coloring implementation. -``` +Welsh-Powell greedy coloring demo +```c++ #include #include #include @@ -145,3 +96,40 @@ int main() { cout << chromatic << endl; } ``` + +I had this idea for formatting graph coloring as an LP problem. Too bad it doesn't work. +Here's a connected graph of three vertices: x1, x2, and x3. +I want to find a color assignment that uses the least number of distinct colors such that no two adjacent vertices have the same color. + +``` +x1 ---- x2 + \ / + \ / + \ / + x3 +``` + +example.lp +``` +Minimize + obj: + x1 + x2 + x3 + +Subject To + c1: + x1 - x2 >= 1 + c2: + x1 - x3 >= 1 + c3: + x2 - x3 >= 1 + +Bounds + x1 >= 0 + x2 >= 0 + x3 >= 0 + +End + +``` + + +``` +apt install glpk +glpsol -lp example.lp > example_output.txt +``` diff --git a/_posts/2024-03-23-march23.markdown b/_posts/2024-03-23-march23.markdown new file mode 100644 index 0000000..b6f3685 --- /dev/null +++ b/_posts/2024-03-23-march23.markdown @@ -0,0 +1,45 @@ +--- +layout: post +title: "Codeforces Round 936 (Div. 2), problem: (C) Tree Cutting" +date: 2024-03-23 07:30:00 -0700 +--- +# C - Tree Cutting +educational dynamming programming contest\ +[Tree Cutting](https://codeforces.com/contest/1946/problem/C) + +The important thing to know is that if you can cut a tree into components whose size >= x, +then the same cut has components with size >= x - 1. + +The other thing to know is that k cuts produces k + 1 components. + +Using the first fact you can do binary search on the possible x values: [1 ... (n/(k+1) + 1)]. + +Why the upper bound of (n/(k+1) + 1)? +If you have a tree with n nodes which you want to split into k + 1 components. The biggest x could be is n/(k + 1). I added the 1 to account for when n is not divisible by k + 1. + +Binary search fixes the x value for you. Know you have to be able to check if you can cut the tree into k + 1 components with size >= x. + +You can greedily do this. Check subtrees in increasing order of their size (smaller subtrees first). If their size >= x, cut them off into a new component. + + +``` +int check(int u, int x, int& num_components) { + int cnt = 1; + + for (int v : adj[u]) { + if (v == parent[u]) continue; + + cnt += check(v, x, num_components); + } + + if (cnt >= x) { + ++num_components; + cnt = 0; + } + + return cnt; +} +``` + + +[my submission](https://codeforces.com/contest/1946/submission/252999559)