QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#32656 | #2569. Kill All Termites | YaoBIG# | WA | 5ms | 7160kb | C++17 | 2.6kb | 2022-05-22 21:07:58 | 2022-05-22 21:08:00 |
Judging History
answer
#include "bits/stdc++.h"
#define rep(i,a,n) for(auto i=a;i<=n;i++)
#define per(i,a,n) for(auto i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define all(A) A.begin(),A.end()
#define sz(A) (int)A.size()
template<class T> inline bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; }
template<class T> inline bool chmin(T &a, T b) { if(b < a) {a = b; return 1;} return 0; }
using namespace std;
template<class A, class B> string to_string(pair<A, B> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(char c) { return "'" + string(1, c) + "'"; }
string to_string(bool x) { return x ? "true" : "false"; }
template<class A> string to_string(A v)
{
bool first = 1;
string res = "{";
for(const auto &x: v)
{
if (!first) res += ", ";
first = 0;
res += to_string(x);
}
res += "}";
return res;
}
template<class A, class B> string to_string(pair<A, B> p) { return "(" + to_string(p.FI) + ", " + to_string(p.SE) + ")"; }
void debug_out() { cerr << endl; }
template<class Head, class... Tail> void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) if(0) puts("No effect.")
#endif
using ll = long long;
// using LL = __int128;
using pii = pair<int,int>;
using vi = vector<int>;
using db = double;
using ldb = long double;
const int maxn = 100'000;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int main()
{
int n; scanf("%d", &n);
static vi e[maxn + 5];
rep(i, 2, n)
{
int f; scanf("%d", &f);
e[f].pb(i);
e[i].pb(f);
}
if(n <= 2) return puts("1"), 0;
static int dp[maxn + 5][3];
function<void(int, int)> dfs = [&](int now, int fa)
{
int best = 0;
for(auto v: e[now]) if(v != fa)
{
dfs(v, now);
dp[now][0] += min({dp[v][0], dp[v][1], dp[v][2]});
dp[now][1] += dp[v][0];
dp[now][2] += dp[v][0];
chmin(best, dp[v][1] - dp[v][0]);
}
dp[now][0]++;
dp[now][2] += best;
// debug(now, dp[now][0], dp[now][1], dp[now][2]);
};
int rt = 1;
rep(i, 1, n) if(sz(e[i]) > 1) { rt = i; break; }
dfs(rt, 0);
int ans = min({dp[rt][0], dp[rt][1], dp[rt][2]});
printf("%d\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 5932kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 5936kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 7160kb
input:
8 1 1 2 1 2 3 2
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 4ms
memory: 7148kb
input:
3 1 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 4ms
memory: 6308kb
input:
4 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 5ms
memory: 6168kb
input:
5 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 0ms
memory: 6136kb
input:
6 1 2 2 2 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: -100
Wrong Answer
time: 3ms
memory: 6004kb
input:
7 1 2 1 4 3 4
output:
2
result:
wrong answer 1st numbers differ - expected: '1', found: '2'