QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#579826#6634. Central SubsetllgRE 1ms5880kbC++142.4kb2024-09-21 18:16:382024-09-21 18:16:39

Judging History

你现在查看的是最新测评结果

  • [2024-09-21 18:16:39]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:5880kb
  • [2024-09-21 18:16:38]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define deb(x) cout << #x << "=" << x << endl
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define mod 1000000007
const int N = 1e5 + 7;
int mx;
int n;
vector<int> answer;
vector<int> adj[N];
void construct_bfs_tree(vector<vector<int>> &tree)
{
    queue<int> q;
    q.push(1);
    vector<bool> vis(n, false);
    vis[1] = true;
    while (!q.empty())
    {
        int node = q.front();
        q.pop();
        for (auto it : adj[node])
        {
            if (!vis[it])
            {
                tree[node].push_back(it);
                tree[it].push_back(node);
                vis[it] = true;
                q.push(it);
            }
        }
    }
}
void dfs(int node, int par, int distance, vector<vector<int>> &tree)
{
    if (distance > mx)
    {
        answer.push_back(node);
        distance = 0;
    }

    for (auto it : tree[node])
    {
        if (it != par)
        {
            dfs(it, node, distance + 1, tree);
        }
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        answer.clear();
        cin >> n;
        int m;
        cin >> m;
        mx = (int)ceil(sqrt(n));
        for (int i = 1; i <= n; i++)
        {
            adj[i].clear();
        }
        for (int i = 1; i <= m; i++)
        {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
        }
        vector<vector<int>> tree(n + 1);
        construct_bfs_tree(tree);
        // for (int u = 1; u <= n; u++)
        // {

        //     for (int v : tree[u])
        //     {
        //         if (v < u)
        //             continue;
        //         cout << u << " " << v << endl;
        //     }
        // }
        dfs(1, 0, 0, tree);
        answer.push_back(1);
        assert((int)answer.size() <= mx);
        cout << (int)answer.size() << endl;
        for (int a : answer)
        {
            cout << a << " ";
        }
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5880kb

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
4 1 1
1 

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
...

output:

3
6 11 1 1
1 3
6 10 1 1
1 1
1 3
5 9 1 1
1 3

result: