QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#585947 | #7798. Colorful Village | phuong2222 | WA | 4ms | 15912kb | C++14 | 4.0kb | 2024-09-23 23:22:28 | 2024-09-23 23:22:29 |
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],s,p[maxN],e[maxN],id[maxN],t;
vector<int> adj[maxN];
vector<int> res;
vector<pair<int,int>> v;
priority_queue<pair<int,int>> q;
struct Tdata
{
int st,en,id;
bool operator < (const Tdata& other) const
{
if (st == other.st) return en < other.en;
return st < other.st;
}
}
el[maxN];
void reset()
{
res.clear();
for (int i = 1;i <= 2 * n;++i)
{
ma[i] = 0;val[i] = 0;ch[i] = 0;
p[i] = 0;e[i] = 0;id[i] = 0;
}
t = 0;
while (!q.empty()) q.pop();
}
void input()
{
cin >> n;
for (int i = 1;i <= 2 * n;++i) {adj[i].clear();op[i] = 0;}
reset();
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);
}
s = op[a[1]] - 1;
}
void dfs(int u,int par)
{
e[++t] = u;
id[u] = t;el[u].st = t;
el[u].id = u;
for (int v : adj[u])
{
if (v == par) continue;
dfs(v,u);
}
p[u] = par;
el[u].en = t;
}
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()
{
reset();
dfs(s,0);
for (int i = 1;i <= 2 * n;++i)
if (id[i] < id[op[a[i]] - i]) v.push_back({id[i],id[op[a[i]] - i]});
sort(v.begin(),v.end());
sort(el + 1,el + 2 * n + 1);
int j = 0;
for (int i = 1;i <= 2 * n;++i)
{
while (v[j].first <= el[i].en && j < v.size()) {q.push({-v[j].second,j});++j;}
while (!q.empty() && v[q.top().second].first < el[i].st) q.pop();
if (!q.empty() && -q.top().first <= el[i].en) ch[el[i].id] = 1;
}
if (check())
{
sol(s,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)
if (id[i] < id[op[a[i]] - i]) v.push_back({id[i],id[op[a[i]] - i]});
sort(v.begin(),v.end());
sort(el + 1,el + 2 * n + 1);
int j = 0;
for (int i = 1;i <= 2 * n;++i)
{
while (v[j].first <= el[i].en && j < v.size()) {q.push({-v[j].second,j});++j;}
while (!q.empty() && v[q.top().second].first < el[i].st) q.pop();
if (!q.empty() && -q.top().first <= el[i].en) ch[el[i].id] = 1;
}
//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();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15880kb
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: 4ms
memory: 15912kb
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: 0ms
memory: 15848kb
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 4 2 -1 1
result:
wrong answer jury found a solution but participant did not (test case 4)