Codeforces Round #398 (Div. 2) C. Garland
来源:互联网 发布:egd网络小黄金骗局 编辑:程序博客网 时间:2024/05/21 09:34
Codeforces Round #398 (Div. 2)
C. Garland
题目链接
题目大意:
给一棵树以及它的根节点,每个节点存一个值,题目中这个值是温度,Dima拿住这个根节点,他想把整个树剪成三段,使得三段上的温度和相等。(拿住根节点是想让根节点在中间的一段)
直接从根节点开始DFS,每次更新子树上的温度和,如果温度和==总和/3,记录答案,除去该子树,tree[x]=0(以后DFS不再使用) 结束DFS后再判断根节点的和是否==总和/3,如果不是输出-1
#include<cstdio>using namespace std;int n,num[1000009],summ,san,ans[10],aa,root;struct E{ int next,to;}edge[2000060];int head[2000060],edge_num;int tree[1000009];void addedge(int x,int y){ edge[++edge_num].next=head[x]; edge[edge_num].to=y; head[x]=edge_num;}void DFS(int x){ int i; for(i=head[x];i;i=edge[i].next){ DFS(edge[i].to); tree[x]+=tree[edge[i].to]; } if(tree[x]==san&&aa<=1&&x!=root) tree[x]=0,ans[aa++]=x;}int main(){ freopen("Garland.in","r",stdin); ans[0]=ans[1]=-1; scanf("%d",&n); int i; for(i=1;i<=n;i++){ int a,b; scanf("%d%d",&a,&b); tree[i]=b; if(a==0) root=i; else addedge(a,i); summ+=b; } if(summ%3!=0){ printf("-1");return 0; } san=summ/3; DFS(root); if(ans[0]!=-1&&ans[1]!=-1 && tree[root]==san){ printf("%d %d",ans[1],ans[0]); return 0; } printf("-1"); return 0;}
0 0
- Codeforces Round #398 (Div. 2) C. Garland
- Codeforces Round #398 (Div. 2) C Garland
- Codeforces Round #398 (Div. 2) C. Garland
- Codeforces Round #398 (Div. 2) C. Garland
- Codeforces Round #398(Div. 2)C. Garland
- 【Codeforces Round #398 (Div. 2)】Codeforces 767C Garland
- Codeforces Round #398 (Div. 2) -- C. Garland (dfs)
- Codeforces Round #398(Div. 2)C. Garland【Dfs+思维】
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
- Codeforces Round #398 (Div. 2) C. Garland 后序遍历树
- Codeforces Round #239 (Div. 2) B. Garland
- Codeforces Round #392 (Div. 2)Blown Garland
- Codeforces Round #398 (Div. 2) A.Snacktower B.The Queue C.Garland D.Cartons of milk E.Change-fr
- CF-Codeforces Round #392 (Div. 2)-B-Blown Garland
- Codeforces Round #392 (Div. 2) B - Blown Garland
- Codeforces Round #392 (Div. 2)-B. Blown Garland(水题)
- Codeforces Round #392(Div. 2) B Blown Garland【暴力枚举】
- Codeforces Round #392 (Div. 2)B Blown Garland
- UVA - 242 线性DP
- java发送邮件
- Nginx、Apache 域名301跳转
- 14. Longest Common Prefix\441. Arranging Coins\367. Valid Perfect Square
- Unity Physics2D.OverlapPointAll
- Codeforces Round #398 (Div. 2) C. Garland
- Notepad++ 插件整理
- Bullet 物理引擎学习笔记(1) -- HelloWorld 详解
- Jetty之SocketEndPoint分析
- RxJava笔记--02(Android中使用RxAndroid)
- 详解 Spotlight on Unix 监控Linux服务器
- ajax_ajax从json数据中取其中某一值,输出未定义
- Android自定义网络加载中对话框
- win7下electron安装