QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#581032#6634. Central SubsetllgRE 0ms0kbC++143.0kb2024-09-22 04:52:122024-09-22 04:52:14

Judging History

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

  • [2024-09-22 04:52:14]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-09-22 04:52:12]
  • 提交

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];
int depth[N];
set<pair<int, int>> s;
int par[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 parent, int distance, vector<vector<int>> &tree)
{

    for (auto it : tree[node])
    {
        if (it != parent)
        {
            par[it] = node;
            s.insert({-(distance + 1), it});
            depth[it] = depth[node] + 1;
            dfs(it, node, distance + 1, tree);
        }
    }
}
void dfs_remove(int node, int distance, int parent, vector<vector<int>> &tree)
{

    for (auto it : tree[node])
    {
        if (it != parent)
        {
            s.erase({-(distance + 1), it});
            dfs_remove(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);
        dfs(1, 0, 0, tree);
        while ((int)s.size())
        {
            auto it = s.begin();
            int distance = -it->first;
            int node = it->second;
            answer.push_back(node);
            s.erase(it);
            int cur = node;
            while ((cur != 1) && (depth[node] - depth[cur] <= mx))
            {
                cur = par[cur];
            }
            cout << "dude" << endl;

            dfs_remove(cur, distance, par[cur], tree);
            cout << "dude" << endl;
        }

        answer.push_back(1);
        assert((int)answer.size() <= mx);
        cout << (int)answer.size() << endl;
        for (int a : answer)
        {
            cout << a << " ";
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

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:

dude
dude
dude
dude

result: