设计一套网站多少钱,网站设计ppt案例,河南郑州建设网站制作,wordpress 时间标题展示CF1305D Kuroni and the Celebration
题意#xff1a;
给你一棵有 n 个节点的树。对于每次询问#xff0c;提交两个点#xff0c;评测机会返回这两个点的 LCA。求树根。
询问格式为 ? u v#xff0c;此时评测机会返回 u 和 v 的 LCA。
提交格式为 ! x#xff0c;表示…CF1305D Kuroni and the Celebration
题意
给你一棵有 n 个节点的树。对于每次询问提交两个点评测机会返回这两个点的 LCA。求树根。
询问格式为 ? u v此时评测机会返回 u 和 v 的 LCA。
提交格式为 ! x表示你得出树根为点 x。
你可以最多询问 ⌊n2⌋\lfloor \frac{n}{2}\rfloor⌊2n⌋次
题解
如果一个叶子和另一个点的LCA是这个叶子那么这个叶子一定为根 否则这个叶子一定不是根 所以我们可以每次询问两个叶子的LCA即可如果是其中一个点则那个点就是根否则删除这个两个点,有可能得到新的叶子节点用得到的LCA去和剩下的叶子节点继续求LCA 最坏情况每次删除两个叶子节点那么⌊n2⌋\lfloor \frac{n}{2}\rfloor⌊2n⌋次后最多只剩下一个点这就是根
代码
#include bits/stdc.h
#include unordered_map
#define debug(a, b) printf(%s %d\n, a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pairint, int PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll 1e18;
const int INF_int 0x3f3f3f3f;
void read(){};
template typename _Tp, typename... _Tps void read(_Tp x, _Tps... Ar)
{x 0;char c getchar();bool flag 0;while (c 0 || c 9)flag| (c -), c getchar();while (c 0 c 9)x (x 3) (x 1) (c ^ 48), c getchar();if (flag)x -x;read(Ar...);
}
template typename T inline void write(T x)
{if (x 0) {x ~(x - 1);putchar(-);}if (x 9)write(x / 10);putchar(x % 10 0);
}
void rd_test()
{
#ifdef ONLINE_JUDGE
#elsestartTime clock ();freopen(data.in, r, stdin);
#endif
}
void Time_test()
{
#ifdef ONLINE_JUDGE
#elseendTime clock();printf(\nRun Time:%lfs\n, (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int maxn1020;
vectorintvec[maxn];
int d[maxn];
int main()
{//rd_test();int n;read(n);for(int i1;in;i){int u,v;read(u,v);vec[u].push_back(v);vec[v].push_back(u);d[u];d[v];}queueintq;for(int i1;in;i){if(d[i]1)q.push(i);}int lca;while(!q.empty()){
// if(q.size()0)printf(! %d\n,lca);int xq.front();q.pop();if(q.size()0)return printf(! %d\n,x),0;int yq.front();q.pop();printf(? %d %d\n,x,y);cinlca;if(lcax)return printf(! %d\n,x),0;else if(lcay)return printf(! %d\n,y),0;for(auto v:vec[x])if((--d[v])1)q.push(v);for(auto v:vec[y])if((--d[v])1)q.push(v);// if(totn/2)break;}
// coutlcaendl;//Time_test();
}