QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#136418 | #6634. Central Subset | NightW0lf# | WA | 43ms | 3496kb | C++23 | 2.6kb | 2023-08-08 18:29:06 | 2023-08-08 18:29:07 |
Judging History
answer
#include <bits/stdc++.h>
#define IO { ios_base::sync_with_stdio(false); cin.tie(0); }
using namespace std;
int lim;
int n, m;
vector<vector<int>> adj;
vector<int> subtree_size;
vector<bool> is_removed;
vector<int> ans;
int get_subtree_size(int node, int parent = -1) {
subtree_size[node] = 1;
for (int child : adj[node]) {
if (child == parent || is_removed[child]) { continue; }
subtree_size[node] += get_subtree_size(child, node);
}
return subtree_size[node];
}
int get_centroid(int node, int tree_size, int parent = -1) {
for (int child : adj[node]) {
if (child == parent || is_removed[child]) { continue; }
if (subtree_size[child] * 2 > tree_size) {
return get_centroid(child, tree_size, node);
}
}
return node;
}
void build_centroid_decomp(int node = 0) {
int sz = get_subtree_size(node);
if (sz <= lim) return;
int centroid = get_centroid(node, sz);
ans.push_back(centroid);
is_removed[centroid] = true;
for (int child : adj[centroid]) {
if (is_removed[child]) { continue; }
build_centroid_decomp(child);
}
}
vector<vector<int>> adj2;
vector<bool> vis;
void dfs(int node) {
vis[node] = 1;
for (int v: adj2[node]) {
if (!vis[v]) {
adj[v].push_back(node);
adj[node].push_back(v);
dfs(v);
}
}
}
void solve() {
cin >> n >> m;
for (int i=1; ;i++) {
if (i*i >= n) {
lim = i;
break;
}
}
adj.clear();
adj2.clear();
adj.resize(n+100);
adj2.resize(n+100);
subtree_size.assign(n+100, 0);
vis.assign(n+100, 0);
is_removed.assign(n+100, 0);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
adj2[a].push_back(b);
adj2[b].push_back(a);
}
dfs(0);
ans.clear();
build_centroid_decomp(0);
assert(ans.size() <= lim);
vector<int> gg(n + 1);
queue<int> q;
for(auto x: ans) {
gg[x] = 1;
q.push(x);
}
while(!q.empty()) {
int temp = q.front();
assert(gg[temp] - 1 <= lim);
q.pop();
for(auto x: adj[temp]) {
if(gg[x] == 0) {
gg[x] = gg[temp] + 1;
q.push(x);
}
}
}
cout << ans.size() << endl;
for (int x: ans) {
cout << x + 1 << " ";
}
cout << endl;
}
int main() {
IO;
int t;
cin >> t;
while(t--) {
solve();
}
}
/*
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
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3492kb
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
Wrong Answer
time: 43ms
memory: 3496kb
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:
3 8 4 12 1 2 2 3 5 0 0 3 5 3 7 0 3 4 2 6 3 6 3 12 1 2 3 10 5 15 2 3 4 2 3 5 3 8 3 13 1 2 3 8 4 12 1 2 0 2 4 2 1 2 1 2 1 3 3 4 2 6 2 3 14 1 2 3 8 4 12 1 2 3 5 3 7 1 2 1 2 3 11 6 16 2 3 6 3 4 2 6 3 6 2 12 1 2 1 3 1 3 3 4 2 6 3 5 15 11 1 2 3 6 3 9 1 2 2 4 2 1 ...
result:
wrong answer Integer 0 violates the range [1, 2] (test case 4)