QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#584245 | #7798. Colorful Village | rtgsp | WA | 6ms | 17904kb | C++20 | 3.2kb | 2024-09-23 10:49:56 | 2024-09-23 10:49:57 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const int maxn = 1e6 + 2, mod = 1e9 + 7;
int q, n, u, v, t, c[maxn], low[maxn], num[maxn], sccc, scc[maxn], id[maxn];
bool del[maxn], res[maxn], in_topo[maxn];
vector<int> l[maxn], adj_t[maxn], adj_g[maxn], new_adj[maxn], topo;
stack<int> st;
void init()
{
for (int i = 1; i <= 4*n; i++)
{
scc[i] = id[i] = low[i] = num[i] = del[i] = res[i] = in_topo[i] = 0;
adj_g[i].clear(); new_adj[i].clear();
}
t = 0; sccc = 0;
}
void add_edge (int u, bool fu, int v, bool fv)
{
adj_g[u + (fu ? 2*n : 0)].push_back(v + (fv ? 0 : 2*n));
adj_g[v + (fv ? 2*n : 0)].push_back(u + (fu ? 0 : 2*n));
}
void dfs (int u, int p)
{
if (p == 0) add_edge(u, 1, u, 1);
else add_edge(u, 0, p, 1);
for (int v : adj_t[u])
{
if (v == p) continue;
dfs(v, u);
}
}
void tarjan (int u)
{
low[u] = num[u] = ++t;
st.push(u);
for (int v : adj_g[u])
{
if (del[v]) continue;
if (!num[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u])
{
sccc++;
int v = 0;
do
{
v = st.top();
scc[v] = sccc;
del[v] = true;
st.pop();
}
while (v != u);
}
}
void topo_sort (int u)
{
in_topo[u] = true;
for (int v : new_adj[u])
if (!in_topo[v]) topo_sort(v);
topo.push_back(u);
}
bool try_node (int u)
{
init();
for (int i = 1; i <= n; i++)
{
add_edge(l[i][0], 1, l[i][1], 1);
add_edge(l[i][0], 0, l[i][1], 0);
}
dfs(u, 0);
for (int i = 1; i <= 4*n; i++)
if (!num[i]) tarjan(i);
for (int i = 1; i <= 4*n; i++)
for (int j : adj_g[i])
new_adj[scc[i]].push_back(scc[j]);
for (int i = 1; i <= sccc; i++)
if (!in_topo[i]) topo_sort(i);
reverse(topo.begin(), topo.end());
for (int i = 0; i < (int)topo.size(); i++)
id[topo[i]] = i;
for (int i = 1; i <= 2*n; i++)
{
if (scc[i] == scc[i + 2*n]) return false;
res[i] = id[scc[i]] > id[scc[i + 2*n]];
}
return true;
}
int main()
{
//freopen(task".INP", "r", stdin);
//freopen(task".OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> q;
while (q--)
{
cin >> n;
for (int i = 1; i <= 2*n; i++)
{
adj_t[i].clear();
l[i].clear();
}
for (int i = 1; i <= 2*n; i++)
{
cin >> c[i];
l[c[i]].push_back(i);
}
for (int i = 1; i < 2*n; i++)
{
cin >> u >> v;
adj_t[u].push_back(v);
adj_t[v].push_back(u);
}
if (try_node(l[1][0]))
{
for (int i = 1; i <= 2*n; i++)
if (res[i]) cout << i << " ";
}
else if (try_node(l[1][1]))
{
for (int i = 1; i <= 2*n; i++)
if (res[i]) cout << i << " ";
}
else cout << -1;
cout << '\n';
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 6ms
memory: 16060kb
input:
2 4 1 3 1 3 4 4 2 2 1 6 5 3 2 4 7 1 7 5 5 8 2 5 3 1 1 2 2 3 3 1 2 2 3 3 4 4 5 5 6
output:
1 2 5 7 -1
result:
ok ok, 1 yes, 1 no (2 test cases)
Test #2:
score: 0
Accepted
time: 3ms
memory: 17904kb
input:
1 1 1 1 1 2
output:
1
result:
ok ok, 1 yes, 0 no (1 test case)
Test #3:
score: 0
Accepted
time: 6ms
memory: 15860kb
input:
5 1 1 1 2 1 1 1 1 2 1 3 3 2 3 1 1 2 4 1 6 1 3 5 5 1 2 4 4 3 3 1 1 4 4 2 2 2 7 7 6 1 2 8 1 4 2 2 5 3 1 1 1 1 1 2
output:
1 2 1 2 4 2 4 5 7 2
result:
ok ok, 5 yes, 0 no (5 test cases)
Test #4:
score: -100
Wrong Answer
time: 6ms
memory: 16084kb
input:
10 3 3 2 3 1 1 2 3 1 3 4 2 5 5 3 6 4 15 8 9 13 12 2 4 11 15 10 10 1 12 8 6 15 7 9 5 3 6 14 14 13 3 1 7 11 5 4 2 24 30 1 26 2 16 4 7 30 19 3 19 13 11 21 25 23 22 30 4 19 27 17 26 16 17 21 7 23 25 15 11 10 8 4 16 2 11 14 16 5 10 22 6 11 5 21 18 15 29 24 9 28 19 23 12 13 20 23 21 20 5 9 18 20 21 9 3 6 ...
output:
3 4 6 -1 -1 -1 -1 1 2 3 5 6 1 2 5 7 8 1 3 5 6 8 10 11 15 1 2 5 6 11 13 14 16 19 21 22 -1
result:
wrong answer subtree not connected: only 3 edges for 8 vertices (test case 8)