博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DP SRM 661 Div2 Hard: ColorfulLineGraphsDiv2
阅读量:5747 次
发布时间:2019-06-18

本文共 5174 字,大约阅读时间需要 17 分钟。

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)
(be sure your method is public)

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

    The 24 different graphs are shown below. In each picture, the vertices have labels 1, 2, 3 from the left to the right.

    • N15
    • K2
    Returns
    789741546
    • N100
    • K1
    Returns
    1
    • N1
    • K3
    Returns
    3
    • N100
    • K3
    Returns

    492594064

  1. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    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) : ab 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 ab 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).
      The addition of these 3 values is the result for f(a,b,c).

    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);}

转载于:https://www.cnblogs.com/gavanwanggw/p/6727776.html

你可能感兴趣的文章
源码包、二进制包
查看>>
公司新年第一次全员大会小记
查看>>
最懒的程序员
查看>>
了解Amdahl定理,该定理再多核时代有怎样的影响?
查看>>
JAVA8 Stream 浅析
查看>>
inner join on, left join on, right join on要详细点的介绍
查看>>
文件名智能排序(按照数字大小排序)
查看>>
jre、jdk版本的选择
查看>>
SQL查询语句
查看>>
VIM文本编辑器
查看>>
SAS vs SSD对比测试MySQL tpch性能
查看>>
流言揭秘:吃黑巧克力就不发胖?
查看>>
Spring boot 整合CXF webservice 全部被拦截的问题
查看>>
深度学习如何入门
查看>>
Tcp协议三次握手抓包分析
查看>>
关于JavaProcess的一些笔记
查看>>
Pinpoint跨节点统计失败
查看>>
Hive体系结构
查看>>
strace
查看>>
时间戳转换为时间(不为1970)
查看>>