标签:mes ref return head \n dfs getch class char s
https://codeforces.com/contest/767/problem/C
之所以把这个题放进来,是因为要记录错误
情况不止一种
所以答案存储就是>=2了
#include <iostream>
#include <cstdio>
#define ll long long
using namespace std;
const int N=1e6+7;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,w[N],ans[N];
ll tot[N],goal;
struct node {
int v,nxt,num;
}e[N<<1];
int head[N<<1],cnt;
void add(int u,int v,int num) {
e[++cnt].v=v;
e[cnt].nxt=head[u];
e[cnt].num=num;
head[u]=cnt;
}
void dfs(int u,int f) {
tot[u]=w[u];
for(int i=head[u];i;i=e[i].nxt) {
int v=e[i].v;
if(v==f) continue;
dfs(v,u);
if(tot[v]==goal)
ans[++ans[0]]=e[i].num;
else
tot[u]+=tot[v];
}
// cout<<u<<" "<<tot[u]<<">\n";
}
int main() {
n=read();
for(int i=1;i<=n;++i) {
int x=read();
w[i]=read();
goal+=w[i];
if(x) add(i,x,i),add(x,i,i);
}
if(goal%3!=0) {
puts("-1");
return 0;
}
goal/=3;
dfs(1,0);
if(ans[0]>=2) {
if(ans[1]>ans[2]) swap(ans[1],ans[2]);
printf("%d %d\n",ans[1],ans[2]);
} else puts("-1");
return 0;
}
标签:mes ref return head \n dfs getch class char s
原文地址:https://www.cnblogs.com/dsrdsr/p/10437196.html