Codeforces 888F, codeforces888f

Source: Internet
Author: User

Codeforces 888F, codeforces888f

Problem Link:

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

 

Analysis:

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

The first observation we coshould made is that if we connectVertex IAndJDirectly, 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 fromLToR, I. e., connectingVertex L, l + 1 ,..., R-1,AndR,Vertex lMust directly connect to one or more of the vertices in the interval fromL + 1ToR, Since the all vertices must be connected. We can split the situation into different cases, each case in which the last vertex in the intervalVertex lConnects to is different. Notice that if the last vertexVertex lConnects to isVertex mIn the interval, it does not follow thatVertex lWill not connect to any vertices otherVertex M, But what it really means is that all the other vertices thatVertex lCan connect to must come beforeVertex mIn 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 verticesVertex lAndVertex mToVertex lOrVertex mAnd the ways of connecting verticesVertex m(Aggressive) andVertex r(Random SIVE). The special case here is the case where we connectVertex lAndVertex rDirectly, 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 intervalVertex lAnd the right part of the intervalVertex r.

 

The DP equations:

If we store the total number of ways of connecting vertices in interval fromVertex lToVertex rInDp1 [l] [r], And the ways whenVertex lAndVertex rAre directly connected inDp2 [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]);

 

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 <ioman Ip> 21 # include <unordered_set> 22 # include <unordered_map> 23 using namespace std; 24 25 typedef 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 ways32 ll dp2 [505] [505]; // stands for ways to directly connect33 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.