97582024-03-06 13:17:54FulopMateEmezen Rt.cpp17Wrong answer 0/100263ms49432 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++;
    }
    if (ok > m/2) {
        cout << "-1\n";
        return;
    }
    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
1Accepted4ms6608 KiB
subtask20/16
2Wrong answer4ms6904 KiB
3Accepted4ms7216 KiB
4Accepted4ms7508 KiB
5Accepted4ms7732 KiB
6Accepted4ms7680 KiB
7Wrong answer4ms7512 KiB
8Wrong answer4ms7688 KiB
9Accepted4ms7768 KiB
10Wrong answer4ms7608 KiB
11Wrong answer4ms7868 KiB
12Accepted4ms7960 KiB
13Accepted4ms8028 KiB
14Accepted4ms7968 KiB
15Accepted4ms8232 KiB
16Accepted4ms8284 KiB
subtask30/18
17Accepted79ms26684 KiB
18Accepted186ms45512 KiB
19Accepted186ms45516 KiB
20Wrong answer8ms10292 KiB
21Accepted100ms31084 KiB
22Accepted150ms40164 KiB
23Accepted209ms45744 KiB
24Accepted246ms47376 KiB
25Accepted225ms48992 KiB
26Accepted263ms49056 KiB
subtask40/66
27Wrong answer8ms10724 KiB
28Wrong answer21ms14572 KiB
29Accepted54ms21984 KiB
30Accepted175ms40636 KiB
31Accepted232ms49212 KiB
32Accepted231ms49432 KiB
33Accepted226ms48792 KiB
34Accepted225ms47856 KiB
35Wrong answer190ms46124 KiB
36Wrong answer189ms46124 KiB
37Wrong answer6ms9476 KiB
38Accepted10ms11052 KiB
39Wrong answer24ms14332 KiB
40Accepted4ms9568 KiB
41Wrong answer6ms9924 KiB
42Accepted57ms9216 KiB
43Wrong answer24ms9224 KiB
44Wrong answer7ms9248 KiB
45Wrong answer4ms9256 KiB