当前位置:网站首页>E2. escape the maze (hard version)
E2. escape the maze (hard version)
2022-07-26 01:48:00 【to cling】
Codeforces Round #756 (Div. 3)
Problem
There is one n A maze of points , Maze has n - 1 side , It's a tree .A Yes k A friend .A At the node 1,k A friend is k Two different nodes . Every second, everyone can move to an adjacent node , You can reach the same node at the same time .A And his friends can meet at the same node or at the midpoint of a side . If A Without meeting his friends , You can move to any leaf node , A win victory , otherwise A Our friends won .
ask : If A Your friend wins , this k At least a few individuals can catch A.
Solution
First consider A How to win
If A Minimum distance to leaf node Less than A The minimum distance to all friends , that A Win . The reverse is A Our friends won .A Your friend wins , Solve the minimum number of friends
- hypothesis
Friend node : The node of a friend is a friend node
A node x Have solution :A If you pass x node ,A Will be caught by his friends
A node x unsolvable :A Can pass x The node reaches a leaf node - Have solution :
①x There is a friend node to x Distance of Less than or equal to A To x Distance of ,
At this time, the solution is :1
( because A This friend can arrive in advance x node , And then grab A)
②x All the friends in the node of... Go to x Distance of Greater than A To x Distance of , And x All child nodes of have solutions ,
At this time, the solution is :x Sum the solutions of all child nodes of
( if x A child node of has no solution , that A You can win through this node . That is, there is no solution ) - unsolvable :
x All the friends in the node of... Go to x Distance of Greater than A To x Distance of , And there are nodes without solutions in its child nodes .
perhaps x A leaf node that is not a friend node ( if x Is a friend node , It's also a leaf node , obviously x Nodes have solutions )
- hypothesis
All in all , This question is very convoluted ... It takes patience to ponder
Code
const int N = 2e5 + 5, M = 4e6 + 7;
vector<int> v[N];
int a[N], d[N], near[N];
int n, k;
int dfs(int x, int fa)
{
int sum = 0; bool flag = 1;// Subtree and 、 Whether there is a subtree without solution
for (int y : v[x])
{
if (y == fa) continue;
d[y] = d[x] + 1;
int c = dfs(y, x);
if (c < 0) flag = 0;
else sum += c;
near[x] = min(near[x], near[y] + 1);
}
if (near[x] <= d[x]) return 1;// As long as there is a friend node, you can arrive first x There is a solution , Even if there are child nodes, there is no solution
if (sum == 0 || flag == 0) return -1;//x There are no child nodes and x Not a friend node perhaps There is a child node without solution , be x Node has no solution
return sum;
}
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
v[i].clear();
near[i] = inf;
d[i] = 0;
}
for (int i = 1; i <= k; i++)
{
cin >> a[i];
near[a[i]] = 0;
}
for (int i = 1, x, y; i < n; i++)
{
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
cout << dfs(1, -1) << endl;
}
return 0;
}
边栏推荐
- 给RestTemplate添加拦截器记录请求响应,还需解决流只读一次的问题
- Prime Ring Problem
- Big view +500 cases, software teams should improve R & D efficiency in this way
- flink sql 如何配置打印insert实参日志呢
- Zombie's treasure test (enumeration)
- npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
- SVN版本控制分支、合并功能使用
- What should I do when my blog is attacked by hackers?
- Overview of database stress testing methods
- There is no setter method in grpc list under flutter. How to use related attributes
猜你喜欢

AUTOCAD——计算面积的方法

pdf. JS introduction
![[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]](/img/fe/746ecaf4123072cca59d7510e9796c.png)
[Verilog digital system design (Xia Yuwen) 4 ----- basic concepts of Verilog syntax 2]

excel中怎么显示数字/英文时间

6 + 1 skills of Software Test Engineer

How idea can quickly delete recently opened projects

Pt onnx ncnn conversion problem record (followed by yolov5 training)

NFT market also began to diversify

大咖观点+500强案例,软件团队应该这样提升研发效能

C language enumeration types and unions
随机推荐
Cross Site Request Forgery (CSRF): impact, examples, and Prevention
Dijkstra find the shortest path
Shell exercises
The detailed knowledge summary of MySQL can be collected
Fiddler5+ lightning simulator 4.0 settings for app packet capturing
“蔚来杯“2022牛客暑期多校训练营2 H.[Take the Elevator] 维护线段
flink sql 如何配置打印insert实参日志呢
The best way to practice Animation: cover transition
Speech comprehension center comprehension summary
Easyrecovery15 data recovery software with high recovery rate and high download volume
Is it safe to buy funds in stock accounts? Professional answers
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
Shell summary (1)
Study notes: original code, inverse code, complement code
excel中怎么显示数字/英文时间
npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
Maximum side length of elements and squares less than or equal to the threshold (source: leetcode)
Stack Title: basic calculator
In spark SQL, date is used to display the day of the week according to the year, month and day_ format(date,‘u‘)
Excuse me, sir. Oracle to PG CDC Oracle, the upper case of the field is the same as that of PG