QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#213464 | #6634. Central Subset | ucup-team228# | WA | 17ms | 11716kb | C++17 | 4.7kb | 2023-10-14 14:21:27 | 2023-10-14 14:21:27 |
Judging History
answer
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <cmath>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <random>
#include <string>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <bitset>
#include <array>
#include <stack>
#include <sstream>
using namespace std;
typedef long long ll;
string to_string(string a) { return '"' + a + '"'; }
string to_string(char a) { return "'" + string(1, a) + "'"; }
string to_string(const char* a) { return to_string((string) a); }
string to_string(bool a) { return a ? "true" : "false"; }
template <class T1, class T2>
string to_string(pair<T1, T2> a) {
return "(" + to_string(a.first) + ", " + to_string(a.second) + ")";
}
template <class T>
string to_string(T a) {
bool first = true; string res = "{";
for (const auto& i : a) {
if (!first) res += ", ";
first = false;
res += to_string(i);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <class T1, class... T2>
void debug_out(T1 a, T2... b) {
cerr << " " << to_string(a);
debug_out(b...);
}
#ifdef LOCAL
#define out(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define out(...) 42
#endif
clock_t start_time; void start_timer() { start_time = clock(); }
double get_time() { return (double) (clock() - start_time) / CLOCKS_PER_SEC; }
void Solve();
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
freopen("usr/share/man/man1/input.txt", "r", stdin);
#endif
start_timer();
Solve();
#ifdef LOCAL
cerr << fixed << setprecision(3);
cerr << endl << "Time spent: " << get_time() << endl;
#endif
return 0;
}
// do something, stay focused
// look for stupid bugs
// guess, slow, stress
// don't overgeneralize
// don't rush
// don't waste time on standings
// SOLVE THE PROBLEM OR DIE TRYING
// THE SOLUTION IS ALWAYS SIMPLE
// THE CODE IS ALWAYS SHORT
const int N = 2e5 + 5;
const int inf = 1e9;
int par[N];
int dep[N];
vector<int> g[N];
bool used[N];
int sz[N];
int msz[N];
vector<int> vis;
int mx;
vector<int> ans;
queue<int> q;
int d[N];
void init(int n) {
for (int i = 1; i <= n; i++) {
par[i] = i;
dep[i] = 0;
g[i].clear();
used[i] = 0;
sz[i] = 0;
msz[i] = 0;
d[i] = 0;
}
vis.clear();
ans.clear();
while (!q.empty()) {
q.pop();
}
}
int find(int v) {
if (v == par[v]) {
return v;
}
return par[v] = find(par[v]);
}
bool unite(int u, int v) {
u = find(u);
v = find(v);
if (u == v) return 0;
if (dep[u] < dep[v]) swap(u, v);
par[v] = u;
if (dep[u] == dep[v]) dep[u]++;
return 1;
}
void calc_sz(int v, int p = 0) {
vis.push_back(v);
sz[v] = 1;
msz[v] = 0;
for (int u : g[v]) {
if (u == p || used[u]) continue;
calc_sz(u, v);
sz[v] += sz[u];
msz[v] = max(msz[v], sz[u]);
}
}
int dfs_d(int v, int p = 0) {
int res = d[v];
for (int u : g[v]) {
if (u == p || used[u]) continue;
d[u] = d[v] + 1;
res = max(res, dfs_d(u, v));
}
return res;
}
int get_cen(int v) {
vis.clear();
calc_sz(v);
int min_sz = inf;
int cen = -1;
for (int u : vis) {
int size = max(msz[u], int(vis.size()) - sz[u]);
if (min_sz > size) {
min_sz = size;
cen = u;
}
}
used[cen] = 1;
return cen;
}
void Solve() {
int T;
cin >> T;
while (T--) {
int n, m;
cin >> n >> m;
init(n);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
if (unite(u, v)) {
g[u].push_back(v);
g[v].push_back(u);
}
}
while ((mx + 1) * (mx + 1) <= n) mx++;
if (mx * mx < n) mx++;
q.push(get_cen(1));
while (!q.empty()) {
int v = q.front();
q.pop();
ans.push_back(v);
for (int u : g[v]) {
if (used[u]) continue;
d[u] = 1;
if (dfs_d(u, v) <= mx) continue;
int cen = get_cen(u);
q.push(cen);
}
}
assert(ans.size() <= mx);
sort(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (int i : ans) {
cout << i << " ";
}
cout << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 11716kb
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: 17ms
memory: 11704kb
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 4 8 12 1 2 1 3 1 1 1 1 1 5 1 1 1 4 2 6 12 1 2 3 5 10 15 1 3 1 3 1 8 1 2 3 4 8 12 1 2 1 1 1 4 1 2 1 2 1 3 1 4 2 3 14 1 2 3 4 8 12 1 2 1 5 1 2 1 2 3 6 11 16 1 3 1 4 1 6 1 2 1 3 1 3 1 4 3 5 11 15 1 2 2 6 9 1 2 1 4 1 2 1 2 1 5 1 3 1 4 1 3 1 2 3 5 9 14 ...
result:
wrong answer Condition failed: "getMaxBfsDist(n, subset) <= csqrtn" (test case 6)