QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#585938#7798. Colorful Villagephuong2222WA 15ms15944kbC++144.0kb2024-09-23 23:15:192024-09-23 23:15:20

Judging History

你现在查看的是最新测评结果

  • [2024-09-23 23:15:20]
  • 评测
  • 测评结果:WA
  • 用时:15ms
  • 内存:15944kb
  • [2024-09-23 23:15:19]
  • 提交

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();
    }
}

详细

Test #1:

score: 100
Accepted
time: 3ms
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:

3 5 7 2 
-1

result:

ok ok, 1 yes, 1 no (2 test cases)

Test #2:

score: 0
Accepted
time: 0ms
memory: 15884kb

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: 0ms
memory: 15920kb

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 
2 7 6 4 
1 

result:

ok ok, 5 yes, 0 no (5 test cases)

Test #4:

score: 0
Accepted
time: 0ms
memory: 13840kb

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 5 3 6 
1 7 5 8 2 
13 12 11 7 9 4 2 16 
1 5 21 16 8 15 14 18 17 13 9 
-1

result:

ok ok, 5 yes, 5 no (10 test cases)

Test #5:

score: -100
Wrong Answer
time: 15ms
memory: 15944kb

input:

100
12
1 2 2 8 10 9 12 11 10 4 12 6 9 3 3 7 5 1 7 6 11 5 8 4
6 5
24 7
2 12
11 3
20 15
8 10
7 10
18 16
15 14
19 2
10 14
18 14
9 14
17 5
12 18
4 23
5 20
10 13
1 4
16 22
11 2
3 21
4 24
2
1 2 1 2
2 1
3 4
1 3
3
1 1 2 2 3 3
1 4
6 4
4 5
2 6
3 4
8
1 8 6 8 4 6 7 5 5 7 4 3 2 1 2 3
9 3
2 16
8 10
1 13
13 2
13 1...

output:

-1
1 2 
1 4 6 
1 13 2 16 3 9 7 11 
1 6 3 
-1
1 
1 2 5 8 
-1
1 6 11 9 8 7 16 5 
-1
-1
-1
1 
1 2 19 4 7 9 15 3 17 13 
2 3 5 
-1
-1
-1
1 
1 
1 9 15 2 11 3 5 12 
1 5 2 6 
1 2 
1 
1 
-1
-1
1 4 2 9 5 
5 6 3 
-1
-1
-1
1 2 
1 3 
1 
-1
2 4 
1 6 2 
-1
1 4 
-1
-1
-1
1 5 2 7 
-1
-1
-1
1 10 9 2 3 
1 
-1
-1
-1
1 ...

result:

wrong answer jury found a solution but participant did not (test case 9)