{"id":96,"date":"2025-01-11T23:25:54","date_gmt":"2025-01-11T15:25:54","guid":{"rendered":"http:\/\/150.230.63.147\/blog\/?p=96"},"modified":"2025-01-11T23:25:54","modified_gmt":"2025-01-11T15:25:54","slug":"e-tree-queries-%e8%a7%a3%e6%9e%90%e6%80%9d%e7%b6%ad%e3%80%81lca","status":"publish","type":"post","link":"https:\/\/blog.seekrealthing.net\/blog\/?p=96","title":{"rendered":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h1>Codeforce 1328 E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)<\/h1>\n<p>\u4eca\u5929\u6211\u5011\u4f86\u770b\u770bCF1328E<br>\n<a href=\"https:\/\/codeforces.com\/problemset\/problem\/1328\/E\">\u984c\u76ee\u9023\u7d50<\/a><\/p>\n<blockquote>\n<p><strong>\u984c\u76ee<\/strong><br>\n\u7d66\u4f60\u4e00\u68f5\u6a39\uff0c\u4e26\u4e14\u7d66\u4f60$m\\le2e5$\u500b\u8a62\u554f(\u5305\u542b$k$\u500b\u9ede)\uff0c\u6c42\u80fd\u4e0d\u80fd\u627e\u5230\u4e00\u500b\u5f9e\u9ede$1$\u958b\u59cb\u7684\u8def\u5f91\uff0c\u4f7f\u5f97\u9019$k$\u500b\u9ede\u96e2\u9019\u500b\u8def\u5f91\u7684\u8ddd\u96e2$\\le1$<\/p>\n<\/blockquote>\n<h3>\u524d\u8a00<\/h3>\n<p>\u4e45\u9055\u7684\u6c92\u770b\u89e3\u7b54\u5beb\u7684\u984c\u76ee\uff0c\u6545\u5beb\u984c\u89e3<\/p>\n<h3>\u60f3\u6cd5<\/h3>\n<p>\u53ef\u4ee5\u89c0\u5bdf\u5230\uff0c\u984c\u76ee\u7684\u6558\u8ff0\u8868\u793a\uff0c\u5c0d\u65bc\u4e00\u500b\u7bc0\u9ede$v$\uff0c\u4ed6\u7684\u5b50\u7bc0\u9ede\u662f\u53ef\u4ee5\u540c\u6642\u51fa\u73fe\u7684\u3002\u56e0\u6b64\u6211\u5011\u53ea\u9700\u8981\u95dc\u6ce8$k$\u500b\u9ede\u7684\u6bcf\u500b\u9ede\u7684\u7236\u7bc0\u9ede\u80fd\u4e0d\u80fd\u5f62\u6210\u4e00\u500b\u8def\u5f91\uff0c\u800c\u8981\u5224\u65b7\u4e00\u500b\u9ede\u96c6\u80fd\u4e0d\u80fd\u6210\u70ba\u4e00\u689d\u8def\u5f91\uff0c\u6211\u5011\u53ea\u9700\u8981\u5148\u628a\u9ede\u96c6\u4ee5\u9ede\u7684\u6df1\u5ea6\u6392\u5e8f\uff0c\u63a5\u8457\u5f9e\u6df1\u5ea6\u4f4e\u7684\u9ede\u5169\u500b\u5169\u500b\u770b\uff0c\u5c0d\u65bc\u9ede\u5c0d$(u,v)$\uff0c\u5982\u679c$lca(u,v)=u$($u$\u662f\u6df1\u5ea6\u4f4e\u7684\u9ede)\uff0c\u5c31\u4ee3\u8868\u6211\u5011\u80fd\u5920\u5f9e$u$\u9023\u5230$v$\u3002<\/p>\n<h3>\u7a0b\u5f0f\u78bc:<\/h3>\n<pre><code class=\"language-cpp=\">const int _n=2e5+10,MAXB=19;  \nint n,m,u,v,k,a,b,dep[_n],fa[_n][MAXB];  \nVI G[_n];  \nvoid dfs(int v,int faa,int d){  \n  dep[v]=d;fa[v][0]=faa;  \n  rep(i,0,SZ(G[v]))if(faa!=G[v][i])dfs(G[v][i],v,d+1);  \n}  \nvoid bfa(){  \n  rep(j,1,MAXB)rep(i,1,n+1)if(~fa[i][j-1])  \n    fa[i][j]=fa[fa[i][j-1]][j-1];  \n}  \nint lca(int a,int b){  \n  if(dep[a]&lt;dep[b])swap(a,b);  \n  per(j,0,MAXB)if(~fa[a][j] and dep[fa[a][j]]&gt;=dep[b])a=fa[a][j];  \n  if(a==b)return a;  \n  per(j,0,MAXB)if(~fa[a][j] and fa[a][j]!=fa[b][j])a=fa[a][j],b=fa[b][j];  \n  return fa[a][0];  \n}  \nbool cmp(const int&amp; a,const int&amp; b){return dep[a]&lt;dep[b];}  \nmain(void) {cin.tie(0);ios_base::sync_with_stdio(0);  \n  cin&gt;&gt;n&gt;&gt;m;rep(i,0,n-1){cin&gt;&gt;u&gt;&gt;v;G[u].pb(v),G[v].pb(u);}  \n  dfs(1,-1,0);rep(i,1,n+1)rep(j,1,MAXB)fa[i][j]=-1; bfa();  \n  while(m--){  \n    cin&gt;&gt;k;VI ks;rep(i,0,k){cin&gt;&gt;v;ks.pb(v);}  \n    rep(i,0,k)ks[i]=(fa[ks[i]][0]==-1?1:fa[ks[i]][0]);  \n    sort(all(ks),cmp);  \n    rep(i,1,k)if(lca(ks[i-1],ks[i])!=ks[i-1]){cout&lt;&lt;&quot;NO\\n&quot;;goto A;}  \n    cout&lt;&lt;&quot;YES\\n&quot;;  \n    A:;  \n  }  \n  return 0;  \n}  \n<\/code><\/pre>\n<p>\u6a19\u982d\u3001\u6a21\u677f\u8acb\u9edeSubmission\u770b<br>\n<a href=\"https:\/\/codeforces.com\/contest\/1328\/submission\/93038340\">Submission<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[3],"tags":[10,13,9,8],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-ojcode","tag-codeforce","tag-lca","tag-ojcode","tag-8"],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\" \/>\n<meta property=\"og:locale\" content=\"zh_TW\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\" \/>\n<meta property=\"og:site_name\" content=\"Logic Formatting\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-11T15:25:54+00:00\" \/>\n<meta name=\"author\" content=\"jeff\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005:\" \/>\n\t<meta name=\"twitter:data1\" content=\"jeff\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9810\u4f30\u95b1\u8b80\u6642\u9593\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\u9418\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\"},\"author\":{\"name\":\"jeff\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454\"},\"headline\":\"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)\",\"datePublished\":\"2025-01-11T15:25:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\"},\"wordCount\":30,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\"},\"keywords\":[\"codeforce\",\"LCA\",\"ojcode\",\"\u601d\u7dad\"],\"articleSection\":[\"ojcode\"],\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.seekrealthing.net\/blog\/?p=96#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\",\"name\":\"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting\",\"isPartOf\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#website\"},\"datePublished\":\"2025-01-11T15:25:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96#breadcrumb\"},\"inLanguage\":\"zh-TW\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.seekrealthing.net\/blog\/?p=96\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/?p=96#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9801\",\"item\":\"https:\/\/blog.seekrealthing.net\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#website\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/\",\"name\":\"Logic Formatting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.seekrealthing.net\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-TW\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#organization\",\"name\":\"Logic Formatting\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png\",\"contentUrl\":\"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png\",\"width\":512,\"height\":512,\"caption\":\"Logic Formatting\"},\"image\":{\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454\",\"name\":\"jeff\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-TW\",\"@id\":\"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g\",\"caption\":\"jeff\"},\"sameAs\":[\"http:\/\/150.230.63.147\/blog\"],\"url\":\"https:\/\/blog.seekrealthing.net\/blog\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.seekrealthing.net\/blog\/?p=96","og_locale":"zh_TW","og_type":"article","og_title":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting","og_url":"https:\/\/blog.seekrealthing.net\/blog\/?p=96","og_site_name":"Logic Formatting","article_published_time":"2025-01-11T15:25:54+00:00","author":"jeff","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005:":"jeff","\u9810\u4f30\u95b1\u8b80\u6642\u9593":"1 \u5206\u9418"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96#article","isPartOf":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96"},"author":{"name":"jeff","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454"},"headline":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)","datePublished":"2025-01-11T15:25:54+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96"},"wordCount":30,"commentCount":0,"publisher":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization"},"keywords":["codeforce","LCA","ojcode","\u601d\u7dad"],"articleSection":["ojcode"],"inLanguage":"zh-TW","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.seekrealthing.net\/blog\/?p=96#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96","url":"https:\/\/blog.seekrealthing.net\/blog\/?p=96","name":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA) - Logic Formatting","isPartOf":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#website"},"datePublished":"2025-01-11T15:25:54+00:00","breadcrumb":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96#breadcrumb"},"inLanguage":"zh-TW","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.seekrealthing.net\/blog\/?p=96"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.seekrealthing.net\/blog\/?p=96#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9801","item":"https:\/\/blog.seekrealthing.net\/blog"},{"@type":"ListItem","position":2,"name":"E. Tree Queries \u89e3\u6790(\u601d\u7dad\u3001LCA)"}]},{"@type":"WebSite","@id":"https:\/\/blog.seekrealthing.net\/blog\/#website","url":"https:\/\/blog.seekrealthing.net\/blog\/","name":"Logic Formatting","description":"","publisher":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.seekrealthing.net\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-TW"},{"@type":"Organization","@id":"https:\/\/blog.seekrealthing.net\/blog\/#organization","name":"Logic Formatting","url":"https:\/\/blog.seekrealthing.net\/blog\/","logo":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png","contentUrl":"https:\/\/blog.seekrealthing.net\/blog\/wp-content\/uploads\/2025\/02\/android-chrome-512x512-1.png","width":512,"height":512,"caption":"Logic Formatting"},"image":{"@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/233e80a0fee3d4706f067f2e88c0b454","name":"jeff","image":{"@type":"ImageObject","inLanguage":"zh-TW","@id":"https:\/\/blog.seekrealthing.net\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f9b571b28c252728386f0002b35d9cb9a9028f740b0f9ca5ad678e7eb21c4857?s=96&d=mm&r=g","caption":"jeff"},"sameAs":["http:\/\/150.230.63.147\/blog"],"url":"https:\/\/blog.seekrealthing.net\/blog\/?author=1"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":1,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":97,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions\/97"}],"wp:attachment":[{"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.seekrealthing.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}