QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#197966 | #6958. Equivalence | PPP# | AC ✓ | 608ms | 137324kb | C++17 | 1.7kb | 2023-10-02 22:50:00 | 2023-10-02 22:50:01 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
mt19937_64 rnd(228);
typedef unsigned long long ull;
const int maxN = 1e6 + 10;
ull f[maxN];
int n;
int p[maxN];
int val[maxN];
ull f0[maxN], f1[maxN];
int p2[maxN];
vector<int> g[maxN];
vector<int> g2[maxN];
void dfs(int v, int p) {
f0[v] ^= f[v];
for (int to : g[v]) {
if (to == p) continue;
dfs(to, v);
f0[v] ^= f0[to];
}
}
void dfs2(int v, int p) {
f1[v] ^= f[v];
for (int to : g2[v]) {
if (to == p) continue;
dfs2(to, v);
f1[v] ^= f1[to];
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
f[i] = rnd();
f0[i] = f1[i] = 0;
g[i].clear();
g2[i].clear();
}
for (int i = 2; i <= n; i++) {
cin >> p[i];
g[i].emplace_back(p[i]);
g[p[i]].emplace_back(i);
}
for (int i = 2; i <= n; i++) {
cin >> val[i];
}
for (int i = 2; i <= n; i++) {
cin >> p2[i];
g2[i].emplace_back(p2[i]);
g2[p2[i]].emplace_back(i);
}
dfs(1, -1);
dfs2(1, -1);
unordered_map<ull,int> mp;
for (int i = 2; i <= n; i++) {
mp[f1[i]]++;
}
int ans = 0;
for (int i = 2; i <= n; i++) {
if (val[i] > 0 && !mp.count(f0[i])) {
ans++;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
int tst;
cin >> tst;
while (tst--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 608ms
memory: 137324kb
input:
8514 265000 72963 99588 118133 209949 249723 133275 171942 167920 86420 191069 169911 123458 176051 140795 122013 123039 252726 229192 52056 65706 247786 155474 231748 50060 24682 114313 43549 53896 73048 149564 51333 62885 117376 255302 235170 95044 56347 246767 193580 219540 227757 92735 173157 17...
output:
75123 27033 26721 233600 1082 1079 1077 1131 1055 1052 1026 1075 1013 1144 0 0 0 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 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0...
result:
ok 8514 lines