QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#697104#3846. Link-Cut Treeyzj123#WA 0ms8084kbC++202.5kb2024-11-01 10:33:542024-11-01 10:33:54

Judging History

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

  • [2024-11-01 10:33:54]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:8084kb
  • [2024-11-01 10:33:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int n, m;
struct Edge {
    int x, y;
}e[MAXN];
int prt[MAXN];
int get_prt(int x) {
    if (x == prt[x]) return x;
    return prt[x] = get_prt(prt[x]);
}
vector<pair<int, int> > g[MAXN];
int du[MAXN], vis1[MAXN], vis2[MAXN];
int ans[MAXN];
int p;
void dfs(int u)
{
    // cout << "u:" << u << "\n";
    vis1[u] = 0;
    for (auto x : g[u])
    {
        int v = x.first, num = x.second;
        if (v == p) 
        {
            ans[++ans[0]] = num;
            continue;
        }
        if (!vis1[v] || !vis2[v]) continue;
        // cout << "v:" << v << "\n";
        ans[++ans[0]] = num;
        dfs(v);
    }
}
void clear() 
{
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i <= n; i++) du[i] = vis1[i] = vis2[i] = 0;
    ans[0] = 0;
}
void solve()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) prt[i] = i;
    for (int i = 1; i <= m; i++) scanf("%d%d", &e[i].x, &e[i].y);
    int lim = 0;
    for (int i = 1; i <= m; i++)
    {
        int x = e[i].x, y = e[i].y;
        vis1[x] = vis1[y] = 1;
        int prtx = get_prt(x), prty = get_prt(y);
        if (prtx != prty) prt[prtx] = prty;
        else {
            lim = i;
            break;
        }
    }
    if (lim == 0) 
    {
        cout << "-1\n";
        return;
    }
    // cout << "lim:" << lim << "\n";
    for (int i = 1; i <= lim; i++)
    {
        int x = e[i].x, y = e[i].y;
        du[x]++, du[y]++;
        g[x].push_back(make_pair(y, i));
        g[y].push_back(make_pair(x, i));
    }
    for (int i = 1; i <= n; i++) vis2[i] = 1;
    queue<int> q;
    for (int i = 1; i <= n; i++) 
        if (du[i] == 1) q.push(i);
    while (q.size())
    {
        int u = q.front();
        q.pop();
        vis2[u] = 0;
        for (auto x : g[u])
        {
            int v = x.first;
            du[v]--;
            if (du[v] == 1) q.push(v);
        }
    } 
    // cout << "pos:";
    // for (int i = 1; i <= n; i++)
        // if (vis1[i] && vis2[i]) cout << i << " ";
    // cout << "\n";
    for (int i = 1; i <= n; i++)
        if (vis1[i] && vis2[i]) {
            p = i;
            dfs(i);
            break;
        }
    sort(ans + 1, ans + ans[0] + 1);
    int X = unique(ans + 1, ans + ans[0] + 1) - ans - 1;
    for (int i = 1; i <= X; i++) cout << ans[i] << " ";
    cout << "\n";
}
int main() 
{
    int T;
    scanf("%d", &T);
    while (T--) solve(), clear();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 8084kb

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 '