QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189144 | #6634. Central Subset | real_sigma_team# | RE | 1ms | 5748kb | C++20 | 3.0kb | 2023-09-26 21:47:54 | 2023-09-26 21:48:09 |
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) {
assert(false);
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: 1ms
memory: 5748kb
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
Runtime Error
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 ...