QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134908 | #6634. Central Subset | kiwiHM# | WA | 13ms | 9716kb | C++14 | 1.8kb | 2023-08-05 09:56:00 | 2023-08-05 09:56:01 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200500;
struct Enode{
int u, v;
} E[1000500];
vector <int> son[Maxn], ans;
int dep[Maxn], maxd[Maxn], p[Maxn];
int n, m, LMT;
void cmax(int &x, int y){ if (x < y) x = y; }
void dfs(int u, int fa){
for (auto v : son[u]) if (v != fa){
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
void solve(int u, int fa){
maxd[u] = dep[u];
for (auto v : son[u]) if (v != fa){
solve(v, u);
if (maxd[v] - dep[u] > LMT){
ans.push_back(v);
} else {
cmax(maxd[u], maxd[v]);
}
}
}
void init(int n){
for (int i = 1; i <= n; i++)
son[i].clear();
dep[1] = 0;
for (int i = 1; i <= n; i++)
p[i] = i;
LMT = sqrt(n);
if (LMT * LMT < n)
LMT++;
}
int getfa(int u){
if (u == p[u])
return u;
return p[u] = getfa(p[u]);
}
int main(){
int T; cin >> T;
while (T--){
cin >> n >> m, init(n);
for (int i = 1; i <= m; i++)
cin >> E[i].u >> E[i].v;
for (int i = 1; i <= m; i++){
int u = E[i].u, v = E[i].v;
int fu = getfa(u), fv = getfa(v);
if (fu == fv)
continue;
p[fu] = fv;
son[u].push_back(v);
son[v].push_back(u);
}
dfs(1, 0), solve(1, 0);
if (ans.size() > LMT){
puts("-1");
} else {
cout << ans.size() << '\n';
for (int i = 0, si = ans.size(); i < si; i++)
cout << ans[i] << (i == si - 1 ? '\n' : ' ');
}
}
return 0;
}
/*
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
*/
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 9584kb
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 2
result:
ok correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 13ms
memory: 9716kb
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:
2 11 6 2 11 6 3 11 6 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -...
result:
wrong answer Condition failed: "getMaxBfsDist(n, subset) <= csqrtn" (test case 1)