Bestcoder Round #53 (hdu 5422&5423&5424)

Source: Internet
Author: User

HDU 5422:

Test instructions: Given an n-vertex, M-bar-changing graph, now lets you add an edge that minimizes the shortest possible 1 to N, and, in the shortest case, writes out the different number of edges that can be added.

Idea: Very simple, two cases: 1. If there is no edge between 1 and N, then the edge we add is definitely 1~n, so the shortest circuit must be 1, the method is only one; 2. If there is an existing edge between 1 and N, then we will be able to connect two vertices at random, by N (n-1)/2.

#include <iostream> #include <string> #include <cstring> #include <iostream>using namespace std ; int G[200][200];int Main () {    int n,m,a,b;    while (cin>>n>>m)    {        memset (g,0,sizeof (g));        for (int i=1;i<=m;i++)        {            cin>>a>>b;            g[a][b]=g[b][a]=1;        }        if (g[1][n]==0)        cout<<1<< "" <<1<<endl;        else        cout<<1<< "" << (n-1) *n/2<<endl;    }    
HDU 5423
Test instructions: give you a tree and ask if you are a "special tree". The definition of a particular tree is: there is no other tree (inconsistent with the father of the knot) and he is similar (the distance from each point to the root remains the same).

After drawing analysis, we can see that in addition to the last layer of the node can be >=1, the other layer nodes must be only one, otherwise the same layer of nodes can be exchanged, for example:
1 1

2 3 and 2 3 do not meet the requirements of a particular tree, D FS then uses the map to record the number of nodes in each layer.

4 5 5 4

#include <iostream> #include <string> #include <cstring> #include <iostream> #include <map > #include <set>using namespace Std;int g[1003][1003];int vis[1005];map<int,int> MP; int n,a,b;void dfs (int begina,int sum) {mp[sum]++;for (int i=1;i<=n;i++) {if (G[begina][i]&&!vis[i]) {vis[i]= 1;dfs (i,sum+1);}}} int main () {     while (cin>>n)    {    mp.clear ();    memset (G,0,sizeof (g));    memset (vis,0,sizeof (Vis));    for (int i=1;i<n;i++)    {    cin>>a>>b;    g[a][b]=g[b][a]=1;    }    Vis[1]=1;    DFS (1,0);    Map<int,int>::iterator it;    BOOL Flag=true;    For (It=mp.begin (); It!=mp.end (); ++it)    {    if (it->second>1&&it->first!=mp.size ()-1)    {    flag=false;    break;    }    }    if (flag)    cout<< "YES" <<endl;    else    cout<< "NO" <<endl;    

HDU 5424:

Test instructions: give n points, n edges, and ask if you exist on Hamilton Road (starting from a point, passing every point once and only once)

Idea: First, if we do DFS for each point as a starting point, we find that if we can compose Hamilton Road, then we will use the n-1 edge, the remaining side has four kinds of cases:
1. Forming a self-ring

2. Connect the end and end so that each point has a degree of 2

3. Connect the other two points, so that the first and last degree is 1, we randomly find a starting point Dfs can be.

4. Connect the one and the other points in the end, or there will be a point with a degree of 1, DFS can

#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int n,x,y,deg[1001];bool    flag,vis[1001],map[1001][1001];void dfs (int u,int sum) {if (flag) return;    if (sum==n) {Flag=1;return;}            for (int i=1;i<=n;++i) if (!vis[i]&&map[u][i]) {vis[i]=1;            DFS (I,SUM+1);        vis[i]=0;        }}int Main () {while (~SCANF ("%d", &n) {flag=0;        memset (deg,0,sizeof (deg));        memset (vis,0,sizeof (VIS));        memset (map,0,sizeof (MAP));            for (int i=0;i<n;++i) {scanf ("%d%d", &x,&y); if (x!=y&&!                Map[x][y]) {map[x][y]=map[y][x]=1;            ++deg[x],++deg[y];        }} int begina=1,tot=0;        for (int i=1;i<=n;++i) {if (deg[i]==1) {begina=i;        ++tot;        }} if (Tot>2) {puts ("NO"); continue;}        Vis[begina]=1;      DFS (begina,1);  if (flag) puts ("YES");    Else puts ("NO"); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bestcoder Round #53 (hdu 5422&5423&5424)

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.