QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#136392 | #6634. Central Subset | NightW0lf# | WA | 1417ms | 4508kb | C++23 | 1.8kb | 2023-08-08 15:54:40 | 2023-08-08 15:54:43 |
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.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: 3508kb
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 3
result:
ok correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 1417ms
memory: 4508kb
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 3 8 4 12 3 8 4 12 7 8 4 2 6 12 10 14 7 8 4 2 6 12 10 14 3 8 4 12 7 8 4 2 6 12 10 14 3 8 4 12 3 8 4 12 3 8 4 12 3 10 5 15 7 10 5 3 7 15 13 18 4 10 5 15 18 3 10 5 15 7 10 5 3 7 15 13 18 4 10 5 15 18 7 10 5 3 7 15 13 18 7 10 5 3 7 15 13 18 7 10 5 3 7 15 13 18 3 10 5 15 7 10...
result:
wrong answer Integer 8 violates the range [1, 6] (test case 2)