97572024-03-06 13:17:27FulopMateEmezen Rt.cpp17Runtime error 0/100261ms49100 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define all(a) (a).begin(), (a).end()
#define sortedpair(a, b) {min((a), (b)), max((a), (b))}

const ll MOD = 1e9+7;

const int MAXN = 100'001;

struct E {
    int u, v, w;

    E(int a, int b, int c) : u(a), v(b), w(c) {}
};

int par[MAXN], ans[MAXN];
vector<int> g[MAXN];

int get(int u) {
    if (par[u] == u) return u;
    return par[u]=get(par[u]);
}

bool unio(int u, int v) {
    u = get(u); v=get(v);
    if (u == v) return false;
    par[u]=v;
    return true;
}

void dfs(int u, int p = -1, int d = 0) {
    ans[u] = d&1;
    for (int v : g[u]) {
        if (v != p) dfs(v, u, d+1);
    }
}

void solve(){
    int n, m; cin>>n>>m;
    iota(par, par+n+1, 0);
    map<pair<int, int>, int> mp;
    vector<pair<int, int>> a;
    for (int i = 0; i < m; i++) {
        int u, v; cin >> u >> v;
        a.emplace_back(u, v);
        if (u > v) swap(u, v);
        mp[{u, v}]++;
    }
    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }

    vector<E> edges;
    for (auto [e, x] : mp) {
        edges.emplace_back(e.first, e.second, x);
    }
    sort(edges.begin(), edges.end(), [](auto x, auto y){
        return x.w > y.w;
    });
    for (E e : edges) {
        if (unio(e.u, e.v)) {
            g[e.u].emplace_back(e.v);
            g[e.v].emplace_back(e.u);
        }
    }
    dfs(1);
    int ok = 0;
    for (auto [u, v] : a) {
        if (ans[u] == ans[v]) ok++;
    }
    assert(ok <= m/2);
    int cnt = count(ans+1, ans+n+1, 1);
    cout << cnt << "\n";
    for (int i = 1; i <= n; i++) {
        if (ans[i]) cout << i << " ";
    }
    cout << "\n";
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int _t = 1;
    cin >> _t;
    while (_t--) {
        solve();
    }
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms6760 KiB
subtask20/16
2Runtime error4ms7136 KiB
3Accepted4ms7128 KiB
4Accepted4ms7272 KiB
5Accepted4ms7488 KiB
6Accepted4ms7704 KiB
7Runtime error4ms7836 KiB
8Runtime error4ms7760 KiB
9Accepted4ms7656 KiB
10Runtime error4ms7912 KiB
11Runtime error4ms8072 KiB
12Accepted4ms7936 KiB
13Accepted4ms8080 KiB
14Accepted4ms8044 KiB
15Accepted4ms7936 KiB
16Accepted4ms7936 KiB
subtask30/18
17Accepted79ms26600 KiB
18Accepted184ms45392 KiB
19Accepted184ms45384 KiB
20Runtime error8ms10308 KiB
21Accepted100ms30996 KiB
22Accepted149ms40072 KiB
23Accepted231ms45948 KiB
24Accepted215ms47252 KiB
25Accepted261ms48936 KiB
26Accepted229ms48980 KiB
subtask40/66
27Runtime error8ms10928 KiB
28Runtime error19ms14872 KiB
29Accepted54ms21976 KiB
30Accepted167ms40628 KiB
31Accepted231ms49100 KiB
32Accepted226ms49100 KiB
33Accepted223ms48456 KiB
34Accepted221ms47548 KiB
35Runtime error170ms45884 KiB
36Runtime error173ms45812 KiB
37Runtime error4ms9136 KiB
38Accepted9ms10516 KiB
39Runtime error23ms14100 KiB
40Accepted4ms9036 KiB
41Runtime error6ms9380 KiB
42Accepted57ms8688 KiB
43Runtime error4ms8692 KiB
44Runtime error4ms8692 KiB
45Runtime error4ms8696 KiB