[Reserved] $ CF290F $ explanations

Read the original

This question from \ (codeforces \) \ (2013 \) on April Fool's Day game \ (F \) title. (Also translated the words of this problem I wrote it \ (HHHH \) )

Required to find a title \ (n-\) points \ (m \) free edges of the graph \ (Hamiltonian \) path.

However, the reason why it can become a grand finale April Fool's Day game, because it's algorithm can not create out of nothing, but needs recovery \ (Petya \) program written.

Examples given in \ (Yes \) \ (or \) \ (No \) are also in \ (Petya \) program written to run under the result, not whether in fact there is \ (Hamilton \ ) path. (This is from a sample \ (3 \) should be able to see it on)

Because I am too weak, too little seen the algorithm can not observe a \ (Petya \) algorithm, only with
the topic and people Gerald (sometimes open slower)Saying little brother pretty niceLet the game of total blog topic written by (sometimes open slower) to obtain \ (Petya \) algorithm.

To save everyone's time, \ (Petya \) algorithm (a well-knownerrorGreedy method) as follows:

1) None of the adjacency list (delete all sides to the loopback FIG storage and heavy).

2) in ascending order (sort order of input equal degrees) according to the degrees points.

3) to try all possible paths starting point, and try to move the abutment table in the corresponding order from the current point to the access point yet.

4) If successfully found a \ (Hamilton \) path, the output \ (Yes \) , otherwise the output \ (No \) .

So, we can begin to restore \ (Petya \) algorithm matter, total time complexity is \ (O (the n-^ 3) \) .

If you have questions, comments section or private messages can be, I will reply as soon as possible.

code show as below:

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,m,x,y,deg[25];
bool edge[25][25],vis[25];
int main()
{
    n=read();
    m=read();
    for(register int i=1;i<=m;i++) 
    {
        x=read();
        y=read();
        if(x==y)
            continue;
        if(edge[x][y])
            continue;
        deg[x]++;
        deg[y]++;
        edge[x][y]=1;
        edge[y][x]=1;
    }
    for(register int i=1;i<=n;i++)
    {
        int st=i,j;
        memset(vis,0,sizeof(vis));
        vis[st]=1;
        for(j=1;j<n;j++)
        {
            int minn=0,de=0;
            for(register int k=1;k<=n;k++)
            {
                if(edge[st][k]&&!vis[k])
                {
                    if(de==0||minn>deg[k])
                    {
                        de=k;
                        minn=deg[k];
                    }
                }
            }
            if(de==0)
                break;
            st=de;
            vis[st]=1;
        }
        if(j==n) 
        {
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
    return 0;
}

Guess you like

Origin www.cnblogs.com/Peter0701/p/11261173.html