0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 17:56:28 +00:00

#1809. 【2017.3 长乐省选集训 Day5 T2】红与蓝

https://sjzezoj.com/submission/66267
This commit is contained in:
Baoshuo Ren 2022-12-25 21:10:57 +08:00
parent 3a98ecdfc6
commit 132ef5c2b5
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
25 changed files with 169 additions and 0 deletions

97
S2OJ/1809/1809.cpp Normal file
View File

@ -0,0 +1,97 @@
#include <iostream>
#include <functional>
#include <set>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
std::vector<int> c(n + 1), f(n + 1);
std::set<int> ans;
std::vector<std::vector<int>> g(n + 1);
for (int i = 1, x; i <= n; i++) {
cin >> x;
g[x].emplace_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
std::function<void(int)> dfs = [&](int u) -> void {
if (g[u].empty()) {
if (c[u] == 0) f[u] = 1;
else if (c[u] == 1) f[u] = -1;
return;
}
for (int v : g[u]) {
dfs(v);
if (f[v] > 0) f[u]++;
else if (f[v] < 0) f[u]--;
}
};
dfs(1);
if (f[1] > 0) {
for (int i = 1; i <= n; i++) {
if (g[i].empty() && c[i] == -1) {
ans.emplace(i);
}
}
cout << ans.size() << ' ';
for (int x : ans) cout << x << ' ';
cout << endl;
} else if (f[1] == 0) {
std::function<void(int)> dfs = [&](int u) -> void {
if (g[u].empty()) {
if (c[u] == -1) {
ans.emplace(u);
}
return;
}
for (int v : g[u]) {
if (f[v] == 0 || f[v] == -1) {
dfs(v);
}
}
};
dfs(1);
cout << ans.size() << ' ';
for (int x : ans) cout << x << ' ';
cout << endl;
} else { // f[1] < 0
cout << -1 << endl;
}
}
return 0;
}

BIN
S2OJ/1809/data/chk.cpp (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/ex_rab1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/ex_rab1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/problem.conf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
S2OJ/1809/data/rab9.out (Stored with Git LFS) Normal file

Binary file not shown.