QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#32663 | #2569. Kill All Termites | YaoBIG# | AC ✓ | 48ms | 10436kb | C++17 | 2.6kb | 2022-05-22 21:36:22 | 2022-05-22 21:36:24 |
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][2];
function<void(int, int)> dfs = [&](int now, int fa)
{
int best = 0, sum0 = sz(e[now]) == 1 ? 1 : 0, sum1 = 0;
for(auto v: e[now]) if(v != fa)
{
dfs(v, now);
sum0 += dp[v][0];
sum1 += min(dp[v][0], dp[v][1]);
dp[now][1] += dp[v][0];
chmin(best, dp[v][1] - dp[v][0]);
}
dp[now][0] = min(sum0, sum1 + 1);
dp[now][1] += best;
// debug(now, dp[now][0], dp[now][1]);
};
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]);
printf("%d\n", ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 6084kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 4ms
memory: 6068kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 6140kb
input:
8 1 1 2 1 2 3 2
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 0ms
memory: 6128kb
input:
3 1 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 6ms
memory: 6144kb
input:
4 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 0ms
memory: 6104kb
input:
5 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 5ms
memory: 6516kb
input:
6 1 2 2 2 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 3ms
memory: 6016kb
input:
7 1 2 1 4 3 4
output:
1
result:
ok 1 number(s): "1"
Test #9:
score: 0
Accepted
time: 0ms
memory: 6204kb
input:
9 1 1 2 3 1 4 2 7
output:
2
result:
ok 1 number(s): "2"
Test #10:
score: 0
Accepted
time: 0ms
memory: 6008kb
input:
10 1 2 3 3 1 2 3 7 9
output:
2
result:
ok 1 number(s): "2"
Test #11:
score: 0
Accepted
time: 2ms
memory: 6260kb
input:
11 1 1 2 1 5 6 7 3 3 2
output:
2
result:
ok 1 number(s): "2"
Test #12:
score: 0
Accepted
time: 0ms
memory: 5988kb
input:
12 1 1 3 3 2 1 4 7 4 8 10
output:
2
result:
ok 1 number(s): "2"
Test #13:
score: 0
Accepted
time: 1ms
memory: 6100kb
input:
13 1 1 3 4 1 4 4 3 6 1 1 4
output:
2
result:
ok 1 number(s): "2"
Test #14:
score: 0
Accepted
time: 1ms
memory: 6008kb
input:
14 1 2 3 2 4 1 4 7 3 9 7 8 4
output:
3
result:
ok 1 number(s): "3"
Test #15:
score: 0
Accepted
time: 4ms
memory: 6068kb
input:
100 1 2 1 1 3 6 5 5 4 8 4 4 3 14 10 14 8 13 12 18 8 3 7 1 12 10 9 17 26 30 21 11 29 19 20 25 17 10 9 26 6 40 42 29 2 24 14 25 6 41 47 24 21 46 28 8 30 2 19 41 54 43 23 1 65 21 19 35 58 8 71 59 12 2 13 4 16 7 22 58 26 44 1 12 14 80 19 12 43 77 21 54 41 94 37 61 28 82 30
output:
19
result:
ok 1 number(s): "19"
Test #16:
score: 0
Accepted
time: 5ms
memory: 6104kb
input:
100 1 2 3 4 4 6 7 8 9 10 11 10 13 14 15 16 17 17 19 19 15 22 23 15 13 26 27 28 29 30 30 32 28 26 13 36 37 38 39 40 41 42 43 44 45 45 47 48 48 48 51 47 44 54 43 56 56 58 59 60 60 59 63 64 64 66 66 64 69 70 56 72 73 72 75 76 77 42 40 80 40 82 83 83 85 85 85 88 89 88 91 40 38 37 95 96 95 95 2
output:
17
result:
ok 1 number(s): "17"
Test #17:
score: 0
Accepted
time: 4ms
memory: 6284kb
input:
1000 1 2 3 3 5 6 7 8 9 10 11 12 11 14 15 16 16 18 19 19 21 22 23 22 21 26 27 28 29 30 31 28 27 27 35 26 37 38 39 40 41 42 43 44 41 40 47 39 18 50 51 52 53 54 55 56 57 58 59 60 60 58 57 64 65 66 67 66 65 70 71 72 73 72 64 76 76 78 79 80 56 82 18 84 84 86 86 88 88 14 14 92 14 94 94 96 96 98 98 100 101...
output:
169
result:
ok 1 number(s): "169"
Test #18:
score: 0
Accepted
time: 4ms
memory: 6232kb
input:
1000 1 2 2 2 2 2 7 2 2 2 2 2 2 2 15 15 15 2 2 2 21 1 23 23 25 25 23 1 1 1 31 31 31 31 31 31 31 1 39 40 39 42 43 42 42 42 39 39 39 50 50 39 39 54 54 39 57 57 39 60 60 60 60 60 60 39 67 67 69 67 67 67 67 39 75 75 77 77 75 75 75 39 39 39 85 39 39 39 39 39 91 91 93 39 95 95 95 98 98 95 95 95 103 103 105...
output:
152
result:
ok 1 number(s): "152"
Test #19:
score: 0
Accepted
time: 1ms
memory: 6168kb
input:
1000 1 2 3 4 5 6 7 8 7 10 11 11 10 14 6 4 17 17 19 20 3 22 23 23 25 26 23 28 29 30 31 32 33 33 31 36 37 31 3 2 41 42 43 44 42 46 47 48 49 50 51 48 53 54 53 56 57 57 59 60 60 62 63 64 64 66 66 59 69 70 71 70 69 74 75 75 53 78 79 78 78 82 83 53 85 86 86 88 88 88 85 85 85 47 95 96 97 98 99 100 101 100 ...
output:
181
result:
ok 1 number(s): "181"
Test #20:
score: 0
Accepted
time: 4ms
memory: 6048kb
input:
1000 1 2 3 3 5 6 3 8 9 9 3 12 12 12 2 2 17 18 17 20 20 20 17 17 25 17 17 28 2 30 30 32 30 30 35 30 2 38 2 40 41 41 40 40 40 46 47 48 46 50 46 52 46 54 55 54 54 54 2 60 61 62 61 60 60 66 60 60 2 70 71 2 73 73 75 73 77 77 73 80 81 80 83 73 85 85 73 88 88 90 90 88 2 94 95 95 94 98 98 98 101 101 98 104 ...
output:
205
result:
ok 1 number(s): "205"
Test #21:
score: 0
Accepted
time: 3ms
memory: 6596kb
input:
1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 28 30 31 32 33 34 35 36 31 38 39 40 41 42 43 44 44 46 47 47 49 50 44 52 44 54 55 56 57 58 59 60 61 62 63 64 65 66 66 68 69 70 71 72 72 72 75 76 77 78 79 77 81 82 76 84 85 85 71 88 71 90 91 91 93 94 94 96 90 98 99 100 101...
output:
165
result:
ok 1 number(s): "165"
Test #22:
score: 0
Accepted
time: 2ms
memory: 6692kb
input:
1000 1 2 2 2 2 2 2 2 1 1 1 1 13 13 13 13 13 13 13 13 13 13 13 1 25 25 1 1 1 1 1 1 1 1 1 1 1 38 38 38 38 38 38 38 38 38 38 38 38 38 38 52 38 38 38 38 38 38 38 38 38 38 38 38 38 66 38 1 1 70 70 70 70 70 70 70 70 70 70 70 70 1 1 1 1 1 1 88 88 1 1 1 1 1 1 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 ...
output:
60
result:
ok 1 number(s): "60"
Test #23:
score: 0
Accepted
time: 19ms
memory: 9948kb
input:
100000 1 2 3 3 5 6 7 8 9 10 11 12 10 14 14 16 17 18 19 19 21 21 23 23 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 39 41 42 41 44 44 46 47 37 49 50 51 49 49 54 55 55 57 58 59 60 60 62 63 64 64 64 62 62 62 70 70 72 62 74 62 76 77 78 79 76 81 82 83 76 76 86 87 88 88 90 88 88 93 62 95 59 59 98 98 100 1...
output:
16927
result:
ok 1 number(s): "16927"
Test #24:
score: 0
Accepted
time: 27ms
memory: 10012kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 17 18 19 20 21 22 23 24 25 20 27 27 29 30 31 30 27 34 34 36 19 38 39 40 41 39 43 44 43 46 46 48 48 48 51 52 53 53 55 56 57 58 59 60 61 62 62 64 65 66 62 68 69 70 71 72 73 73 75 76 77 78 79 77 76 82 83 84 85 84 87 82 82 76 91 92 93 92 95 91 97 98 98 100 1...
output:
16621
result:
ok 1 number(s): "16621"
Test #25:
score: 0
Accepted
time: 17ms
memory: 9940kb
input:
100000 1 2 3 4 5 6 7 8 9 10 11 12 11 14 15 16 17 18 19 20 20 22 23 24 24 26 27 27 29 24 31 32 33 34 35 36 37 36 39 40 40 40 40 44 44 46 47 47 49 49 47 52 46 54 54 56 56 58 59 60 61 62 63 64 64 66 67 68 69 70 71 72 67 74 75 76 77 77 79 80 81 82 83 84 85 85 85 88 89 88 88 92 93 93 92 92 88 98 99 100 1...
output:
16484
result:
ok 1 number(s): "16484"
Test #26:
score: 0
Accepted
time: 48ms
memory: 10128kb
input:
100000 1 2 3 4 5 5 7 8 9 10 11 10 7 14 15 16 16 7 19 20 21 21 23 21 25 21 20 20 29 30 30 32 33 34 34 33 37 38 39 40 38 33 43 44 43 29 47 48 49 50 51 49 49 48 55 56 57 56 59 60 59 56 63 64 64 63 67 68 67 70 71 63 73 63 75 76 77 76 75 80 55 47 83 83 85 85 87 88 89 90 89 83 93 94 47 96 47 98 99 99 47 1...
output:
18227
result:
ok 1 number(s): "18227"
Test #27:
score: 0
Accepted
time: 20ms
memory: 10004kb
input:
100000 1 2 2 4 5 6 7 8 9 10 11 12 13 14 15 16 16 16 19 20 21 22 23 24 25 26 27 28 29 30 31 32 29 34 28 36 37 37 26 40 41 42 42 44 45 46 44 48 49 44 44 40 53 54 55 53 57 58 59 60 60 62 62 64 65 60 67 68 67 67 67 59 73 74 75 76 40 78 40 80 81 82 81 84 84 86 87 88 89 86 91 92 93 94 94 93 97 97 99 93 10...
output:
17563
result:
ok 1 number(s): "17563"
Test #28:
score: 0
Accepted
time: 25ms
memory: 10436kb
input:
100000 1 2 3 3 3 3 3 2 2 2 2 2 2 14 14 14 14 14 14 14 2 2 23 2 25 25 2 2 29 2 2 32 32 32 32 32 2 2 2 40 2 2 43 43 43 2 47 47 2 2 51 51 1 54 54 1 57 57 57 57 1 62 63 62 62 66 62 68 68 62 62 72 62 62 62 76 76 62 79 62 62 62 1 84 84 86 86 86 86 86 86 86 84 84 95 95 95 84 99 99 99 99 99 99 84 106 106 10...
output:
11610
result:
ok 1 number(s): "11610"
Test #29:
score: 0
Accepted
time: 1ms
memory: 5992kb
input:
30 1 1 1 1 2 6 7 8 9 10 10 11 12 13 14 15 15 17 18 19 20 21 22 23 24 25 26 26 26
output:
3
result:
ok 1 number(s): "3"