3 solutions
-
0
[模式三:分析标程]
树上牧羊人分配 标程深度学习:BFS 序与 DFN 序的二维映射降维打标
基础信息:提高+/省选-(蓝题) | | 树上贪心、多源 BFS、树状数组、DFN 序与 BFN 序
1. 思路来源与取舍:原作者怎么看透这题的?
树上连通块覆盖问题,核心矛盾永远是:如何快速判断一个羊是否已经被覆盖?如何快速将一个牧羊人能辐射到的所有羊打上覆盖标记? 很多选手会本能地考虑利用倍增或者建反向 DAG 去打标记,但在极深树或菊花图中,这些图遍历容易被常数卡住。 原作者直接放弃了基于图搜索的打标方式,采用了极其硬核的数据结构区间打标 (Range Update)。 原作者洞悉到一个关键几何性质:当我们在点 放置牧羊人,他辐射到的点 必然满足 ,或者通过向上跳跃拆解为:子树内深度恰好为 的所有点! 因为同深度的节点在 DFN(DFS 序)和 BFN(BFS 序)中具有极其优良的连续与单调性质,原作者直接将“图上扩散”转化为了“一维数组的区间覆盖”。
2. 核心代码掰开揉碎讲
- 双序预处理 (
dfs与 BFS 建树):dfn[x]:子树的映射,一个节点的子树在 DFN 序中是连续的一段 。bfn[x]:同层(同深度)的节点在 BFS 序中也是严格连续且单调的。dt[depth]:按照深度分层,存入该层所有节点的 DFN 序。
- 贪心主逻辑与上跳界限:
按照羊的深度从深到浅遍历(
sort(t, dep[x]>dep[y]))。while(dep[x] && dep[i]-dep[fa[x]] == dis[fa[x]])这是在寻找该羊所在连通块的最高点。只要父亲到该羊的真实距离等于父亲的最短辐射距离,牧羊人就可以安放在父亲处。这通过一步步向上跳实现。 - 覆盖核心逻辑
choosenode与update: 当牧羊人被安置在 时,它向外辐射的方式被拆解成了 的所有祖先 (包括 自己)向 的子树进行定深辐射。 辐射半径为 。因为深度必须绝对吻合,目标节点在子树 中的深度被锁死在 。 在update函数中,我们知道目标点具有三个限制:- 深度必须为 。
- 必须在 的子树内,即 DFN 序在 之间。
这完美对应了
lower_bound与upper_bound在dt[d](深度为 的 DFN 列表)中的二分查找! 找到这一段合法的节点后,将它们的 BFN 序提取出来,交给树状数组进行区间加一!
3. 代码架构与底层细节
- 树状数组
BIT的巧用: 这里只需要区间加、单点查是否为 0(差分树状数组)。 原作者用b.query(bfn[x])来判定羊是否已经被看管。因为 BFS 序在更新时具有同层集中的特性。 - 边界防越界处理:
update(int l, int r)传入的是树状数组,里面特地写了++l; r+=2;。这说明原作者的 BFN 是从0开始的 0-based 体系,为了适配树状数组必须变成 1-based,同时差分时尾部需要r+1,因此合成了r+=2的写法。 - 找区间的细节:
if(it!=dt[d].end() && (*it)<=sz[x])这里的sz[x]实际上在前面被改写成了dfn[x] + sz[x] - 1,代表子树 DFN 序的右端点。因此这里直接判跨出了子树。
4. 工程还原与调试推演
原作者在思考“如何覆盖”时,面临了两个选择:
- 沿着等距离的反向边在图上 BFS 扩散。
- 拆解成“子树内某一深度的点集”。
显然,原作者对树上 DFN 序与深度的结合非常熟练(这是解决 -cousin 问题的经典长链剖分/DFN二分套路)。
于是果断手写
vector<int> dt[depth],通过二分直接在 内锁定了那批待覆盖的羊。 随后,为了防止一头羊被覆盖多次却无法 判定,原作者引入了 BFN(因为同一层的点在 DFN 中可能被其他分支隔断,但在 BFN 中更易于打差分标签),用一棵差分树状数组完美闭环!
5. 适用场景与死穴
- 适用场景:树上涉及“按距离定深辐射”的覆盖判定问题。利用 DFN 序 + 深度分类二分是处理子树定深查询的最佳无脑套路。
- 死穴:
while(dep[x] && dep[i]-dep[fa[x]] == dis[fa[x]]) x = fa[x];在本题中由于树的深度可能高达 (链状图),这一步暴力往上跳可能退化成 的最坏复杂度从而 TLE!这不如倍增直接跳跃来得稳健。- BFN 和 DFN 在打差分标记时的混用极易写错下标,导致 UB 或越界。
6. 带详尽注释的完整代码
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n,m; int a[500005]; vector<int>v[500005]; int dis[500005],dep[500005],bfn[500005]; int dfn[500005],sz[500005],rev[500005],fa[500005]; int tot; // [核心]: 生成 DFS 序,为“子树定深辐射”提供区间二分基础 void dfs(int x,int pre){ rev[tot]=x;dfn[x]=tot++;sz[x]=1;fa[x]=pre; for(auto i:v[x])if(i!=pre)dfs(i,x),sz[x]+=sz[i]; } // 树状数组:维护 BFN 序的区间差分,单点查询是否覆盖 struct BIT{ int a[500005]; void init(){ REP(i,0,n+1)a[i]=0; } void update(int l,int r){ // 将 0-based BFN 区间转化为 1-based 的差分加 ++l;r+=2; while(l<=n)++a[l],l+=lowbit(l); while(r<=n)--a[r],r+=lowbit(r); } bool query(int x){ ++x;int s=0; while(x)s+=a[x],x-=lowbit(x); return s==0; } }b; bool checknode(int x){ return b.query(bfn[x]); } vector<int>dt[500005]; // [核心]: 在子树 x 中,覆盖深度恰为 d 的所有点 void update(int x,int d){ d+=dep[x];if(d>=n)return; int l=0,r=-1; // 在同层深度列表中,二分出 DFN 序位于 [dfn[x], dfn[x]+sz[x]-1] 的一段连续块 auto it=lower_bound(all(dt[d]),dfn[x]); if(it!=dt[d].end()&&(*it)<=sz[x]){ l=*it;it=upper_bound(all(dt[d]),sz[x]);--it;r=*it; // 将 DFN 转换回节点编号,再转换为 BFS 序编号,交由树状数组打标记 l=bfn[rev[l]];r=bfn[rev[r]]; b.update(l,r); } } // 牧羊人放在 x 时,向其所有祖先拆分辐射,辐射定深目标 void choosenode(int x){ int y=x; REP(i,0,dis[x]+1)if(i<=dep[x]){ update(y,dis[x]-i); y=fa[y]; }else break; } void Main() { cin>>n>>m; REP(i,1,n){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y);v[y].pb(x); } queue<int>q; vector<int>t; REP(i,0,m){int x;cin>>x;a[--x]=1;q.push(x);t.pb(x);} // 多源 BFS 求所有点距离羊的最短辐射半径 dis REP(i,0,n)if(a[i])dis[i]=0;else dis[i]=-1; while(!q.empty()){ int x=q.front();q.pop(); for(auto i:v[x])if(dis[i]==-1){ dis[i]=dis[x]+1; q.push(i); } } // 从根 0 开始的 BFS,生成 BFS 序与深度 q.push(0);dep[0]=0;int c=0; REP(i,1,n)dep[i]=-1; while(!q.empty()){ int x=q.front();bfn[x]=c++;q.pop(); // 记录 BFS 序 for(auto i:v[x])if(dep[i]==-1){ dep[i]=dep[x]+1; q.push(i); } } dfs(0,-1); // 变形 sz 为子树 DFN 的右端点,并将节点按深度分类塞入对应的 DFN 列表 REP(i,0,n)sz[i]+=dfn[i]-1,dt[dep[i]].pb(dfn[i]); // 深羊先处理 sort(all(t),[&](int x,int y){return dep[x]>dep[y];}); b.init(); // 对每层的 DFN 列表进行排序,为二分做准备 REP(i,0,n)sort(all(dt[i])); vector<int>res; for(auto i:t)if(checknode(i)){ int x=i; // 暴力跳祖先找最高合法点(存在被菊花链卡成 O(N^2) TLE 的风险) while(dep[x]&&dep[i]-dep[fa[x]]==dis[fa[x]])x=fa[x]; res.pb(x);choosenode(x); } cout<<res.size()<<endl; for(auto i:res)cout<<i+1<<' '; cout<<endl; } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("tree.in","r",stdin); freopen("tree.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; }7. [核心 Trick 与通用技巧](高亮警告)
【子树内定深节点的快速查询 (DFN 分层二分法)】: 在树上遇到“在子树 内,寻找所有深度恰好等于 的点”这类查询时: 绝对不要在树上进行暴搜! 将所有点的深度(或者层数)作为第一维数组
vector<int> dt[depth],在里面塞入这些点的 DFN 序。 子树 在 DFN 中是一段完美的区间 。 因此,我们只需在dt[D]中做两次lower_bound和upper_bound即可在 时间内提取出所有符合深度的子树内节点。这一技巧在树上距离统计、点集覆盖中是纯粹的降维打击! - 双序预处理 (
-
0
[题目名称]:树上牧羊人分配(连通块最小点覆盖)
基础信息:提高+/省选-(蓝题) | | 树上贪心、多源 BFS、倍增、DAG 遍历
1. 这题在考啥 & 常规套路
题目的核心是**“牧羊人只会看管离他最近的羊”。 对于任意一个放置在 的牧羊人,他到任意被他看管的羊 的距离,必然等于他到所有羊的最短距离,即 。 这意味着,对每一只羊 而言,它能够被放在树上某些点的牧羊人看管,这些合法的位置构成了一个连通子图(Voronoi 区域)**,我们不妨记为 。
- 如果将牧羊人放置在 中的任意一点,他都可以看管 (可能是同时看管多只最近的羊)。
- 题目要求每只羊都至少被看管一次,本质上等价于:寻找最少数量的点(牧羊人),使得每个点集 内都至少包含被选中的一个点(树上连通块 Hitting Set 模型)。
处理树上的连通块最小点覆盖问题,有一个极强且极其经典的贪心算法:
- 找出每个连通块深度最浅(最高)的节点 。
- 不断在尚未被覆盖的连通块中,选取其最高点深度最深的那个连通块,并将牧羊人安放在其最高点。
- 随后将该点所在的全部连通块打上“已被覆盖”的标记。
2. 怎么想到的 (How & Why)
首先,通过 多源 BFS,在 时间内预处理出每个节点到所有羊的最小距离 。 对于每一只羊 ,其连通块 中的最高点 必定是 在树上的某一个祖先。 我们要找到 的最浅的祖先 ,满足 。因为 是 的祖先,所以 。 转换一下等式: 合法的条件是 。 随着祖先不断变浅,如果某一点不满足该条件,它更上方的祖先也必然不满足(具有单调性)。因此,我们可以通过 倍增法,在 时间内迅速跃迁找到每只羊的最高合法祖先 。
当牧羊人选择在某个节点 时,他能覆盖哪些羊呢? 因为看管条件是 ,如果我们在原树上建立一个DAG(有向无环图):将每条满足 的边转为有向边 。 牧羊人放置在 时,他在这个 DAG 上能遍历到达的汇点( 的羊所在的点),恰恰就是被他覆盖的所有羊! 在 DAG 上 BFS 遍历能实现 一次性将相关羊标记完毕,极其高效。
3. 核心算法与“焊死”变量定义
D[x]:点 距离所有羊之中的最小距离。up[x][j]:倍增数组,维护点 往上跳 步的祖先,快速找出满足D[u] + depth[u] == depth[y]的最浅点。top[y]:对于羊 的连通块 ,其合法能够放置牧羊人的深度最浅点。vis_dag[x]:防重复搜索标记,若 DAG 上的点 已经被先前的牧羊人探索过,那么它后方必然没有未被覆盖的羊,可直接剪枝跳过。
4. 极简完整代码与 I/O 抉择
采用预分配一维数组
array提升内存连续性与缓存命中率,速度极快。#include <iostream> #include <vector> #include <queue> #include <algorithm> #include <array> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; if(!(cin >> n >> k)) return 0; vector<vector<int>> adj(n + 1); for(int i = 0; i < n - 1; ++i){ int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } vector<bool> is_sheep(n + 1, false); for(int i = 0; i < k; ++i){ int x; cin >> x; is_sheep[x] = true; } // 1. 多源 BFS:求出每个点到最近的羊的距离 D[x] vector<int> D(n + 1, 1e9); queue<int> q; for(int i = 1; i <= n; ++i){ if(is_sheep[i]){ D[i] = 0; q.push(i); } } while(!q.empty()){ int u = q.front(); q.pop(); for(int v : adj[u]){ if(D[v] > D[u] + 1){ D[v] = D[u] + 1; q.push(v); } } } // 2. BFS 建树与倍增初始化(避免深树导致 DFS 爆栈) vector<int> depth(n + 1, 0); vector<array<int, 20>> up(n + 1); for(int i = 0; i <= n; ++i) up[i].fill(0); vector<bool> vis_tree(n + 1, false); queue<int> tq; tq.push(1); vis_tree[1] = true; while(!tq.empty()){ int u = tq.front(); tq.pop(); for(int v : adj[u]){ if(!vis_tree[v]){ vis_tree[v] = true; depth[v] = depth[u] + 1; up[v][0] = u; for(int j = 1; j < 20; ++j){ up[v][j] = up[ up[v][j-1] ][j-1]; } tq.push(v); } } } // 3. 寻找并确定所有羊连通块的最高点 top vector<int> top(n + 1, 0); vector<int> order; for(int i = 1; i <= n; ++i){ if(is_sheep[i]){ int u = i; for(int j = 19; j >= 0; --j){ int p = up[u][j]; // 如果由于 D[p] + depth[p] == depth[y] 满足等价于该祖先合法 if(p != 0 && D[p] + depth[p] == depth[i]){ u = p; } } top[i] = u; order.push_back(i); } } // 按最高点深度降序排列(深度大的在前面) sort(order.begin(), order.end(), [&](int a, int b){ return depth[top[a]] > depth[top[b]]; }); // 4. 贪心选取最高点并在 DAG 上进行覆盖打标 vector<bool> covered(n + 1, false); vector<bool> vis_dag(n + 1, false); vector<int> ans; for(int y : order){ if(!covered[y]){ int t = top[y]; ans.push_back(t); queue<int> bq; if(!vis_dag[t]){ vis_dag[t] = true; bq.push(t); } // 沿着 D[u] == D[v] + 1 的 DAG 进行 BFS,搜索出当前牧羊人能辐射到的所有羊 while(!bq.empty()){ int u = bq.front(); bq.pop(); if(is_sheep[u]){ covered[u] = true; // 发现羊并视为覆盖 } for(int v : adj[u]){ if(D[u] == D[v] + 1){ if(!vis_dag[v]){ vis_dag[v] = true; bq.push(v); } } } } } } cout << ans.size() << "\n"; for(int i = 0; i < ans.size(); ++i){ cout << ans[i] << (i + 1 == ans.size() ? "" : " "); } cout << "\n"; return 0; }5. 核心 Trick 与通用技巧(高亮警告)
【度量空间求交转化为树上连通块覆盖与 DAG 有向打标】 当遇到
距当前点的绝对距离恰好是距目标合集的极小值这一极其复杂的条件时,不要直接陷入二维偏序里求交点。- 第一步始终应当是多源 BFS 计算目标点权等高线 (等势面)。
- 其余过程利用偏导属性建 DAG:
D[u] == D[v] + 1建立的有向连边实质上演变成了最短流的传播路线,只需在图上跑 BFS 打标即可取代暴力枚举判断,完美防 TLE。 - 利用倍增寻找目标区间端点是卡常防递归爆栈的最佳策略。在高达百万级别的数据规模题中,尽量使用 BFS 辅以倍增递推能够让你彻底脱离深度超限 (RE) 的灾难。
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define int long long #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over(x) {cout<<(x)<<endl;return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int n,m; int a[500005]; vector<int>v[500005]; int dis[500005],dep[500005],bfn[500005]; int dfn[500005],sz[500005],rev[500005],fa[500005]; int tot; void dfs(int x,int pre){ rev[tot]=x;dfn[x]=tot++;sz[x]=1;fa[x]=pre; for(auto i:v[x])if(i!=pre)dfs(i,x),sz[x]+=sz[i]; } struct BIT{ int a[500005]; void init(){ REP(i,0,n+1)a[i]=0; } void update(int l,int r){ ++l;r+=2; while(l<=n)++a[l],l+=lowbit(l); while(r<=n)--a[r],r+=lowbit(r); } bool query(int x){ ++x;int s=0; while(x)s+=a[x],x-=lowbit(x); return s==0; } }b; bool checknode(int x){ return b.query(bfn[x]); } vector<int>dt[500005]; void update(int x,int d){ d+=dep[x];if(d>=n)return; // cout<<x<<':'<<d-dep[x]<<endl; int l=0,r=-1; auto it=lower_bound(all(dt[d]),dfn[x]); if(it!=dt[x].end()&&(*it)<=sz[x]){ l=*it;it=upper_bound(all(dt[d]),sz[x]);--it;r=*it; // cout<<rev[l]<<' '<<rev[r]<<endl; l=bfn[rev[l]];r=bfn[rev[r]]; b.update(l,r); } } void choosenode(int x){ int y=x; REP(i,0,dis[x]+1)if(i<=dep[x]){ update(y,dis[x]-i); y=fa[y]; }else break; } void Main() { cin>>n>>m; REP(i,1,n){ int x,y; cin>>x>>y; --x,--y; v[x].pb(y);v[y].pb(x); } queue<int>q; vector<int>t; REP(i,0,m){int x;cin>>x;a[--x]=1;q.push(x);t.pb(x);} REP(i,0,n)if(a[i])dis[i]=0;else dis[i]=-1; while(!q.empty()){ int x=q.front();q.pop(); for(auto i:v[x])if(dis[i]==-1){ dis[i]=dis[x]+1; q.push(i); } } q.push(0);dep[0]=0;int c=0; REP(i,1,n)dep[i]=-1; while(!q.empty()){ int x=q.front();bfn[x]=c++;q.pop(); for(auto i:v[x])if(dep[i]==-1){ dep[i]=dep[x]+1; q.push(i); } } dfs(0,-1); REP(i,0,n)sz[i]+=dfn[i]-1,dt[dep[i]].pb(dfn[i]); sort(all(t),[&](int x,int y){return dep[x]>dep[y];}); b.init(); REP(i,0,n)sort(all(dt[i])); vector<int>res; for(auto i:t)if(checknode(i)){ int x=i; while(dep[x]&&dep[i]-dep[fa[x]]==dis[fa[x]])x=fa[x]; res.pb(x);choosenode(x); } cout<<res.size()<<endl; for(auto i:res)cout<<i+1<<' '; cout<<endl; } void TC() { int tc=1; while(tc--){ Main(); cout.flush(); } } signed main() { freopen("tree.in","r",stdin); freopen("tree.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2961
- Time
- 2000ms
- Memory
- 512MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 2
- Accepted
- 2
- Uploaded By