Gym 100962E Elvis Presley—— 读题,深搜

82 篇文章 1 订阅

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值