QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#781250 | #9806. Growing Tree | masttf# | WA | 2ms | 3768kb | C++20 | 2.1kb | 2024-11-25 15:24:43 | 2024-11-25 15:24:43 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
#define dbg(x...) \
do{ \
cout << #x << " -> "; \
err(x); \
}while(0)
void err(){
cout << endl;
}
template<class T, class ...Ts>
void err(T arg, Ts ...args){
cout << fixed << setprecision(10) << arg << ' ';
err(args...);
}
const int maxn = 1e4 + 5;
vector <int> t[maxn];
vector <int> leaf;
vector <int> line[maxn];
vector <int> route;
int a[maxn],vis[maxn],dep[maxn],check[100],fa[maxn];
void dfs(int u,int f) {
int ok = 0;
route.push_back(u);
for (auto i : t[u]) {
if (i == f) continue;
ok = 1;
dep[i] = dep[u] + 1;
dfs(i,u);
}
if (!ok) {
leaf.push_back(u);
line[u] = route;
}
route.pop_back();
}
void solve(){
route.clear();
leaf.clear();
for (int i = 0;i < 100;i++) check[i] = 0;
int n;cin >> n;
int N = 1;
for (int i = 1;i <= n + 1;i++) N *= 2;
N--;
//dbg(N);
for (int i = 1;i <= N;i++) vis[i] = 0,line[i].clear(),t[i].clear();
for (int i = 2;i <= N;i++) {
cin >> a[i];
}
for (int i = 1;i <= N;i++) {
int l,r;
l = 2 * i,r = 2 * i + 1;
if (l > N || r > N) continue;
t[i].push_back(l);
t[i].push_back(r);
t[l].push_back(i);
t[r].push_back(i);
fa[l] = i,fa[r] = i;
}
dfs(1,0);
int len = leaf.size(),cnt = 0;
for (int i = 0;i < len;i++) {
for (int j = 0;j < i;j++) {
int u1 = leaf[i],v1 = leaf[j];
int length = line[u1].size();
int sum1 = 0,sum2 = 0,ok = 0;
for (int k = 0;k < length;k++) {
int u = line[u1][k],v = line[v1][k];
if (u == v) continue;
if (vis[u] || vis[v]) {
ok = 1;
break;
}
sum1 += a[u],sum2 += a[v];
}
if (ok) continue;
if (sum1 == sum2) {
while (fa[u1] != fa[v1]) u1 = fa[u1],v1 = fa[v1];
u1 = fa[u1],v1 = fa[v1];
if (check[dep[u1 * 2 + 1]]) {
cout << "-1" << '\n';
return ;
}
else {
cnt++;
vis[u1 * 2 + 1] = 1;
check[dep[u1 * 2 + 1]] = 1;
//dbg(i,j,u1);
}
}
}
}
cout << cnt << '\n';
//dbg(cnt);
return ;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
cin >> t;
while(t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3604kb
input:
3 2 1 2 4 3 2 1 2 1 2 3 3 2 1 2 1 2 3 3 1 1
output:
1 2 -1
result:
ok 3 number(s): "1 2 -1"
Test #2:
score: -100
Wrong Answer
time: 2ms
memory: 3768kb
input:
94 5 44 65 38 61 64 94 71 53 65 10 24 36 98 74 11 4 5 46 72 34 9 24 37 32 76 29 48 88 17 14 36 4 22 6 71 53 24 61 89 79 39 57 99 61 27 85 99 46 81 75 90 25 16 13 1 87 55 81 56 78 67 2 3 83 3 74 14 45 17 22 41 62 74 25 1 56 22 7 21 73 83 99 3 91 16 53 8 10 49 29 54 81 45 10 12 68 32 9 30 11 99 85 73 ...
output:
-1 0 -1 -1 0 1 -1 0 0 3 0 0 0 1 2 1 0 2 0 1 0 -1 0 -1 0 0 -1 -1 -1 -1 -1 -1 -1 0 4 2 -1 -1 -1 -1 2 2 -1 0 0 -1 -1 1 -1 0 -1 2 -1 0 0 0 -1 1 -1 -1 0 0 1 1 -1 0 1 2 0 -1 0 0 1 1 -1 0 -1 0 0 0 -1 -1 -1 1 -1 0 0 0 0 1 0 -1 4 -1
result:
wrong answer 1st numbers differ - expected: '2', found: '-1'