QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#596596#7755. Game on a Forestrxzfn639WA 1ms5304kbC++231.9kb2024-09-28 16:05:402024-09-28 16:05:42

Judging History

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

  • [2024-09-28 16:05:42]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:5304kb
  • [2024-09-28 16:05:40]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const i64 P = 998244353;
struct DSU {
    vector<int> fa, p, e, f;

    DSU(int n) {
        fa.resize(n + 1);
        iota(fa.begin(), fa.end(), 0);
        p.resize(n + 1, 1);
        e.resize(n + 1);
        f.resize(n + 1);
    }
    int get(int x) {
        if (x != fa[x]) {
            fa[x] = get(fa[x]);
        }
        return fa[x];
    }
    bool merge(int x, int y) { // 设x是y的祖先
        if (x == y) f[get(x)] = 1;
        x = get(x), y = get(y);
        e[x]++;
        if (x == y) return false;
        if (x < y) swap(x, y); // 将编号小的合并到大的上
        fa[y] = x;
        f[x] |= f[y], p[x] += p[y], e[x] += e[y];
        return true;
    }
    bool same(int x, int y) {
        return get(x) == get(y);
    }
    bool F(int x) { // 判断连通块内是否存在自环
        return f[get(x)];
    }
    int size(int x) { // 输出连通块中点的数量
        return p[get(x)];
    }
    int E(int x) { // 输出连通块中边的数量
        return e[get(x)];
    }
};
void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> in(n + 1);
    DSU dsu(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        dsu.merge(u, v);
        in[u]++, in[v]++;
    }
    int siz = 0, ans = 0;
    for (int i = 1; i <= n; i++) {
        if (dsu.get(i) == i) {
            siz++;
            int t = dsu.size(i);
            if (t % 2) ans += t;
            else ans += t - 1;
        }
    }
    // if ((siz + 1) % 2 == 0) ans += m;
    // for (int i = 1; i <= n; i++) {
    //     if ((siz - in[i]) % 2 == 0) ans++;
    // }
    cout << ans << '\n';
}
int main() {
    // ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int t = 1; 
    // cin >> t;
    while(t--) solve();
    return 0;
}
/*
3 1
1 2

4 3
1 2
2 3
3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

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

input:

100000 1
4647 17816

output:

99999

result:

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