QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#793948 | #9184. Team Coding | Mousa_Aboubaker | 19 | 2308ms | 37044kb | C++20 | 3.8kb | 2024-11-30 06:08:32 | 2024-11-30 06:08:32 |
Judging History
answer
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <deque>
#include <climits>
#include <cmath>
#include <numeric>
#include <string>
#include <bitset>
#include <assert.h>
#include <iomanip>
using namespace std;
template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const long long infl = 1e18 + 1;
const int inf = 1e9 + 1;
const int mod1 = 1e9 + 7;
const int mod2 = 998244353;
const long double eps = 1e-7;
const int mod = mod1;
int add(int a, int b) { return (a + b) % mod; }
int sub(int a, int b) { return (a - b + mod) % mod; }
int mul(int a, int b) { return (int)((long long)a * b % mod); }
int pwr(int a, int b = mod - 2)
{
int res = 1;
for(; b > 0; b >>= 1, a = mul(a, a))
if(b & 1)
res = mul(res, a);
return res;
}
void solve()
{
int n, k;
cin >> n >> k;
vector<int> c(n);
for(auto &i: c)
cin >> i;
vector adj(n, vector<int>());
for(int i = 1; i < n; i++)
{
int u;
cin >> u;
adj[u].push_back(i);
}
vector<int> dep(n, 0);
vector up(n, vector<int>(21, -1));
auto dfs1 = [&](auto self, int u, int d) -> void
{
dep[u] = d++;
for(auto i: adj[u])
{
up[i][0] = u;
self(self, i, d);
}
};
dfs1(dfs1, 0, 0);
for(int i = 1; i < 21; i++)
for(int j = 0; j < n; j++)
if(~up[j][i - 1])
up[j][i] = up[up[j][i - 1]][i - 1];
auto get_kth_par = [&](int u, int k) -> int
{
for(int i = 0; i < 21; i++)
if((k >> i) & 1)
u = up[u][i];
return u;
};
int mx = *max_element(dep.begin(), dep.end());
vector levels(mx + 1, vector<int>());
vector whole(mx + 1, vector<int>(2, 0));
for(int i = 0; i < n; i++)
levels[dep[i]].push_back(i);
int cnt1 = 0;
for(int i = 0; i < n; i++)
{
whole[dep[i]][0] += c[i] == c[0];
whole[dep[i]][1] += c[i] != c[0];
if(c[i] == c[0])
cnt1++;
}
vector<int> others;
auto dfs2 = [&](auto self, int u) -> void
{
if(c[u] != c[0])
{
others.push_back(u);
return;
}
for(auto i: adj[u])
self(self, i);
};
dfs2(dfs2, 0);
vector<int> mxx(mx + 1, 0), have(mx + 1, 0);
auto dfs3 = [&](auto self, int u) -> void
{
mxx[dep[u]]++;
if(c[u] != c[0])
{
have[dep[u]]++;
}
for(auto i: adj[u])
self(self, i);
};
int mxxx = cnt1, mn = 0;
int curr1 = 0, curr2 = 0;
for(auto i: others)
{
curr1 = 0, curr2 = 0;
dfs3(dfs3, i);
for(int j = dep[i]; j <= mx; j++)
{
curr2 += min(whole[j][1] - have[j], mxx[j] - have[j]);
curr1 += min(whole[j][1], mxx[j]);
have[j] = 0;
mxx[j] = 0;
}
if(curr1 > mxxx)
{
mxxx = curr1;
mn = curr2;
}
else if(curr1 == mxxx)
mn = min(mn, curr2);
}
cout << mxxx << ' ' << mn;
/*
for(int i = 0; i < n; i++)
{
curr1 = 1, curr2 = 0;
for(int j = dep[i] + 1; j <= mx; j++)
{
int all = 0, have = 0, mxx = 0;
for(auto x: levels[j])
{
if(get_kth_par(x, j - dep[i]) == i)
{
mxx++;
if(c[x] == c[i])
{
have++;
all++;
}
}
else if(c[x] == c[i])
{
all++;
}
}
curr2 += min(mxx - have, all - have);
curr1 += min(mxx, all);
}
if(curr1 > mxxx)
{
mxxx = curr1;
mn = curr2;
}
else if(curr1 == mxxx)
{
mn = min(mn, curr2);
}
}
cout << mxxx << ' ' << mn;
*/
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << (t ? "\n" : "");
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 12
Accepted
time: 0ms
memory: 3784kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #2:
score: 12
Accepted
time: 0ms
memory: 3560kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
4 0
result:
ok single line: '4 0'
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 3660kb
input:
10 9 6 5 6 2 5 4 5 3 4 1 0 1 2 3 4 5 6 7 8
output:
8 0
result:
wrong answer 1st lines differ - expected: '3 0', found: '8 0'
Subtask #2:
score: 19
Accepted
Test #17:
score: 19
Accepted
time: 0ms
memory: 3504kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #18:
score: 19
Accepted
time: 0ms
memory: 3576kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
4 0
result:
ok single line: '4 0'
Test #19:
score: 19
Accepted
time: 0ms
memory: 3812kb
input:
100 2 0 1 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 1 0 1 0 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 0 0 1 1 0 1 1 0 1 1 1 1 0 0 0 1 1 0 0 0 1 1 0 1 0 0 0 0 1 1 0 1 0 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 29 30 31 32 33 34...
output:
50 0
result:
ok single line: '50 0'
Test #20:
score: 19
Accepted
time: 1ms
memory: 4176kb
input:
2000 2 0 0 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 0 1 1 0 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 0...
output:
1001 0
result:
ok single line: '1001 0'
Test #21:
score: 19
Accepted
time: 31ms
memory: 37044kb
input:
100000 2 0 0 1 0 0 0 1 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 0 1 0 1 0 0...
output:
50198 0
result:
ok single line: '50198 0'
Test #22:
score: 19
Accepted
time: 0ms
memory: 3576kb
input:
99 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 65 53 0 0 41 0 59 26 87 78 62 23 52 0 97 0 0 90 0 4 77 55 0 73 45 0 19 92 32 0 0 0 82 48 60 54 72...
output:
2 0
result:
ok single line: '2 0'
Test #23:
score: 19
Accepted
time: 1ms
memory: 4140kb
input:
1999 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
2 0
result:
ok single line: '2 0'
Test #24:
score: 19
Accepted
time: 21ms
memory: 20556kb
input:
99999 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
output:
2 0
result:
ok single line: '2 0'
Test #25:
score: 19
Accepted
time: 0ms
memory: 3616kb
input:
100 2 1 1 1 0 0 1 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 0 1 0 0 0 1 1 1 1 1 0 1 0 1 1 0 1 1 0 0 1 0 1 0 1 0 0 0 1 0 1 0 0 1 1 0 0 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 68 34 15 37 60 98 14 80 81 1 94 60 1 45 55 5 29 93 55 41 54 54 96 70 2 32 62 94 15 37 41 90 68...
output:
51 0
result:
ok single line: '51 0'
Test #26:
score: 19
Accepted
time: 0ms
memory: 4176kb
input:
2000 2 1 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 1 0 1 0 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 0 1 1 0 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 1 0 0 0 1 1 1 1 0...
output:
1001 0
result:
ok single line: '1001 0'
Test #27:
score: 19
Accepted
time: 2308ms
memory: 31136kb
input:
100000 2 0 0 1 1 1 0 1 1 1 1 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 0 1 0 1 1 1 0 0 0 1 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 1 0 1 0 1 1 1 0 1 0 1 0 0 0 0 0 1 0 1 1 1 1 1 0 0 0 0 0 0 0...
output:
50001 0
result:
ok single line: '50001 0'
Test #28:
score: 19
Accepted
time: 11ms
memory: 19548kb
input:
100000 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
7 2
result:
ok single line: '7 2'
Test #29:
score: 19
Accepted
time: 0ms
memory: 3496kb
input:
10 2 1 1 1 1 0 1 1 1 1 0 8 8 8 3 9 0 9 0 0
output:
8 0
result:
ok single line: '8 0'
Test #30:
score: 19
Accepted
time: 0ms
memory: 3632kb
input:
100 2 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 1 1 1 0 0 1 0 0 1 0 0 0 0 0 0 1 1 1 1 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 0 0 1 1 1 0 1 1 1 1 0 1 1 0 1 0 1 27 11 39 3 90 87 91 49 39 95 91 49 89 15 64 27 85 38 43 86 51 28 51 64 54 28 69 87 4 54 27 24 ...
output:
46 0
result:
ok single line: '46 0'
Test #31:
score: 19
Accepted
time: 27ms
memory: 20512kb
input:
100000 2 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 1 1 0 1 1 1 1 0 1 0 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 1 0 1 0 0 1 0 1 1 1 1 0 1 0 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0...
output:
50040 0
result:
ok single line: '50040 0'
Test #32:
score: 19
Accepted
time: 32ms
memory: 20316kb
input:
100000 2 1 0 0 1 0 0 1 0 1 1 1 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 1 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 1 0 1 1 0 0 0 0 1 0 1 0 1 1 1 1 1 0 1 0 1 1 1 1 0 0 1 0 1 1 1 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0...
output:
50006 0
result:
ok single line: '50006 0'
Test #33:
score: 19
Accepted
time: 0ms
memory: 3632kb
input:
100 2 1 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 0 0 0 0 1 47 84 42 64 90 84 0 84 7 56 35 59 80 98 16 73 56 7 47 33 6 16 24 70 97 3 22 8 19 18 97 7 84 79...
output:
47 12
result:
ok single line: '47 12'
Test #34:
score: 19
Accepted
time: 1ms
memory: 3852kb
input:
2000 2 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
1127 147
result:
ok single line: '1127 147'
Test #35:
score: 19
Accepted
time: 25ms
memory: 20276kb
input:
100000 2 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
39105 5115
result:
ok single line: '39105 5115'
Test #36:
score: 19
Accepted
time: 36ms
memory: 20460kb
input:
100000 2 0 0 0 1 0 1 1 1 1 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1...
output:
61321 12576
result:
ok single line: '61321 12576'
Test #37:
score: 19
Accepted
time: 22ms
memory: 20212kb
input:
100000 2 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1 1 0 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1...
output:
75885 8789
result:
ok single line: '75885 8789'
Test #38:
score: 19
Accepted
time: 0ms
memory: 3820kb
input:
4 2 0 1 0 0 0 0 1
output:
3 0
result:
ok single line: '3 0'
Subtask #3:
score: 0
Wrong Answer
Test #39:
score: 27
Accepted
time: 0ms
memory: 3556kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #40:
score: 27
Accepted
time: 0ms
memory: 3600kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
4 0
result:
ok single line: '4 0'
Test #41:
score: 0
Wrong Answer
time: 0ms
memory: 3616kb
input:
10 9 6 5 6 2 5 4 5 3 4 1 0 1 2 3 4 5 6 7 8
output:
8 0
result:
wrong answer 1st lines differ - expected: '3 0', found: '8 0'
Subtask #4:
score: 0
Wrong Answer
Test #76:
score: 23
Accepted
time: 0ms
memory: 3552kb
input:
1 1 0
output:
1 0
result:
ok single line: '1 0'
Test #77:
score: 23
Accepted
time: 0ms
memory: 3628kb
input:
5 2 0 1 0 0 0 0 1 2 3
output:
4 0
result:
ok single line: '4 0'
Test #78:
score: 0
Wrong Answer
time: 0ms
memory: 3816kb
input:
10 9 6 5 6 2 5 4 5 3 4 1 0 1 2 3 4 5 6 7 8
output:
8 0
result:
wrong answer 1st lines differ - expected: '3 0', found: '8 0'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%