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

本文共 5119 字,大约阅读时间需要 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);}

转载地址:http://kilml.baihongyu.com/

你可能感兴趣的文章
php汉字转换拼音插件,PHP将汉字转换拼音_PHP教程
查看>>
php幂等性,通过Ansible安装PHP Pear包,具有幂等性
查看>>
php实现决策树模型,通俗地说决策树算法(二)实例解析
查看>>
fck.php,将FCKeditor导入PHP+SMARTY的实现方法
查看>>
php isnotset,如何解决微信小程序报错:this.setData is not a function的问题
查看>>
linux命令grep搜索倒序输出,Linux下的搜索命令grep(转)
查看>>
lora 网关 linux,什么是LoRa网关 如何选择一个好的LoRaWAN网关
查看>>
android工程换背景图片,Android ImageView图片显示点击背景切换
查看>>
html中加入js有什么用,css和js后加问号和数字有什么用?
查看>>
html dom的nodetype值介绍,HTML DOM nodeType用法及代码示例
查看>>
html怎么对多个td应用样式,html – 如何将样式类应用于td类?
查看>>
Proxmox集群ceph报“ceph 1pg inconsistent”错误解决备忘
查看>>
多级菜单系统安装维护shell脚本实现企业级案例
查看>>
那些年,我玩过的操作系统
查看>>
Lync Server 2013标准版升级Skype for Business Server 2015实战(上)
查看>>
新浪、万网前系统架构师高俊峰:统一监控报警平台架构设计思路
查看>>
2011-9-25俱乐部活动
查看>>
JMeter正则表达式提取器
查看>>
Nginx
查看>>
How To Enable‘root’Account Login Solaris 11 Directly
查看>>