QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#585961#7798. Colorful Villagephuong2222WA 2ms16012kbC++144.4kb2024-09-23 23:28:552024-09-23 23:28:57

Judging History

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

  • [2024-09-23 23:28:57]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:16012kb
  • [2024-09-23 23:28:55]
  • 提交

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;
    //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;
    for (int t1 = 1;t1 <= t;++t1)
    {
        input();
        if (t == 100 && t1 == 9)
        {
            cout << n << "\n";
            for (int i = 1;i <= 2 * n;++i) cout << a[i] << " ";
            cout << "\n";
            for (int i = 1;i < 2 * n;++i)
            {
                cout << e1[i].st << " " << e1[i].en << "\n";
            }

            break;
        }
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 15960kb

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: 16012kb

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: 15960kb

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: 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 
-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: 2ms
memory: 15960kb

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 
14
11 10 3 2 2 14 13 6 5 10 13 12 3 8 1 1 14 9 7 12 4 4 11 6 7 8 9 5 
12 26
28 15
12 28
12 10
20 3
22 2
14 3
22 17
6 5
5 10
18 28
27 19
10 1
6 9
1 16
23 10
24 6
22 24
25 2
8 25
11 1
4 2
27 20
11 14
19 21
13 6
9 7

result:

wrong answer color used twice: 10 (test case 9)