1. 程式人生 > >Gym 100962E Elvis Presley—— 讀題,深搜

Gym 100962E Elvis Presley—— 讀題,深搜

Consider a possibly infinite directed graph G = (V, E) without loops (that is, for each (u, v) ∈ E,
u ̸= v). Let’s define its transitive closure G+ = (V, E+) as the directed graph having the same set
of vertices V such that its edges are all pairs (u, v) for which u ̸= v and there exists a finite path
u = p0, p1, p2, . . . , pk−1, pk = v such that (pi−1, pi) ∈ E for all i = 1, 2, . . . , k.
An antichain is a (possibly infinite) set A ⊆ V such that, for any two distinct vertices u, v ∈ A,
none of the two edges (u, v) and (v, u) are present in G+.
Consider a set U and some property P defined for all its subsets (an example of a property defined
for each subset A ⊆ V is whether A is an antichain of the graph G = (V, E)). We say that subset
S ⊆ U is a maximum subset satisfying property P if its size |S| (that is possibly equal to ∞)
is maximum possible. We say that S ⊆ U is a maximal subset satisfying property P if there
exists no other subset T satisfying P such that S ⊊ T, that is, S can’t be extended to become
a larger subset satisfying the same property. Note that those two definitions are different: if a
maximum subset satisfying P exists and it is finite, it is obviously also maximal, but a maximal
subset satisfying P may not be maximum.
We define minimal and minimum subsets satisfying some property P in a similar manner.
Let’s define an EP-graph: EP = (VEP , EEP ) such that its vertices are VEP = N = {1, 2, 3, . . .}
and its edges are EEP = {(v, 2v) : v ∈ N} ∪ {(v, 2v + 1) : v ∈ N}.
You are given two distinct vertices a and b of V . Find a minimum maximal antichain in EP
containing both a and b, or determine that there are no such antichains. If there are several
possible answers, find any of them.
More formally:
AC = {A ⊆ VEP : A is an antichain in EP}.
MaxAC = {A ∈ AC : A is maximal}.
MinMaxAC = {A ∈ MaxAC : |A| = min
S∈MaxAC
|S|}.
Your task is to find any element of MinMaxAC .
Input
The first line of input contains two integers a and b (1 ≤ a, b ≤ 109
, a ̸= b).
Output
If there exists no minimum maximal antichain in EP containing both a and b, or if it is infinite,
print −1. Otherwise, print elements of any minimum maximal antichain in EP containing both a
and b in ascending order.

Examples
standard input standard output
2 7 2 6 7
1 2 -1

題意:

這道題目題意太難懂了,我看了好久愣是沒看出什麼,補題的時候聽別人講的,就是給你一個二叉樹,無限大的二叉樹,每次給你兩個點,如果兩個點有任何一個是另一個的父輩,輸出-1,否則輸出從他們到lca,再從lca到根節點的所有節點不在路徑上的兒子節點,在輸出他們本身。比如你有一個12和7,那麼就要輸出2,13,12,7

題解:

第一個dfs搜的是他們能不能再回溯父親的時候找到另一個點,同時記錄路徑上經過的所有節點,在第二個dfs搜的是路徑上所有節點的兒子並且這個兒子在剛才沒被訪問過。

#include<bits/stdc++.h>
using namespace std;
map<int,bool>vis;
set<int>st;
int flag;
void dfs(int x,int y)
{
    if(flag)
        return ;
    if(x==y)
    {
        flag=1;
        return ;
    }
    vis[x]=1;
    if(x==1)
        return ;
    dfs(x>>1,y);
}
void finds(int x)
{
    if(vis[x]==0)
    {
        st.insert(x);
        return ;
    }
    finds(x<<1);
    finds(x<<1|1);
}
int main()
{
    int l,r;
    while(~scanf("%d%d",&l,&r))
    {
        vis.clear();
        st.clear();
        flag=0;
        dfs(l,r),dfs(r,l);
        if(flag)
        {
            printf("-1\n");
            continue;
        }
        vis[l]=vis[r]=0;
        finds(1);
        for(set<int>::iterator it=st.begin();it!=st.end();it++)
            printf("%d ",*it);
    }
    return 0;
}