QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#177442 | #6634. Central Subset | tovarisch | WA | 11ms | 14212kb | C++17 | 2.3kb | 2023-09-12 23:16:50 | 2023-09-12 23:16:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int maxn = 2e5 + 10;
int n, m, limit;
int vis[maxn], height[maxn], dis[maxn];
vector <int> graph[maxn] ,tree[maxn], ans;
void dfs(int u) {
vis[u]=1;
for (int &v : graph[u]) {
if (vis[v]) continue;
tree[u].push_back(v);
tree[v].push_back(u);
dfs(v);
}
}
pii dfs2(int u, int p) {
pii ans = {u, 0};
for (int& v : tree[u]) {
if (v == p) continue;
pii cur = dfs2(v, u);
if (cur.second+1 > ans.second) ans = {cur.first, cur.second+1};
}
return ans;
}
void dfs3(int u, int p) {
height[u]=0;
for(int& v : tree[u]) {
if (v == p) continue;
dfs3(v, u);
height[u] = max(height[u], height[v]+1);
}
}
void clean() {
for (int i=0; i<n; i++) {
graph[i].clear();
tree[i].clear();
vis[i]=0, height[i]=0, dis[i]=maxn;
}
ans.clear();
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
int tt; cin >> tt;
while (tt--) {
cin >> n >> m;
for (int i=0; i < m; i++) {
int u, v; cin >> u >> v;
u--, v--;
graph[u].push_back(v);
graph[v].push_back(u);
}
dfs(0);
pii node = dfs2(0, 0);
int a=node.first;
limit = ceil(sqrt(n));
dfs3(a, a);
for (int i=0; i<n; i++) {
dis[i]=maxn;
if (height[i]>0 && height[i]%limit == 0) ans.push_back(i);
}
queue<int> q;
assert(ans.size()<= limit);
for(int& i : ans) dis[i]=0, q.push(i);
while (!q.empty()) {
int u=q.front(); q.pop();
for(int& v : graph[u]) {
if (dis[v] > dis[u]+1) {
dis[v]=dis[u]+1;
q.push(v);
}
}
}
bool ok=1;
for (int i=0; i<n; i++) ok &= dis[i]<=limit;
if (ok) {
cout << ans.size() << endl;
for(int& i : ans) cout << i+1 << ' ';
cout << endl;
} else cout << -1 << endl;;
clean();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 13940kb
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 3 1 4
result:
ok correct (2 test cases)
Test #2:
score: -100
Wrong Answer
time: 11ms
memory: 14212kb
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 5 9 13 1 4 1 5 -1 -1 2 4 7 -1 1 5 2 6 15 -1 3 6 11 16 1 4 1 5 1 10 -1 3 5 9 13 1 4 -1 1 4 -1 1 3 1 4 1 5 1 8 -1 3 5 9 13 1 3 2 5 16 1 3 -1 4 6 11 16 21 1 8 1 5 2 6 15 -1 1 4 1 4 1 5 2 6 15 -1 2 5 9 1 4 1 5 1 3 -1 2 5 9 1 6 1 5 1 7 -1 3 6 11 16 1 7 1 4 1 6 -...
result:
wrong answer Integer -1 violates the range [1, 2] (test case 4)