QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#189057#6634. Central Subsetreal_sigma_team#WA 12ms8220kbC++201.9kb2023-09-26 20:11:212023-09-26 20:11:23

Judging History

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

  • [2023-09-26 20:11:23]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:8220kb
  • [2023-09-26 20:11:21]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()

using ll = long long;

const int N = 2e5 + 5;

vector<int> gr[N];
int d[N], choose[N];
int need;
mt19937 gen;
void dfs(int v, int di) {
    int ch = 0;
    for (int u: gr[v]) {
        if (d[u] == d[v] + 1) {
            ch++;
        }
    }

    if (ch == 0 && di > need) {
        choose[v] = 1;
        return;
    }

    if (di > need * 2) {
        choose[v] = 1;
        di = 0;
    }

    for (int u: gr[v]) {
        if (d[u] == d[v] + 1) {
            dfs(u, di + 1);
        }
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    fill(d, d + n, N);
    for (int i = 0; i < n; i++) {
        gr[i].clear();
        choose[i] = 0;
    }
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        gr[u].push_back(v);
        gr[v].push_back(u);
    }
    int st = gen() % n;
    queue<int> q;
    d[st] = st;
    q.push(st);
    vector<int> used(n, 0);
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        for (int u: gr[v]) {
            if (used[u] || d[u] <= d[v] + 1)
                continue;
            d[u] = d[v] + 1;
            q.push(u);
        }
    }

    need = ceil(sqrt(n));
    choose[st] = 1;
    dfs(st, 0);
    vector<int> ans;
    for (int i = 0; i < n; i++) {
        if (choose[i])
            ans.push_back(i + 1);
    }
    if (sz(ans) > need) {
        cout << "-1\n";
    } else {
        cout << sz(ans) << '\n';
        for (int I: ans) {
            cout << I << ' ';
        }
        cout << '\n';
    }


}

int main() {
#ifndef LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#else
    freopen("input.txt", "r", stdin);
#endif
    gen.seed(66);
    int t;
    cin >> t;
    while (t--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
4 3
1 2
2 3
3 4
6 7
1 2
2 3
3 1
1 4
4 5
5 6
6 4

output:

2
1 4 
1
1 

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 12ms
memory: 8152kb

input:

10000
15 14
13 12
5 4
9 8
11 12
15 14
10 9
14 13
2 3
2 1
6 5
10 11
3 4
7 6
8 7
6 5
2 1
2 4
4 6
2 3
3 5
10 9
8 3
9 4
5 6
5 10
3 2
5 4
2 7
1 2
4 3
2 1
2 1
2 1
2 1
9 8
9 8
5 4
1 2
6 5
3 4
3 2
7 8
7 6
2 1
1 2
14 13
3 10
5 6
2 9
11 4
2 3
2 1
8 7
13 6
5 4
5 12
6 7
4 3
7 14
16 15
2 3
2 1
6 10
6 9
6 4
9 11
...

output:

2
2 11 
1
1 
3
1 6 7 
1
2 
1
1 
2
4 9 
1
2 
3
8 11 14 
2
6 16 
1
4 
3
3 14 20 
2
1 6 
1
5 
4
1 6 9 16 
1
4 
2
5 14 
3
5 6 7 
1
2 
2
5 8 
1
5 
2
1 4 
-1
1
5 
5
1 7 10 12 17 
1
3 
2
3 12 
1
3 
4
3 9 15 16 
1
1 
1
1 
2
5 16 
2
7 11 
3
8 11 14 
3
5 9 16 
1
3 
2
1 6 
1
2 
4
1 7 8 9 
4
1 14 18 21 
1
1 
2
...

result:

wrong answer Integer -1 violates the range [1, 3] (test case 22)