QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#585986 | #7798. Colorful Village | phuong2222 | WA | 3ms | 17900kb | C++14 | 4.1kb | 2024-09-23 23:40:17 | 2024-09-23 23:40:24 |
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],e1[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;
v.clear();
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;
e1[i].st = u;e1[i].en = 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)
{
if (t == 100) return;
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";
val[v] = -1;
calc(v,p[v]);
val[u] = 1;
}
// if (u == 1) cout << val[v] << "a\n";
}
else if (val[u] == -1) 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);
for (int i = 1;i <= 2 * n;++i) if (val[i] == 1) res.push_back(i);
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);
for (int i = 1;i <= 2 * n;++i) if (val[i] == 1) res.push_back(i);
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;
for (int t1 = 1;t1 <= t;++t1)
{
input();
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 15884kb
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: 15948kb
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: 3ms
memory: 17900kb
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 2 4 2 4 6 7 1
result:
ok ok, 5 yes, 0 no (5 test cases)
Test #4:
score: -100
Wrong Answer
time: 3ms
memory: 15880kb
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 2 4 7 9 11 13 14 15 16 18 21 23 24 29 30 1 3 4 5 6 9 10 11 12 15 22 24 26 27 29 31 32 33 37 40 42 44 45 -1 -1 1 2 3 5 6 1 2 5 7 8 2 4 7 9 11 12 13 16 1 5 8 9 13 14 15 16 17 18 21 -1
result:
wrong answer subtree not connected: only 13 edges for 15 vertices (test case 2)