QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#627590#7798. Colorful VillageIllusionaryDominance#WA 3ms16140kbC++202.7kb2024-10-10 16:24:282024-10-10 16:24:28

Judging History

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

  • [2024-10-10 16:24:28]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:16140kb
  • [2024-10-10 16:24:28]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef vector <int> vi;

const int MAX_N = 400000 + 5;
const int MAX_M = MAX_N * 12;

int N, c[MAX_N], node[MAX_N][2];
vi edge[MAX_N];
struct Edge{
    int y, prev;
}e[MAX_M];
int elast[MAX_N], ecnt;
int low[MAX_N], dfsn[MAX_N], Tm;
int sta[MAX_N], tp, scc[MAX_N], scc_cnt;
bool vis[MAX_N];

void Build(int x, int y) {
    ecnt ++;
    e[ecnt].y = y;
    e[ecnt].prev = elast[x];
    elast[x] = ecnt;
}

void tarjan(int u) {
    dfsn[u] = low[u] = ++ Tm;
    sta[++ tp] = u; vis[u] = 1;
    for (int i = elast[u]; i; i = e[i].prev) {
        int v = e[i].y;
        if (!dfsn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }else if (vis[v]) low[u] = min(low[u], dfsn[v]);
    }
    if (dfsn[u] == low[u]) {
        scc_cnt ++;
        while (sta[tp] != u) {
            scc[sta[tp]] = scc_cnt;
            vis[sta[tp --]] = 0;
        }
        scc[sta[tp]] = scc_cnt;
        vis[sta[tp --]] = 0;
    }
}

void dfs(int u, int fa) {
    for (auto v : edge[u]) {
        if (v != fa) {
            // !v || u
            Build(v, u);
            Build(u + (N << 1), v + (N << 1));
            dfs(v, u);
        }
    }
}

vi solve(int rt) {
    ecnt = 0; Tm = 0; scc_cnt = 0;
    memset(elast, 0, sizeof(int) * (N << 2 | 1));
    
    dfs(rt, 0);
    for (int i = 1; i <= N; i ++) {
        int u = node[i][0], v = node[i][1];
        // u ^ v -> (u || v) && (!u || !v)
        Build(u + (N << 1), v);
        Build(v + (N << 1), u);
        Build(u, v + (N << 1));
        Build(v, u + (N << 1));
    }
    for (int i = 1; i <= (N << 2); i ++) {
        if (!dfsn[i]) tarjan(i);
    }
    vi res(0);
    for (int i = 1; i <= (N << 1); i ++) {
        int u = i, v = i + (N << 1);
        if (scc[u] == scc[v]) {
            res.clear();
            return res;
        }else if (scc[u] < scc[v]) res.push_back(i);
    }
    return res;
}

void solve() {
    cin >> N;
    for (int i = 1; i <= N; i ++) node[i][0] = 0;
    for (int i = 1; i <= (N << 1); i ++) {
        cin >> c[i]; edge[i].clear();
        node[c[i]][node[c[i]][0] != 0] = i;
    }
    for (int i = 1; i < (N << 1); i ++) {
        int x, y;
        cin >> x >> y;
        edge[x].push_back(y);
        edge[y].push_back(x);
    }
    vi res = solve(node[1][0]);
    if (res.empty()) res = solve(node[1][1]);
    if (res.empty()) cout << "-1\n";
    else {
        for (auto x : res) cout << x << ' ';
        cout << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    int T; cin >> T;
    while (T --) solve();
    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
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: 0ms
memory: 15916kb

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

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

result:

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