Problem Statement
Bob is going to create a graph with N nodes. The graph will be constructed in two steps. First, Bob will take N isolated vertices, label them 1 through N and color each of them using one of K colors. Then, Bob will add some directed edges to the graph. For each i between 2 and N, inclusive, Bob may choose a single value j < i such that the nodes i and j have different colors. If he does, he will add the edge from i to j to his graph. Note that Bob may choose not to add any edge from node i, even if there are some valid choices of j.
Two graphs are considered the same if they have the same node colors and the same set of edges.
You are given the ints N and K. Compute and return the number of different graphs Bob may construct, modulo 1,000,000,007.
Definition
- ClassColorfulLineGraphsDiv2
- MethodcountWays
- Parametersint , int
- Returnsint
- Method signatureint countWays(int N, int K)
Limits
- Time limit (s)2.000
- Memory limit (MB)256
Constraints
- N will be between 1 and 100, inclusive.
- K will be between 1 and 3, inclusive.
Test cases
-
- N3
- K2
Returns 24 -
- N15
- K2
Returns 789741546 -
- N100
- K1
Returns 1 -
- N1
- K3
Returns 3 -
- N100
- K3
Returns492594064
- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
There can be at most 3 colors. So how about we solve for 3 colors, then we can adapt the solution for fewer number of colors with small tweaks.
The problem statement describes the decision as first choosing colors and then creating the edges. But the actual rules for counting the unique setups combines the two together. Let's try to make the decision about picking a color AND an edge for each vertex.
Imagine we've already picked colors and edges for the first i nodes, let's decide the i-th node. We can pick one of 3 colors:
- Pick color A. Now we have to decide the edge. We can choose to connect vertex i to any of the previous vertices as long as they have a different color. Since this is a counting problem, let's keep in mind the counts for each color (assume 3 colors) : a, b and c. We cannot connect to color a, so there are b+c vertices we can connect this vertex to. There is an additional option: Don't connect the vertex at all. This gives b+c+1 options. Note that later vertices don't need to know what edge we picked, only the color.
- If we pick color B: There are 1+a+c options.
- 1+a+b options in case color C is picked.
The idea is that we can just increment the count of the respective color and move on to the next i. Also note that i=a+b+c, because there were i vertices so the sum of all colors must be i. This helps us do a dynamic programming solution. Let's name f(a,b,c) to the number of ways to pick colors and edges for vertices starting from i=a+b+c onwards, assuming there were a, b and c vertices of each color among the first i vertices.
- Base case: a+b+c=N. This means that we have already made a decision for all the vertices and there is nothing else to do. One way to do nothing: The result is 1.
- Else there are 3 options:
- We can pick color A. This means (1+b+c) options for the edge. The number of A vertices is incremented. Between picking the edge and picking the next colors/edges we have two independent decisions, so we multiply: (1+b+c)⋅f(a+1,b,c).
- Or pick color B: (1+a+c)⋅f(a,b+1,c).
- Or C: (1+a+b)⋅f(a,b,c+1).
Fewer colors
When there are two colors, we can just disable the part where we pick color C. The state can still be (a,b,c), but c will always be 0.
When there is one color, we can just return the number of graphs of size N that follow this rule OR we can do the same trick as for K=2 and just disable the part where we decide to use color B and C.
Code
The idea with that recurrence relation is that it is acyclic and the number of states is not very large O(n3). We can implement the recurrence using memoization or iteratively (dynamic programming) to guarantee O(n3) run time.
const int MOD = 1000000007; int dp[101][101][101]; int N, K; int f(int a, int b, int c){ int & res = dp[a][b][c]; if (res == -1) { if (a + b + c == N) { // the end res = 1; } else { res = 0; long p, q; // color vertex with color a p = 1 + b + c; q = f(a + 1, b, c); res += (int)( (p * q) % MOD ); res %= MOD; // color vertex with color b if (K >= 2) { p = 1 + a + c; q = f(a, b + 1, c); res += (int)( (p * q) % MOD ); res %= MOD; } // color vertex with color c if (K >= 3) { p = 1 + a + b; q = f(a, b, c + 1); res += (int)( (p * q) % MOD ); res %= MOD; } } } return res;} int countWays(int N, int K){ this->N = N; this->K = K; memset(dp, -1, sizeof(dp)); return f(0,0,0);}
The 24 different graphs are shown below. In each picture, the vertices have labels 1, 2, 3 from the left to the right.