• 欧拉回路/路径——2020-camp-day2-H


    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int M =2e6+7;
    int head[M],cnt;
    struct EDGE{int to,nxt;}ee[M*2];
    void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
    int st[M],ans[M];
    bool vis[M];
    ll top,t;
    ll len;
    void euler()
    {
        st[++top]=1;
        while(top>0)
        {
            int x=st[top],i=head[x];
            while(i&&vis[i])i=ee[i].nxt;
            if(i)
            {
                st[++top]=ee[i].to;
                vis[i]=vis[i^1]=true;
                head[x]=ee[i].nxt;
            }
            else 
            {
                top--;
                ans[++t]=x;
            }
        }
    }
    void gao(ll n,ll m)
    {
        printf("%lld
    ",n);
        cnt=1;
        for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
        add(i,j),add(j,i);
        if(n%2==0)
        {
            for(int i=1;i<=n;i+=2)
            add(i,i+1),add(i+1,i);
        }
        euler();
        int tt=0;
        for(int i=t;i>2;i--)printf("%d ",ans[i]),tt++;
        if(tt==len-1)
            printf("%d
    ",ans[2]);
        else
        {
            printf("%d ",ans[2]);tt++;
            for(int i=tt+1;i<len;i++)printf("1 ");
            printf("1
    ");
        }
    }
    
    bool ck(ll x)
    {
        ll nm=0;
        if(x&1)nm=x*(x-1)/2;
        else nm=x*(x-1)/2+x/2-1;
    //    printf("===   %lld    %lld  %lld
    ",x,nm,len);
        if(nm<len)return true;
        else return false;
    }
    int main()
    {
        scanf("%lld",&len);
        if(len==1)
        {
            puts("1");
            puts("1");
            return 0;
        }
        ll L=1,R=1500000000,n=0;//图上点数的界限
        while(L<=R)
        {
        //    printf("%lld  %lld
    ",L,R);
            ll mid=(L+R)/2;
            if(ck(mid))n=mid,L=mid+1;
            else R=mid-1;
        }
    //    cout<<n<<"--  "<<endl;
        ll m=(n&1)?(n*(n-1)/2):(n*(n-1)/2+n/2-1);
        if(len>(ll)2e6){
            printf("%lld
    ",n);
            return 0;
        }
        gao(n,m);
    //    else cout<<n<<"  "<<m<<endl;
        //n个点 m条边 
        return 0;
        
    }
  • 相关阅读:
    POJ1700 Crossing River
    Glad to see you! CodeForces
    Summer sell-off CodeForces
    atcoderI
    Selling Souvenirs CodeForces
    Array Division CodeForces
    Tea Party CodeForces
    Atcoder F
    Atcoder E
    Average Sleep Time CodeForces
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12197497.html
Copyright © 2020-2023  润新知