当前位置:网站首页>【AtCoder2304】Cleaning
【AtCoder2304】Cleaning
2022-06-11 07:37:00 【CaptainHarryChen】
The question
A tree has n Nodes , node i Yes A[i] A stone . Remove all stones .
For each stone removal operation , Select two leaf nodes (u,v)(u Can not be equal to v), remove u To v A stone for each node on the path ( Include u,v). Be careful : If there is a node on this path without stones , The operation cannot be performed .
( The degree of leaf node here is 1 The node of )
Can the stones on the tree be removed through the above operations .
Answer key
Find a degree greater than 1 Node of is root ,dfs down , Every time I go back , How many more operations are needed on this tree .
The current node is u, processed u Of all the sons of , There are still some operations left over from these subtrees , Need to pass through u To complete .
The remaining operands are paired in pairs , That is, it forms several paths , Finally, make the remaining operands of the subtree and the current node u The number of stones is the same , The remaining stones need to be removed by a higher path .
Let the son subtree remain sum operations , When pairing , Take two operations from the son subtree each time , Remove a stone from the current node , Then the total number of operations is sum-A[u].
sum<A[u] unsolvable ( The number of operations is negative )
A[u]<sum-A[u] unsolvable ( There are not enough stones at the current node )
take mx Is the maximum value of the remaining operands of the son subtree
sum-mx<sum-A[u] unsolvable ( Cannot pair in pairs )
For the root node , The remaining operands need to be 0, Then we must satisfy A[u]==sum/2
If n==2, Special judgement ( Root not found ...)
Code
#include<cstdio> #include<cstdlib> #include<vector> #include<algorithm> using namespace std; const int MAXN=100005; void NoSolution() {
puts("NO"); fclose(stdin); fclose(stdout); exit(0); } int n,A[MAXN]; vector<int> adj[MAXN]; int dfs(int u,int fa=0) {
if(adj[u].size()==1U) return A[u]; long long sum=0; int tmp=0,mx=0; for(int i=0;i<(int)adj[u].size();i++) {
int v=adj[u][i]; if(v==fa) continue; tmp=dfs(v,u); mx=max(mx,tmp); sum+=tmp; } if(fa==0) {
if(sum%2==1||mx>sum/2||A[u]!=sum/2) NoSolution(); return 0; } long long delta=sum-A[u]; if(A[u]<delta||sum<A[u]||sum-mx<delta) NoSolution(); return A[u]-delta; } int main() {
scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&A[i]); if(n==2) {
if(A[1]==A[2]) puts("YES"); else puts("NO"); return 0; } for(int i=1;i<n;i++) {
int a,b; scanf("%d%d",&a,&b); adj[a].push_back(b); adj[b].push_back(a); } for(int i=1;i<=n;i++) if(adj[i].size()>1U) {
dfs(i); break; } puts("YES"); return 0; } 边栏推荐
- C language judging big end and small end [consortium or pointer] big end and small end conversion
- MFC debugger OutputDebugString override
- Compound ratemodel contract analysis
- Classification of MNIST datasets with keras
- 零基础自学SQL课程 | OUTER JOIN外连接
- Configuration software -- control import
- C language Yanghui triangle code
- Nosqlzoo question brushing-1
- Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
- Compound RateModel合約解析
猜你喜欢

2022低压电工考题及在线模拟考试

QT picture adaptive display control

【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算

@Jsonproperty annotation

If you want to save an IP address, what data type is better? 99% of people will answer wrong!
![[Oracle database] mammy tutorial day03 Sorting Query](/img/ea/24c9495a2ef4f1786f7b7852bde321.png)
[Oracle database] mammy tutorial day03 Sorting Query

Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]
![[analysis of STL source code] summary notes (6): Design of iterator and magical traits](/img/57/eaf02e880d205c5912f353609c9520.png)
[analysis of STL source code] summary notes (6): Design of iterator and magical traits
随机推荐
C language Yanghui triangle code
Nosqlzoo question brushing-1
big. Js-- use / instance
Djikstra solves the shortest circuit with negative weight
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
wordcloud的使用
【AtCoder1998】Stamp Rally(整体二分+并查集)
R language Parallel Computing practice tutorial
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
MySQL设置管理员密码无法生效的案例一则
A case in which the MySQL administrator password cannot take effect
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
【CodeForces908H】New Year and Boolean Bridges (FWT)
[poj3691] DNA repair (AC automata +dp)
C language function stack frame
[codeforces1019e] raining season
[Oracle database] mammy tutorial day04 Sorting Query
QT picture adaptive display control
【POJ3691】DNA repair (AC自动机+DP)
【AtCoder1981】Shorten Diameter(图论思维)