QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#134934 | #6634. Central Subset | PanC_ake# | ML | 5ms | 18972kb | C++20 | 1.8kb | 2023-08-05 10:07:17 | 2023-08-05 10:07:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
const int maxn = 200010;
const int inf = 0x3f3f3f3f3f3f3f3f;
int n, m, N, rt, cnt, d, dv, ont[maxn], vis[maxn], dep[maxn], fa[maxn];
vector<int> e[maxn], g[maxn];
vector<int> ans;
void dfs0(int u) {
ont[u] = 1;
for(int v: e[u]) {
if(ont[v]) continue;
g[u].push_back(v);
g[v].push_back(u);
dfs0(v);
}
}
void dfs1(int u, int f) {
dep[u] = dep[f] + 1;
if(!vis[u]) {
if(dep[u] - 1 > d) {
dv = u;
d = dep[u] - 1;
}
}
for(int v: g[u]) {
if(v == f) continue;
fa[v] = u;
dfs1(v, u);
}
}
void dfs2(int u, int f) {
dep[u] = dep[f] + 1;
if(dep[u] - 1 <= N && !vis[u]) {
vis[u] = 1;
cnt++;
}
for(int v: g[u]) {
if(v == f) continue;
dfs2(v, u);
}
}
int climb(int u) {
for(int i = 1; i <= N; i++) u = fa[u];
return u;
}
void solve() {
ans.clear();
cnt = 0;
cin >> n >> m;
N = ceil(sqrt(n));
for(int i = 1; i <= n; i++) {
vis[i] = dep[i] = fa[i] = ont[i] = 0;
e[i].clear();
g[i].clear();
}
for(int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs0(1);
int rt = 1;
while(true) {
if(cnt == n) break;
fa[rt] = 0;
d = 0;
dfs1(rt, 0);
rt = climb(dv);
dfs2(rt, 0);
ans.push_back(rt);
}
cout << ans.size() << endl;
for(int v: ans) cout << v << " ";
cout << endl;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while(t--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 18972kb
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
Memory Limit Exceeded
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 ...