QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#498415 | #7755. Game on a Forest | MCdyc | WA | 1ms | 3812kb | C++20 | 1.7kb | 2024-07-30 14:13:53 | 2024-07-30 14:13:53 |
Judging History
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'