QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189026 | #6634. Central Subset | real_sigma_team# | RE | 0ms | 0kb | C++20 | 2.5kb | 2023-09-26 19:22:08 | 2023-09-26 19:22:33 |
answer
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using ll = long long;
const int N = 2e5 + 5;
vector<int> gr[N];
int d[N], choose[N], di[N];
int need;
void dfs(int v) {
int ch = 0;
for (int u: gr[v]) {
if (d[u] == d[v] + 1) {
ch++;
}
}
if (ch == 0 && di[v] > need) {
choose[v] = 1;
di[v] = 0;
}
if (di[v] > need * 2) {
choose[v] = 1;
di[v] = 0;
}
for (int u: gr[v]) {
di[u] = min(di[u], di[v] + 1);
}
for (int u: gr[v]) {
if (d[u] == d[v] + 1) {
dfs(u);
}
}
}
void solve() {
int n, m;
cin >> n >> m;
fill(d, d + n, N);
for (int i = 0; i < n; i++) {
gr[i].clear();
choose[i] = 0;
di[i] = N;
}
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--, v--;
gr[u].push_back(v);
gr[v].push_back(u);
}
queue<int> q;
d[0] = 0;
q.push(0);
vector<int> used(n, 0);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u: gr[v]) {
if (used[u] || d[u] <= d[v] + 1)
continue;
d[u] = d[v] + 1;
q.push(u);
}
}
need = ceil(sqrt(n));
choose[0] = 1;
di[0] = 0;
// dfs(0);
q.push(0);
while (!q.empty()) {
int v = q.front();
q.pop();
int ch = 0;
for (int u: gr[v]) {
if (d[u] == d[v] + 1) {
ch++;
}
}
if (ch == 0 && di[v] > need) {
choose[v] = 1;
di[v] = 0;
}
if (di[v] > need * 2) {
choose[v] = 1;
di[v] = 0;
}
for (int u: gr[v]) {
di[u] = min(di[u], di[v] + 1);
}
for (int u: gr[v]) {
if (d[u] == d[v] + 1) {
q.push(u);
}
}
}
vector<int> ans;
for (int i = 0; i < n; i++) {
if (choose[i])
ans.push_back(i + 1);
}
cout << sz(ans) << '\n';
assert(sz(ans) > need);
for (int I: ans) {
cout << I << ' ';
}
cout << '\n';
}
int main() {
#ifndef LOCAL
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#else
freopen("input.txt", "r", stdin);
#endif
int t;
cin >> t;
while (t--)
solve();
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
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