QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#32658 | #2569. Kill All Termites | YaoBIG# | WA | 4ms | 7184kb | C++17 | 2.6kb | 2022-05-22 21:26:13 | 2022-05-22 21:26:14 |
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, min(dp[v][1], dp[v][2]) - 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;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 7184kb
input:
1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 2ms
memory: 6452kb
input:
2 1
output:
1
result:
ok 1 number(s): "1"
Test #3:
score: 0
Accepted
time: 2ms
memory: 6136kb
input:
8 1 1 2 1 2 3 2
output:
2
result:
ok 1 number(s): "2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 6104kb
input:
3 1 1
output:
1
result:
ok 1 number(s): "1"
Test #5:
score: 0
Accepted
time: 3ms
memory: 5988kb
input:
4 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #6:
score: 0
Accepted
time: 4ms
memory: 6016kb
input:
5 1 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #7:
score: 0
Accepted
time: 4ms
memory: 6108kb
input:
6 1 2 2 2 1
output:
1
result:
ok 1 number(s): "1"
Test #8:
score: 0
Accepted
time: 2ms
memory: 6972kb
input:
7 1 2 1 4 3 4
output:
1
result:
ok 1 number(s): "1"
Test #9:
score: 0
Accepted
time: 4ms
memory: 6620kb
input:
9 1 1 2 3 1 4 2 7
output:
2
result:
ok 1 number(s): "2"
Test #10:
score: 0
Accepted
time: 1ms
memory: 6104kb
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: 3ms
memory: 6164kb
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: 3ms
memory: 6060kb
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: 3ms
memory: 6696kb
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: 2ms
memory: 6108kb
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: 3ms
memory: 6204kb
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: -100
Wrong Answer
time: 3ms
memory: 6076kb
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:
21
result:
wrong answer 1st numbers differ - expected: '17', found: '21'