QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#136394 | #6634. Central Subset | NightW0lf# | WA | 61ms | 3568kb | C++23 | 1.9kb | 2023-08-08 16:05:49 | 2023-08-08 16:05:51 |
Judging History
answer
#include <bits/stdc++.h>
#define IO { ios_base::sync_with_stdio(false); cin.tie(0); }
using namespace std;
int n, m;
vector<set<int>> adj;
vector<int> subtree_size;
int get_subtree_size(int node, int parent = -1) {
int &res = subtree_size[node];
res = 1;
for (int i : adj[node]) {
if (i == parent) { continue; }
res += get_subtree_size(i, node);
}
return res;
}
int get_centroid(int node, int sz, int parent = -1) {
for (int i : adj[node]) {
if (i == parent) { continue; }
if (subtree_size[i] * 2 > sz) { return get_centroid(i, sz, node); }
}
return node;
}
vector<vector<int>> adj2;
vector<bool> vis;
void dfs(int node) {
vis[node] = 1;
for (int v: adj2[node]) {
if (!vis[v]) {
adj[v].insert(node);
adj[node].insert(v);
dfs(v);
}
}
}
int lim;
vector<int> cs;
void rec(int v) {
int sz = get_subtree_size(v);
if (subtree_size[v] <= lim) return;
int x = get_centroid(v, sz);
//cout << "C = " << x + 1 << endl;
cs.push_back(x);
for (int y: adj[x]) {
adj[y].erase(x);
rec(y);
}
}
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);
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);
cs.clear();
rec(0);
cout << cs.size() << endl;
for (int x: cs) {
cout << x + 1 << " ";
}
cout << endl;
}
int main() {
IO;
int t;
cin >> t;
while(t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3568kb
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: 61ms
memory: 3516kb
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 11 15 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)