QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#498430#7755. Game on a ForestMCdycWA 0ms3956kbC++201.9kb2024-07-30 14:34:112024-07-30 14:34:12

Judging History

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

  • [2024-07-30 14:34:12]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3956kb
  • [2024-07-30 14:34:11]
  • 提交

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++;
        }
    }

    if (cnt == n)
    {
        if (cnt & 1)
        {
            cout << n;
            return 0;
        }
        else
        {
            cout << 0;
            return 0;
        }
    }

    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: 3476kb

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 3956kb

input:

100000 1
4647 17816

output:

99999

result:

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