August 8, 2019 (NOIP simulation game DAY2)

NOIP18年好难 T_T ~~~

prob1: Travel

60 points of sub-section will certainly do now is to see how the rest of the 40 minutes turned into 60 minutes of that situation.

That 40 minutes is that \ (n-m = \) , it is clear that the ring is a group the tree, according to the nature of the subject, must have an edge did not run, then enumeration edge, a puncturing section, then the method 60 minute run, to give answer.

Time complexity \ (O (^ n-2) \) :

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define in read()
#define fur(i,a,b) for(int i=a;i<=b;i++)
#define jinitaimei signed
inline int read()
{
    int x=0;
    char ch=getchar();
    for(;!isalnum(ch);ch=getchar());
    for(;isalnum(ch);ch=getchar()) x=x*10+ch-'0';
    return x;
}
const int xx=5e3+10;
vector <int> e[xx];
bool vis[xx],viss[xx],to[xx];
int n,m,pre[xx],delx,dely,u[xx],v[xx],ans[xx],tmp[xx],all=0;
inline void dfs1(int g)
{
    vis[g]=true;
    printf("%d ",g);
    int tp;
    do
    {
        tp=6e3;
        fur(i,0,(int)e[g].size()-1)
        {
            if(vis[e[g][i]]) continue;
            tp=min(tp,e[g][i]);
        }
        if(tp<6e3) dfs1(tp);
    }while(tp<6e3);
}
inline void dfs2(int g)
{
    vis[g]=true;
    tmp[++all]=g;
    fur(i,0,(int)e[g].size()-1)
    {
        if(vis[e[g][i]]) continue;
        if(e[g][i]==delx&&g==dely) continue;
        if(e[g][i]==dely&&g==delx) continue;
        dfs2(e[g][i]);
    }
}
jinitaimei main()
{
    n=in;m=in;
    fur(i,1,m)
    {
        u[i]=in,v[i]=in;
        e[u[i]].push_back(v[i]);
        e[v[i]].push_back(u[i]);
    }
    if(n-1==m) dfs1(1);
    else
    {
        if(n==5000) m=m*2/3;
        fur(i,1,n) sort(e[i].begin(),e[i].end()),ans[i]=6e3;
        fur(i,1,m)
        {
            delx=u[i];
            dely=v[i];
            all=0;
            fur(j,1,n) vis[j]=false;
            dfs2(1);
            bool change=false;
            if(all<n) continue;
            fur(j,1,n)
            {
                if(ans[j]!=tmp[j])
                {
                    change=ans[j]>tmp[j];
                    break;
                }
            }
            if(change&&all==n) fur(j,1,n) ans[j]=tmp[j];
        }
        fur(j,1,n) printf("%d ",ans[j]);
    }
    printf("\n");
    return 0;
}  

Guess you like

Origin www.cnblogs.com/ALANALLEN21LOVE28/p/11330522.html