QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#111025#6560. Broken Minimum Spanning TreeTobo#WA 2ms3452kbC++202.0kb2023-06-05 15:21:352023-06-05 15:21:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-05 15:21:39]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:3452kb
  • [2023-06-05 15:21:35]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define N 3005

int n, m, fa[N], dep[N], rem[N], val[N], tr[N], id[N];
array<int, 3> e[N];
vector<array<int, 3>> adj[N];
void dfs(int cur, int f)
{
    dep[cur] = dep[f] + 1;
    fa[cur] = f;
    for (auto [i, x, d] : adj[cur])
    {
        if (i == f)
            continue;
        val[i] = x;
        id[i] = d;
        dfs(i, cur);
    }
}
int solve(int u, int v, int x)
{
    pair<int, int> s = {-1, -1};
    while (u != v)
    {
        if (dep[u] < dep[v])
            swap(u, v);
        if (s.second == -1 && val[u] > x)
            s = {id[u], val[u]};
        else if (val[u] > x && s.second > val[u])
            s = {id[u], val[u]};
        u = fa[u];
    }
    return s.first;
}
void solve()
{
    cin >> n >> m;
    vector<pair<int, int>> ans;
    for (int i = 1; i <= m; i++)
    {
        cin >> e[i][0] >> e[i][1] >> e[i][2];
        if (i < n)
        {
            adj[e[i][0]].push_back({e[i][1], e[i][2], i});
            adj[e[i][1]].push_back({e[i][0], e[i][2], i});
            tr[i] = 1;
        }
    }
    dfs(1, 0);
    sort(e + n, e + m + 1, [&](auto &e1, auto &e2)
         { return e1[2] < e2[2]; });
    for (int i = n; i <= m; i++)
    {
        int s = solve(e[i][0], e[i][1], e[i][2]);
        if (s == -1)
            continue;
        tr[s] = 0, rem[s] = 1;
        ans.push_back({s, i});
        tr[i] = 1;
        for (int i = 1; i <= n; i++)
            adj[i].clear();
        for (int j = i; j >= 1; j--)
        {
            if (!tr[i])
                continue;
            adj[e[i][0]].push_back({e[i][1], e[i][2], i});
            adj[e[i][1]].push_back({e[i][0], e[i][2], i});
        }
        dfs(1, 0);
    }
    cout << ans.size() << '\n';
    for (auto [l, r] : ans)
        cout << l << ' ' << r << '\n';
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    while (t--)
        solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3412kb

input:

4 4
1 2 10
2 3 3
3 4 1
1 4 4

output:

1
1 4

result:

ok correct!

Test #2:

score: -100
Wrong Answer
time: 2ms
memory: 3452kb

input:

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

output:

2
2 6
5 7

result:

FAIL participant's MST is better than jury!