QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#134951#6634. Central SubsetPanC_ake#ML 4ms17256kbC++201.8kb2023-08-05 10:17:482023-08-05 10:17:52

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 10:17:52]
  • 评测
  • 测评结果:ML
  • 用时:4ms
  • 内存:17256kb
  • [2023-08-05 10:17:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long

const int maxn = 200010;
const int inf = 0x3f3f3f3f3f3f3f3f;

int n, m, N, rt, cnt, d, dv, ont[maxn], vis[maxn], dep[maxn], fa[maxn];
vector<int> e[maxn], g[maxn];
vector<int> ans;

void dfs0(int u) {
    ont[u] = 1;
    for(int v: e[u]) {
        if(ont[v]) continue;
        g[u].push_back(v);
        g[v].push_back(u);
        dfs0(v);
    }
}

void dfs1(int u, int f) {
    dep[u] = dep[f] + 1;
    if(!vis[u]) {
        if(dep[u] > d) {
            dv = u;
            d = dep[u];
        }
    }
    for(int v: g[u]) {
        if(v == f) continue;
        fa[v] = u;
        dfs1(v, u);
    }
}

void dfs2(int u, int f) {
    dep[u] = dep[f] + 1;
    if(dep[u] - 1 <= N && !vis[u]) {
        vis[u] = 1;
        cnt++;
    }
    for(int v: g[u]) {
        if(v == f) continue;
        dfs2(v, u);
    }
}

int climb(int u) {
    for(int i = 1; i <= N; i++) u = fa[u];
    return u;
}

void solve() {
    ans.clear();
    cnt = 0;
    cin >> n >> m;
    N = ceil(sqrt(n));
    for(int i = 1; i <= n; i++) {
        vis[i] = dep[i] = fa[i] = ont[i] = 0;
        e[i].clear();
        g[i].clear();
    }
    for(int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        e[u].push_back(v);
        e[v].push_back(u);
    }
    dfs0(1);
    int rt = 1;
    while(true) {
        if(cnt == n) break;
        fa[rt] = 0;
        d = 0;
        dfs1(rt, 0);
        rt = climb(dv);
        dfs2(rt, 0);
        ans.push_back(rt);
    }
    cout << ans.size() << endl;
    for(int v: ans) cout << v << " ";
    cout << endl; 
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--) solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 17256kb

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 

result:

ok correct (2 test cases)

Test #2:

score: -100
Memory Limit Exceeded

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:


result: