# Topcoder SRM 628 Div 2

Source: Internet
Author: User

I was dumbly crying ....

250-Point Problem

Two coordinates are given on the chess board. It takes at least a few steps to walk from one to the other.

The black box can only go through the black box, and the white box can only go through the white box. You only need to judge whether the two coordinates are the same color to determine whether they can be reached. Observing the checker, we can find that the parity of coordinates determines the color of the grid; A maximum of two steps can be achieved when the grid is reachable, so you only need to judge whether the grid is on the same diagonal line.

`# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm # define maxnusing namespace STD; class bishopmove {public: int howmanymoves (INT R1, int C1, int R2, int C2) {If (r1 = R2 & C1 = c2) return 0; if (R1 + C1) & 1) = (r2 + C2) & 1) {If (ABS (r1-r2) = ABS (c1-c2) return 1; return 2;} return-1 ;}}; int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r ", stdin); # endif // online_judge return 0 ;}`

500-Point Problem

Given a sequence of parentheses, X can be replaced by parentheses and asked if the sequence is valid. Because we didn't see that the number of X was no more than 5, we used the interval DP, and a subscript did not control the result re .... In fact, we can use DFS to find all the possible sequences, and then use the stack to judge. In the interval DP, we only need to find the minimum number of parentheses for this sequence to make it legal. If it is 0, it is legal, pay attention to the judgment of matching parentheses.

`# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm # define maxnusing namespace STD; class bracketexpressions {public: bool check (char X, char y) {If (x = '(' & Y = ') '| x =' ['& Y ='] '| x =' {'& Y ='} ') return true; if (x = 'X' & (y = ']' | Y = '}' | Y = ') '| Y = 'X') return true; if (y = 'X' & (x = '(' | x = '[' | x = '{') return true; return false;} string ifpossible (string Str) {int L = Str. size (); int DP  ; memset (DP, 0, sizeof DP); For (INT I = 0; I <L; I ++) {DP [I] [I] = 1 ;}for (int m = 1; m <L; m ++) {for (INT I = 0; I <L; I ++) {Int J = I + m; If (j> = L) break; DP [I] [J] = inf; If (check (STR [I], STR [J]) DP [I] [J] = min (DP [I] [J], DP [I + 1] [J-1]); for (int K = I; k <j; k ++) {DP [I] [J] = min (DP [I] [J], DP [I] [k] + dp [k + 1] [J]) ;}} if (DP  L-1] = 0) Return "possible "; else return "impossible" ;}}; int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin ); # endif // online_judge return 0 ;}`

1000-Point Problem

There is not enough time to finish the competition .....

Question:

Returns a set of n elements, E = {x | 0 <= x <n, X is an integer}, and a sequence of f = {Xi | 0 <= xi, I <n}: For the subset S of set E, any I belongs to S, and f [I] = xi also belongs to S.

Analysis:

This is actually a graph theory.

For each I, it corresponds to an F [I] = xi. If I take I, then I must also take F [I] = xi, that is, I depends on f [I] = xi. Therefore, a directed edge I ----> F [I] = xi is created. For each strongly connected component in the graph, all vertices in this component must be obtained at the same time. We can perform a strongly linked deflation point (Tarjan). Each point in the remaining Dag has two relationships: Take and not take. By applying the multiplication principle and addition principle, the final number of solutions is obtained through tree DP after reverse graph creation: For node u, the number of solutions for this node is multiplied by the number of solutions for all its subnodes, if this node is not selected, the number of solutions is 1.

`# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm 555555 # define maxn 5 5 using namespace STD; int FIR [maxn]; int U [maxm], V [maxm], NEX [maxm]; long W [maxn]; int N; int pre [maxn], low [maxn], sccno [maxn]; int st [maxn], top; int dfs_clock, scc_cnt; int deg [maxn]; void tarjan_dfs (INT _ u) {pre [_ u] = low [_ u] = ++ dfs_clock; ST [++ top] = _ u; for (int e = FIR [_ u]; e! =-1; E = NEX [e]) {int _ v = V [E]; If (pre [_ v] = 0) {tarjan_dfs (_ V ); low [_ u] = min (low [_ u], low [_ v]);} else {If (sccno [_ v] = 0) low [_ u] = min (low [_ u], pre [_ v]) ;}} if (low [_ u] = pre [_ u]) {scc_cnt ++; while (true) {int x = sT [top --]; sccno [x] = scc_cnt; If (x = _ u) Break ;}}} void find_scc () {dfs_clock = scc_cnt = 0; Top =-1; memset (sccno, 0, sizeof sccno); memset (PRE, 0, sizeof pre ); for (INT I = 0; I <n; I ++) {If (pre [I] = 0) Tarjan_dfs (I) ;}long long DFS (INT _ u) {long temp = 1; for (int e = FIR [_ u]; ~ E; E = NEX [e]) {temp * = DFS (V [e]);} return temp + 1;} class invariantsets {public: long long countsets (vector <int> F) {n = f. size (); memset (FIR,-1, sizeof fir); For (INT I = 0; I <n; I ++) {u [I] = I; V [I] = f [I]; NEX [I] = FIR [I]; FIR [I] = I;} find_scc (); memset (FIR,-1, sizeof fir); memset (deg, 0, sizeof deg); int e = 0; For (INT I = 0; I <n; I ++) {If (sccno [U [I] = sccno [V [I]) continue; U [e] = sccno [U [I]; V [e] = sccno [V [I]; swap (U [E], V [e]); NEX [e] = FIR [U [e]; deg [V [e] ++; FIR [U [e] = e ++;} For (INT I = 1; I <= scc_cnt; I ++) {If (deg [I] = 0) {u [e] = I; V [e] = 0; swap (U [e], V [e]); nex [e] = FIR [U [e]; FIR [U [e] = e ++;} return DFS (0)-1 ;}}; int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin); # endif/online_judge invariantsets; itn x, Y; while (~ Scanf ("% d", & X) {vector <int> V; For (INT I = 0; I <X; I ++) {scanf ("% d", & Y); V. push_back (y);} printf ("% LLD \ n",. countsets (v);} return 0 ;}`

Topcoder SRM 628 Div 2

Related Keywords:

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.