QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#585873 | #7798. Colorful Village | phuong2222 | WA | 3ms | 12264kb | C++14 | 2.7kb | 2024-09-23 22:35:41 | 2024-09-23 22:35:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using lli = long long;
const int maxN = 2e5 + 5;
int n,a[maxN],op[maxN],ma[maxN];
int ch[maxN],val[maxN],st,p[maxN];
vector<int> adj[maxN];
vector<int> res;
void input()
{
cin >> n;
for (int i = 1;i <= 2 * n;++i)
{
adj[i].clear();op[i] = 0;
ma[i] = 0;val[i] = 0;ch[i] = 0;
}
for (int i = 1;i <= 2 * n;++i)
{
cin >> a[i];
op[a[i]] += i;
}
for (int i = 1;i < 2 * n;++i)
{
int u,v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
st = op[a[1]] - 1;
}
void dfs(int u,int par)
{
ch[u] = 0;
for (int v : adj[u])
{
if (v == par) continue;
dfs(v,u);
ch[u] = max(ch[v],ch[u]);
}
if (ma[op[a[u]] - u] == 1) ch[u] = 1;
ma[u] = 1;
p[u] = par;
}
bool check()
{
for (int i = 1;i <= 2 * n;++i) if (ch[i] & ch[op[a[i]] - i]) return false;
return true;
}
void calc(int u,int par)
{
for (int v : adj[u])
{
if (v == par) continue;
val[v] = -1;
val[op[a[v]] - v] = 1;
calc(v,u);
}
}
void sol(int u,int par)
{
// cout << u << " " << val[u] << "\n";
if (val[u] == 0)
{
int v = op[a[u]] - u;
if (ch[v])
{
val[u] = -1;
calc(u,p[u]);
val[v] = 1;
}
else
{
// cout << u << "a\n";
res.push_back(u);
val[v] = -1;
calc(v,p[v]);
val[u] = 1;
}
// if (u == 1) cout << val[v] << "a\n";
}
else if (val[u] == 1) res.push_back(u);
else return;
for (int v : adj[u])
{
if (v == par) continue;
sol(v,u);
}
}
void solve1()
{
for (int i = 1;i <= 2 * n;++i)
{
ma[i] = 0;val[i] = 0;ch[i] = 0;
}
dfs(st,0);
if (check())
{
sol(st,0);
if (res.size() == n)
{
for (int s : res)
cout << s << " ";
cout << "\n";
}
else cout << -1 << "\n";
}
else cout << -1 << "\n";
}
void solve()
{
dfs(1,0);
// for (int i = 1;i <= 2 * n;++i) cout << ch[i] << "\n";
if (check())
{
sol(1,0);
if (res.size() == n)
{
for (int s : res)
cout << s << " ";
cout << "\n";
}
else solve1();
}
else solve1();
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("P4.inp","r",stdin);
//freopen("P4.out","w",stdout);
int t;
cin >> t;
while (t--)
{
input();
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 12264kb
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 7 5 2 -1
result:
ok ok, 1 yes, 1 no (2 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 11604kb
input:
1 1 1 1 1 2
output:
1
result:
ok ok, 1 yes, 0 no (1 test case)
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 11028kb
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 -1 -1 -1 -1
result:
wrong answer jury found a solution but participant did not (test case 2)