Nyoj 1078 tower of Hanoi (iv) [bipartite graph | regular | violent | greedy]

Source: Internet
Author: User
Tags cmath

Question: nyoj 1078 tower of legends (4)


Analysis: I found this question in graph theory. Then I read the question and pushed it. It seems a little regular and I tried it.


Then I checked the data and got 50, so I tried the simulation and passed it.

It seems that zoj has a question card simulation, and it must be greedy before it can pass through the simulation.


The intention of the author of this question is to test everyone's knowledge: The minimum path coverage of the Bipartite Graph.

Consider each ball as a vertex. If the sum of the two is the number of vertices, connect an edge between the two vertices and use a special Matching Algorithm Similar to the Hungary algorithm, however, if a matching EDGE connection is added to each search for a matching result, it cannot be matched. The final result is a maximum match. This topic should be pre-processed to offline at 50 points.

If a graph is created every time, the time complexity is very high, so we can use the TAG method. If the matching method is used, no matching is required. This can reduce the time, but it is still very long.


Find the regular code:

 #include <cstdio>int ans[55];void isit(){    ans[1]=1,ans[2]=3;    for(int i=3;i<51;i++)        ans[i]=ans[i-1]+(i+1)/2 * 2;}int main(){    int T,n;isit();    scanf("%d",&T);    for(;T--&&scanf("%d",&n);printf("%d\n",ans[n]));}        


Brute force code:

 #include <cstdio>#include <stack>#include <cmath>using namespace std;stack<int> st[60];int ans[60];void isit(){    int tmp=1,i=1;    while(i<55)    {        int ok=0;        for(i=1;!st[i].empty();i++)        {            int zhi = st[i].top()+tmp;            int sq=sqrt(zhi);            if(sq * sq == zhi)            {                st[i].push(tmp);                ok=1;                break;            }        }        if(!ok){            st[i].push(tmp);            ans[i]=tmp-1;        }        tmp++;    }}int main(){    int T,n;isit();    scanf("%d",&T);    for(;T--&&scanf("%d",&n);printf("%d\n",ans[n+1])){}}        


Binary Graph Matching code:

# Include <cstdio> # include <stack> # include <cmath> # include <cstring> using namespace STD; const int n = 60; const int M = 1500; int ans [N]; bool OK [M * 2 + 10]; bool MP [m] [m]; int MX [m], my [m]; // MX save forward edge my save reverse edge bool used [m]; // bool DFS (INT v) {for (INT u = 1; U <v; ++ U) // optimize if (MP [v] [u] &! Used [u]) {used [u] = true; If (my [u] =-1 | DFS (my [u]) {// keep searching for my [u] = V; MX [v] = u; return true;} return false;} int max_pi (INT ans, int N) // maximum match {for (INT I = 1; I <= N; I ++) {If (MX [I] <0) {memset (used, false, sizeof (used); If (DFS (I) ans ++;} return ans;} void min_road () // The minimum path overwrites {memset (MX,-1, sizeof (MX); // optimized memset (My,-1, sizeof (my); For (INT I = 1; I * I <= 2 * m; I ++) OK [I * I] = true; ans [1] = 1; int TMP = 0; For (INT I = 2; I <= m; I ++) {for (Int J = 1; j <I; j ++) if (OK [I + J]) MP [I] [J] = true; TMP = max_pi (TMP, I); // printf ("XX % d", I, TMP); ans [I-TMP] = I; if (I-TMP> 55) break;} // For (INT I = 1; I <= 50; I ++) // printf ("% d ", ans [I]);} int main () {int t, n; min_road (); scanf ("% d", & T); (; t -- & scanf ("% d", & N); printf ("% d \ n", ANS [N]) {}}


Nyoj 1078 tower of Hanoi (iv) [bipartite graph | regular | violent | greedy]

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.