QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#585465#7798. Colorful Village_laxWA 9ms32532kbC++203.5kb2024-09-23 20:54:102024-09-23 20:54:10

Judging History

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

  • [2024-09-23 20:54:10]
  • 评测
  • 测评结果:WA
  • 用时:9ms
  • 内存:32532kb
  • [2024-09-23 20:54:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
const ll N = 5e5 + 5;
#define bit(x,y) ((x >> y) & 1LL)
const ll inf = LLONG_MAX/5;
vector<ll> a[N];
vector<ll> p[N];
set<ll> a2[N];
vector<ll> haha[N];
vector<ll> c[N];
ll low[N],num[N],cnt,g[N],seen[N],cg;
void build(ll x, ll px)
{
    if(x != 1)
    {
        ll t = x * 2;
        ll pt = px * 2;
        ll ut = t - 1;
        ll upt = pt - 1;
        a[t].push_back(pt);
        a[upt].push_back(ut);
    }
    for(auto y : p[x])
    {
        if(y != px)
        {
            build(y,x);
        }
    }
}
stack<ll> st;
void scc(ll x)
{
    st.push(x);
    low[x] = num[x] = cnt++;
    for(auto y : a[x])
    {
        if(num[y] == 0)
        {
            scc(y);
            low[x] = min(low[x],low[y]);
        }else
        {
            if(g[y] == 0) low[x] = min(low[x],num[y]);
        }
    }
    if(num[x] == low[x])
    {
        g[x] = ++cg;
        haha[cg].push_back(x);
        while(st.top() != x)
        {
            g[st.top()] = g[x];
            haha[cg].push_back(st.top());
            st.pop();
        }
        st.pop();
    }
}
void mark(ll x)
{
    for(auto y : a2[x])
    {
        if(seen[y] == 0)
        {
            mark[y];
        }
    }
    if(seen[x] != 0) return;
    seen[x] = 1;
    for(auto y : a2[x])
    {
        if(seen[y] == -1) seen[x] = -1;
    }
    if(seen[x] == 1)
    {
        for(auto y : haha[x])
        {
            ll uy;
            if(y % 2 == 0) uy = y - 1;
            else uy = y + 1;
            seen[g[uy]] = -1;
        }
    }
}
void clea(ll n)
{
    ll i,j;
    for(i  = 1;i <= n * 4;i++)
    {
        a[i].clear();
        a2[i].clear();
        haha[i].clear();
        c[i].clear();
        p[i].clear();
        seen[i] = 0;
        low[i] = 0;
        num[i] = 0;
        g[i] = 0;
    }
    while(!st.empty()) st.pop();
    cnt = 0;
    cg = 0;
}
void solve()
{
    ll n;
    cin >> n;
    ll i,j;
    for(i = 1;i <= 2 * n;i++)
    {
        ll tmp;
        cin >> tmp;
        c[tmp].push_back(i);
    }
    for(i = 1;i <= n;i++)
    {
        ll x = c[i][0];
        ll y = c[i][1];
        ll ux = 2 * x - 1;
        ll uy = 2 * y - 1;
        x = x * 2;
        y = y * 2;
        a[x].push_back(uy);
        a[uy].push_back(x);
        a[ux].push_back(y);
        a[y].push_back(ux);
    }
    for(i = 1;i <= 2 * n - 1;i++)
    {
        ll u,v;
        cin >> u >> v;
        p[u].push_back(v);
        p[v].push_back(u);
    }
    build(1,0);
    for(i = 1;i <= 4 * n;i++)
    {
        if(g[i] == 0) scc(i);
    }
    for(i = 1;i <= 2 * n;i++)
    {
        ll x = i * 2;
        ll ux = x - 1;
        if(g[x] == g[ux])
        {
            cout << -1 << "\n";
            clea(n);
            return;
        }
    }
    for(i = 1;i <= cg;i++)
    {
        for(auto x : haha[i])
        {
            for(auto y : a[x])
            {
                if(g[y] != i) a2[i].insert(g[y]);
            }
        }
    }
    for(i = 1;i <= cg;i++)
    {
        if(seen[i] == 0)
        {
            mark(i);
        }
    }
    for(i = 1;i <= 2 * n;i++)
    {
        if(seen[g[i * 2]] == 1) cout << i << " ";
    }
    cout << "\n";
    clea(n);
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    cin >> t;
    while(t--)
    {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 32488kb

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: 6ms
memory: 32532kb

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: 4ms
memory: 30136kb

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 5 6 
-1
1 

result:

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