QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#498415#7755. Game on a ForestMCdycWA 1ms3812kbC++201.7kb2024-07-30 14:13:532024-07-30 14:13:53

Judging History

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

  • [2024-07-30 14:13:53]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3812kb
  • [2024-07-30 14:13:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
int solve()
{
    int n, m;
    cin >> n >> m;

    vector<int> fa(n + 1), siz(n + 1, 1);
    iota(all(fa), 0);
    auto find = [&](auto self, int x) -> int
    {
        return fa[x] == x ? x : fa[x] = self(self, fa[x]);
    };

    auto merge = [&](int u, int v)
    {
        u = find(find, u);
        v = find(find, v);
        if (u != v)
        {
            fa[v] = u;
            siz[u] += siz[v];
        }
    };

    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        merge(u, v);
    }

    int cnt = 0;
    vector<bool> vis(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int x = find(find, i);
        if (siz[x] == 1)
        {
            cnt++;
        }
    }

    int ans = 0;
    for (int i = 1; i <= n; i++)
    {
        int x = find(find, i);
        if (vis[x])
            continue;
        vis[x] = true;

        // cout << x << ' ' << siz[x] << endl;

        if (cnt & 1)
        {
            if (siz[x] & 1)
            {
                ans += siz[x] - 1;
            }
            else
            {
                ans += siz[x];
            }
        }
        else
        {
            if (siz[x] & 1)
            {
                ans += siz[x];
            }
            else
            {
                ans += siz[x] - 1;
            }
        }
    }

    cout << ans;
    return 0;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int test = 1;
    // cin >> test;
    while (test--)
    {
        solve();
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3544kb

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3812kb

input:

100000 1
4647 17816

output:

99999

result:

wrong answer 1st numbers differ - expected: '1', found: '99999'