QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#613553#7798. Colorful VillagexyyyWA 2ms13852kbC++171.7kb2024-10-05 14:12:252024-10-05 14:14:51

Judging History

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

  • [2024-10-05 14:14:51]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:13852kb
  • [2024-10-05 14:12:25]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define rep(i, f, l) for(int i(f); i <= l; ++i)
#define per(i, f, l) for(int i(f); i >= l; --i)
const int N = 2e5 + 5;
const int M = 1e3 + 5;
int n;
int c[N];
int Next[N << 1], head[N], ver[N << 1], tot;

void add(int x, int y) {
    ver[++tot] = y;
    Next[tot] = head[x];
    head[x] = tot;
}
int dfn[N], low[N], cnt, vis[N], stk[N], top, co[N], sum;
bool fl = 0;
int total;
bool ex[N];
int lin[N];

void dfs(int x, int cnt) {
    if (cnt == n) {
        fl = 1;
        for (int i = 1; i <= n; i++)
            cout << lin[i] << " ";
        cout << endl;
        return;
    }
    if (fl == 1)
        return;
    for (int i = head[x]; i; i = Next[i]) {
        int y = ver[i];
        if (ex[c[y]] == 0) {
            ex[c[y]] = 1;
            lin[cnt + 1] = y;
            dfs(y, cnt + 1);
            ex[c[y]] = 0;
        }
        if (fl == 1)
            return;
    }
}

void work() {
    cin >> n;
    for (int i = 1; i <= 2 * n; i++) {
        cin >> c[i];
    }
    memset(head, 0, sizeof(head));
    tot = 0;
    for (int i = 1; i < 2 * n; i++) {
        int x, y;
        cin >> x >> y;
        add(x, y);
        add(y, x);
    }
    fl = 0;
    for (int i = 1; i <= 2 * n; i++) {
        if (c[i] == 1) {
                memset(ex,0,sizeof(ex));
            ex[1] = 1;
            lin[1] = 1;
            dfs(1, 1);
            if(fl==1)break;
        }
    }
    if (fl == 0)
        cout << "-1" << endl;
    return;
}

signed main() {
//  ios::sync_with_stdio(0);
//  cin.tie(0);
//  cout.tie(0);
    int t ;
    cin >> t;
    while (t--) {
        work();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

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

result:

wrong answer color used twice: 3 (test case 3)