Codeforces 888F - Connecting Vertices

来源:https://www.cnblogs.com/ShakuganSky/archive/2018/02/20/8454747.html
-Advertisement-
Play Games

Problem Link: http://codeforces.com/problemset/problem/888/F Problem Statement: There are n points marked on the plane. The points are situated in suc ...


 

Problem Link:

 

http://codeforces.com/problemset/problem/888/F

 


 

Problem Statement:

 

There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).

But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).

How many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109 + 7.

 

Input

The first line contains one number n (3 ≤ n ≤ 500) — the number of marked points.

Then n lines follow, each containing n elements. ai, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j= 0). It is guaranteed that for any pair of points ai, j= aj, i, and for any point ai, i= 0.

 

Output

Print the number of ways to connect points modulo 109 + 7.

 


 

Analysis:

 

The difficulty here is how to avoid double counting and how to store dp values.

The first observation we could made is that if we connect vertex i and j directly, the rest vertices are split into two parts, and vertices in each part can only connect to vertices in their own parts. This fact reminds us of the classical way of dynamic programming, i.e., the number of ways to connect vertices on a large interval is based on the number of ways to connect vertices on smaller intervals.

Next, how can we avoid double counting? We need to see some characteristics of the way of connecting the vertices on some interval. If we are to connect the vertices on the interval from l to r, i.e., connecting vertex l, l+1, …, r-1, and r, vertex l must directly connect to one or more of the vertices in the interval from l+1 to r, since the all vertices must be connected. We can split the situation into different cases, each case in which the last vertex in the interval vertex l connects to is different. Notice that if the last vertex vertex l connects to is vertex m in the interval, it does not follow that vertex l will not connect to any vertices other than vertex m, but what it really means is that all the other vertices that vertex l can connect to must come before vertex m in this interval. Then we can make the following claim: counting the ways of connecting vertices in the interval from different cases won’t count any distinct way of connecting the vertices more than once, and this is clear, as we have made the first observation. To handle the cases, we just need to calculate the ways of connecting vertices between vertex l and vertex m to vertex l or vertex m and the ways of connecting vertices between vertex m (inclusive) and vertex r (inclusive). The special case here is the case where we connect vertex l and vertex r directly, and we can handle this case by splitting the interval differently by iterating on the split point, and then connect the left part of the interval to vertex l and the right part of the interval to vertex r.

 


 

The DP Equations:

 

If we store the total number of ways of connecting vertices in interval from vertex l to vertex r in dp1[l][r], and the ways when vertex l and vertex r are directly connected in dp2[l][r], we can write the following equations:

dp2[l][r] = if(l and r can be connected) sum(dp1[l][m] * dp1[m+1][r]);

dp1[l][r] = sum(dp2[l][m] * dp1[m][r]);

 


 

Time Complexity:

 

O(n3)

 


 

AC Code:

 

 1 #include <iostream>
 2 #include <sstream>
 3 #include <fstream>
 4 #include <string>
 5 #include <vector>
 6 #include <deque>
 7 #include <queue>
 8 #include <stack>
 9 #include <set>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 #include <utility>
14 #include <bitset>
15 #include <cmath>
16 #include <cstdlib>
17 #include <ctime>
18 #include <cstdio>
19 #include <memory.h>
20 #include <iomanip>
21 #include <unordered_set>
22 #include <unordered_map>
23 using namespace std;
24 
25 typedef long long ll;
26 
27 const ll md=1e9+7;
28 
29 int n;
30 int a[505][505];
31 ll dp1[505][505]; //stands for total ways
32 ll dp2[505][505]; //stands for ways to directly connect
33 
34 int main(){
35     scanf("%d",&n);
36     for(int i=0;i<n;i++){
37         for(int j=0;j<n;j++){
38             scanf("%d",&a[i][j]);
39         }
40     }
41     for(int i=0;i<n;i++){
42         dp1[i][i]=dp2[i][i]=1;
43     }
44     for(int i=1;i<n;i++){
45         for(int x=0,y=i;x<n;x++,y=(y+1)%n){
46             if(a[x][y]){
47                 for(int k=x;k!=y;k=(k+1)%n){
48                     dp2[x][y]+=dp1[x][k]*dp1[(k+1)%n][y];
49                     dp2[x][y]%=md;
50                 }
51             }
52             for(int k=x;k!=y;k=(k+1)%n){
53                 dp1[x][y]+=dp2[x][(k+1)%n]*dp1[(k+1)%n][y];
54                 dp1[x][y]%=md;
55             }
56         } 
57     }
58     printf("%I64d\n",dp1[0][n-1]);
59 }
View Code

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 網路知識 網路配置 ...
  • 一.官網下載地址:https://dev.mysql.com/downloads/mysql/ 1.選擇對應版本,下載免安裝版: 2.不要註冊賬號,點擊“No thanks,just start my download”: 3.下載到本地後直接解壓: 二.開始安裝 1.在D盤(放在哪個盤隨個人喜好) ...
  • 先介紹一下《MySQL資料庫開發的三十六條軍規》,這裡只介紹核心的,具體內容大家可以自行百度,這是從底層開發人員到管理者必須知道規範。出自58趕集。 介紹兩個例子。這個適合用STAR法則。STAR法則是情境(situation)、任務(task)、行動(action)、結論(result)四項的縮寫 ...
  • 首先,原標題是對那些只知道玩的成人說的。 Die With Me ========== Die With Me是一個超級無聊的比列時程式員開發的IOS的APP,有關這個APP大家可以自行 "百度" 。 不少人(包括我)都是通過"躺倒鴨"知道的DWM,我是一初二學生,買不起IPhone,用國產Andr ...
  • js
    1、js:JavaScript一種直譯式腳本語言(解釋型腳本語言,執行前不需要編譯;這一點和Java類似,Java也是解釋型語言,源碼變為位元組碼(jvm可執行的代碼)的過程不是編譯過程),是一種動態類型、弱類型、基於原型的語言,內置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分 ...
  • timeChunk函數讓創建節點的工作分批進行,比如一秒鐘創建1000個節點,改為每個200ms創建10個節點。具體timeChunk函數封裝如下 應用實例見https://92node.com/article/js-fen-shi.html ...
  • 上一篇聊了聊構建分散式系統所面臨的困難,這篇將著重討論構建容錯分散式系統的演算法與協議。構建容錯系統的最佳方法是使用通用抽象,允許應用程式忽略分散式系統中的一些問題。本篇我們先聊一聊線性一致性,以及與線性一致性有關的技術,後續需要瞭解的分散式協調服務,如:ZooKeeper等,都是基於分散式系統的線性 ...
  • 本文簡要地示範瞭如何使用java CardLayout對程式進行佈局。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...