【CF1044B Intersecting Subtrees】题解
题目链接
题目
这是一道交互题
你和正在玩一个奇怪的游戏。给出一棵个点的树,双方分别给顶点编号为到,双方都不知道对方给树编号的方式。
接着双方在自己对应的树上选择一个联通子图,在你的编号方式对应的树上你选择了,在的编号方式对应的树上选择了,双方都知道与的值
现在你想知道两个子图是否存在至少一个公共点。你可以进行询问,问题有以下两种:
:得到在你的编号方式下的号点在的编号方式下的值
:得到在的编号方式下的号点在你的编号方式下的值
现在请你使用不多于次询问得出是否存在公共点,或者确定两棵子树没有公共点。
You are playing a strange game with Li Chen. You have a tree with nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from to . Neither of you know the other's labelling of the tree.
You and Li Chen each chose a subtree (i.e., a connected subgraph) in that tree. Your subtree consists of the vertices labeled in your labeling, Li Chen's subtree consists of the vertices labeled in his labeling. The values of and are known to both of you.
The picture shows two labelings of a possible tree: yours on the left and Li Chen's on the right. The selected trees are highlighted. There are two common nodes.You want to determine whether your subtrees have at least one common vertex. Luckily, your friend Andrew knows both labelings of the tree. You can ask Andrew at most questions, each of which is in one of the following two forms:
- A x: Andrew will look at vertex in your labeling and tell you the number of this vertex in Li Chen's labeling.
- B y: Andrew will look at vertex in Li Chen's labeling and tell you the number of this vertex in your labeling.
Determine whether the two subtrees have at least one common vertex after asking some questions. If there is at least one common vertex, determine one of your labels for any of the common vertices.
思路
我们定义我的树为 树,对方的树为 树,如下图所示:
我们先取 树中选了的其中一个点,假设为 ,询问它在 树中的编号,记为 。
假如 就是 中选了的点,输出 即可。
否则,在 树中以 为根,则 中 选了的 点应如蓝色区域所示,对应 树中应为黄色区域。
设蓝色区域的根为 点,询问其在 中对应的点 。因为选的点是连通块,所以 中选了的点必然为红色区域,此时只需判断 是否为 中选中的点即可。
每组数据询问次数两次,总时间复杂度 。
Code
// Problem: CF1044B Intersecting Subtrees
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1044B
// Memory Limit: 250 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
using namespace std;
//#define int long long
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<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 10010
//#define M
//#define mo
struct node
{
int x, y, n;
}d[N<<1];
int n, m, i, j, k, T;
int h[N], mx[N], my[N];
int a[N], b[N];
int u, v, x, y, k1, k2;
void init()
{
memset(h, 0, sizeof(h));
memset(d, 0, sizeof(d));
memset(mx, 0, sizeof(mx));
memset(my, 0, sizeof(my));
k=0;
}
void cun(int x, int y)
{
d[++k].x=x; d[k].y=y;
d[k].n=h[x]; h[x]=k;
}
int dfs(int x, int fa)
{
int g, y, z;
for(g=h[x]; g; g=d[g].n)
{
y=d[g].y;
if(y==fa) continue;
if(mx[y]) return y;
z=dfs(y, x);
if(z) return z;
}
return 0;
}
signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
T=read();
while(T--)
{
n=read(); init();
for(i=1; i<n; ++i)
u=read(), v=read(), cun(u, v), cun(v, u);
k1=read(); for(i=1; i<=k1; ++i) mx[a[i]=read()]=1;
k2=read(); for(i=1; i<=k2; ++i) my[b[i]=read()]=1;
printf("B %d\n", b[1]);
fflush(stdout);
cin>>x; if(x==-1) break;
// printf("%d\n", x);
if(mx[x]) printf("C %d\n", x), fflush(stdout);
else
{
y=dfs(x, 0);
printf("A %d\n", y);
fflush(stdout);
cin>>x; if(x==-1) break;
printf("C %d\n", (my[x] ? y : -1));
fflush(stdout);
}
}
return 0;
}
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/16302197.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!