QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#234109#3846. Link-Cut Treetselmegkh#WA 1ms3400kbC++172.0kb2023-11-01 14:07:432023-11-01 14:07:43

Judging History

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

  • [2023-11-01 14:07:43]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3400kb
  • [2023-11-01 14:07:43]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        vector<int> u(m), v(m);
        for (int i = 0; i < m; i++) {
            cin >> u[i] >> v[i];
            u[i]--, v[i]--;
        }
        vector<int> par(n), sz(n, 1);
        iota(par.begin(), par.end(), 0);
        function<int(int)> find = [&](int i) {
            return i == par[i] ? i : par[i] = find(par[i]);
        };
        auto unite = [&](int x, int y) {
            x = find(x);
            y = find(y);
            if (x == y) return;
            if (sz[x] > sz[y]) swap(x, y);
            sz[y] += sz[x];
            par[x] = y;
        };
        vector<vector<pair<int, int>>> adj(n);
        bool f = 0;
        for (int i = 0; i < m; i++) {
            if (find(u[i]) != find(v[i])) {
                adj[u[i]].emplace_back(v[i], i);
                adj[v[i]].emplace_back(u[i], i);
                unite(u[i], v[i]);
                continue;
            }
            vector<int> a(n, -1);
            a[u[i]] = u[i];
            queue<int> q;
            q.push(u[i]);
            while (!q.empty()) {
                int x = q.front();
                q.pop();
                for (auto [y, z] : adj[x]) {
                    if (a[y] == -1) {
                        a[y] = z;
                        q.push(y);
                    }
                }
            }
            vector<int> ans{i};
            int x = v[i];
            while (x != u[i]) {
                int i = a[x];
                x ^= u[i] ^ v[i];
                ans.push_back(i);
            }
            sort(ans.begin(), ans.end());
            for (int i : ans) {
                cout << i + 1 << " ";
            }
            cout << "\n";
            f = 1;
            break;
        }
        if (!f) cout << -1 << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3400kb

input:

2
6 8
1 2
2 3
5 6
3 4
2 5
5 4
5 1
4 2
4 2
1 2
4 3

output:

2 4 5 6 
-1

result:

wrong answer 1st lines differ - expected: '2 4 5 6', found: '2 4 5 6 '