Tour route - disjoint-set, dfs

Title Description

Lambdaland consisting of N cities, there is a road between the two cities are connected either.  
TBL ready next month to visit Lambdaland. He will start from 1 city to visit the depth-first search order to be able to traverse all of the city. Since TBL is a very important figure, terrorists eyeing him, and blew up the M road before he set out.  
Now terrorists to hire you to write a program to determine the tour route of TBL. If more than minimal solution, dictionary output sequence.

20% score, N≤1000, M≤50000.
50% score, N≤30000, M≤800000.
100% score, N≤100000, M≤1000000. 
Each city most visited once, each road can be blown up many times.

Thinking

  Violence dfs, with a disjoint-set to achieve consecutive points to reduce visited for an edge $ (u, i) $, represents $ u, i $ a direct road was destroyed. Every time we visit a node U $ $, to sort out the side, to open a pointer which indicates a go, jump to the current minimum lexicographic order with DSU, ran no point, then the current point is determined lower_bound can not run after run directly dfs return

code

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
vector<int>g[N];
int n,m;
int father[N*10];
void init(int x){for(int i=1;i<=x;i++)father[i]=i;}
int find(int x){return x==father[x]?x:father[x]=find(father[x]);}
bool vis[N];
 
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
inline void dfs(int u)
{
    printf("%d\n",u);
    vis[u]=1;
    father[u]=u+1;
    int next=find(1);
    sort(g[u].begin(),g[u].end());
    while(next<=n)
    {
        if(!vis[next])
        {
            int tmp=lower_bound(g[u].begin(),g[u].end(),next)-g[u].begin();
            if(tmp>=g[u].size()||next!=g[u][tmp])
            {dfs(next);return;}
        }next=find(next+1);
    }
}
     
 
int main()
{
    n=read();m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        g[x].push_back(y);
        g[y].push_back(x);
    }
    init(N);
    dfs(1);
}

 

Guess you like

Origin www.cnblogs.com/THRANDUil/p/11563752.html