QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189142 | #6634. Central Subset | real_sigma_team# | WA | 21ms | 5848kb | C++20 | 2.9kb | 2023-09-26 21:47:12 | 2023-09-26 21:47:13 |
Judging History
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 = 1e5 + 5;
const int lovv66 = 228;
vector<int> gr[N];
int used[N];
int d[N], need, p[N];
vector<int> ans;
mt19937 gen;
void bfs(int st) {
d[st] = 0;
p[st] = -1;
queue<int> q;
q.push(st);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u: gr[v]) {
if (used[u] == 1 || d[u] <= d[v] + 1)
continue;
p[u] = v;
d[u] = d[v] + 1;
q.push(u);
}
}
}
int mx(const vector<int> &ar) {
pair<int, int> best = {0, 0};
for (int i: ar) {
best = max(best, {d[i], i});
}
return best.second;
}
void sol(vector<int> &ar) {
if (ar.empty())
return;
for (int u: ar) {
d[u] = N;
}
int st = ar[gen() % sz(ar)];
bfs(st);
int st1 = mx(ar);
for (int u: ar) {
d[u] = N;
}
bfs(st1);
vector<int> way;
int mx1 = mx(ar);
while (mx1 != -1) {
way.push_back(mx1);
mx1 = p[mx1];
}
for (int u: ar) {
d[u] = N;
}
queue<int> q;
for (int i = 0; i < sz(ar);) {
ans.push_back(ar[i] + 1);
d[ar[i]] = 0;
q.push(ar[i]);
int next = need * 2 + 1;
if (next + i >= sz(ar)) {
if (need + 1 + i < sz(ar)) {
i = need + 1 + i;
} else {
break;
}
} else {
i += next;
}
}
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u: gr[v]) {
if (used[u] == 1 || d[u] <= d[v] + 1)
continue;
d[u] = d[v] + 1;
q.push(u);
}
}
vector<int> ar1;
while (!ar.empty()) {
int i = ar.back();
if (d[i] <= need) {
used[i] = 1;
} else {
ar1.push_back(i);
}
ar.pop_back();
}
sol(ar1);
}
void solve() {
int n, m;
cin >> n >> m;
for(int i =0; i < n; i++){
gr[i].clear();
used[i] = 0;
}
ans.clear();
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
v--, u--;
gr[u].push_back(v);
gr[v].push_back(u);
}
vector<int> ar(n);
iota(all(ar), 0);
need = ceil(sqrt(n));
sol(ar);
if (sz(ans) > need) {
cout << "-1\n";
} else {
cout << sz(ans) << '\n';
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
gen.seed(lovv66);
int t;
cin >> t;
while (t--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5848kb
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:
2 1 4 2 1 5
result:
ok correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 21ms
memory: 5844kb
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 1 10 15 2 1 5 2 1 10 1 1 1 1 2 1 8 1 1 4 1 10 14 13 3 1 10 15 3 1 10 15 3 1 12 18 2 1 8 2 1 10 2 1 12 2 1 5 3 1 10 15 2 1 5 1 1 2 1 8 3 1 12 18 2 1 4 2 1 8 4 1 10 14 13 3 1 12 18 2 1 5 3 1 10 15 2 1 5 3 1 10 15 1 1 2 1 8 3 1 12 18 4 1 10 13 9 4 1 10 14 13 -1 2 1 5 ...
result:
wrong answer Integer -1 violates the range [1, 4] (test case 34)