zoukankan      html  css  js  c++  java
  • UESTC 1960 咸鱼自画像 构造哈密顿通路

    题目:http://www.qscoj.cn/#/problem/show/1960

    每两个点之间都有只有一条有向边的图是竞赛图。

    定理: 
    竞赛图一定存在哈密顿路径 
    竞赛图存在哈密顿回路 充要条件是强连通。

    构造方法一共3种

    加到头

    加到尾

    插到中间

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    using namespace std;
    int a[1005][1005];
    char s[1005];
    int nt[1005];
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++)
            {
                scanf("%s",s);
                for(int j=0;j<n;j++)
                    if (s[j]=='+') a[i][j]=1;
            }
            memset(nt,-1,sizeof(nt));
            int head=0;
            int tail=1;
            if (a[1][0]) swap(head,tail);
            nt[head]=tail;
            for(int i=2;i<n;i++)
            {
                if (a[i][head])
                {
                    nt[i]=head;
                    head=i;
                }
                else if (a[tail][i])
                {
                    nt[tail]=i;
                    tail=i;
                }
                else
                {
                    int now=head;
                    while(nt[now]!=-1)
                    {
                        if (a[now][i]&&a[i][nt[now]])
                        {
                            nt[i]=nt[now];
                            nt[now]=i;
                            break;
                        }
                        now=nt[now];
                    }
                }
            }
            printf("YES
    ");
            printf("%d",head);
            for(int i=nt[head];i!=-1;i=nt[i])
                printf(" %d",i);
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    html实现文件夹的上传和下载
    JQuery & Javascript
    JSP Scripting Element
    JSP Filter
    Oct22 实例测试
    JSP HTML error code
    Implicit Object in JSP
    JSP action elements
    JSP lifecycle
    Eclipse 配置Tomcat
  • 原文地址:https://www.cnblogs.com/bk-201/p/9306836.html
Copyright © 2011-2022 走看看