QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#703841#6634. Central SubsetlvliangWA 0ms8092kbC++141.4kb2024-11-02 18:40:082024-11-02 18:40:10

Judging History

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

  • [2024-11-02 18:40:10]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:8092kb
  • [2024-11-02 18:40:08]
  • 提交

answer

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define x first
#define y second
using namespace std;
const int N = 2e5 + 10;
int h[N], e[N << 1], ne[N << 1], idx;
int ans[N], tp;
int p[N];

void add(int a, int b) {
    e[++idx] = b, ne[idx] = h[a], h[a] = idx;
}

int find(int x) {
    if (x ^ p[x]) p[x] = find(p[x]);
    return p[x];
}

int sqrtn;
int dfs(int u, int fa) {
    if (tp == sqrtn) return 0;
    int dist = 0;
    for (int i = h[u]; i; i = ne[i]) {
        int j = e[i];
        if (j == fa) continue;
        dist = max(dfs(j, u) + 1, dist);
    }
    if (dist == sqrtn) {
        ans[tp++] = u;
        dist = -1;
    }
    return dist;
}

void solve() {
    tp = 0;
    idx = 0;
    memset(h, 0, sizeof h);
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) p[i] = i;
    for (int i = 0; i < m; i++) {
        int u, v;
        scanf("%d%d", &u, &v);
        int fu = find(u), fv = find(v);
        if (fu ^ fv) {
            p[fu] = fv;
            add(u, v), add(v, u);
        }
    }
    sqrtn = ceil(sqrt(n));
    dfs(1, -1);
    printf("%d\n", tp);
    int flag = false;
    for (int i = 0; i < tp; i++) {
        printf("%d ", ans[i]);
        if (ans[i] == 1) flag = true;
    }
    if (!flag) printf("1 ");
    puts("");
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 8092kb

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:

1
2 1 
1
1 

result:

wrong output format Extra information in the output file (test case 2)