QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#287127 | #7755. Game on a Forest | Max_s_xaM | WA | 1ms | 9320kb | C++14 | 3.5kb | 2023-12-19 18:54:56 | 2023-12-19 18:54:56 |
Judging History
answer
#include <iostream>
#include <vector>
typedef long long ll;
typedef double lf;
// #define DEBUG 1
struct IO
{
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
#endif
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
#define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')
template <typename T>
void Read(T &x)
{
#if DEBUG
std::cin >> x;
#else
bool sign = 0; char ch = gc(); x = 0;
for (; !isdigit(ch); ch = gc())
if (ch == '-') sign = 1;
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
if (sign) x = -x;
#endif
}
void Read(char *s)
{
#if DEBUG
std::cin >> s;
#else
char ch = gc();
for (; blank(ch); ch = gc());
for (; !blank(ch); ch = gc()) *s++ = ch;
*s = 0;
#endif
}
void Read(char &c) {for (c = gc(); blank(c); c = gc());}
void Push(const char &c)
{
#if DEBUG
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
template <typename T>
void Write(T x)
{
if (x < 0) x = -x, Push('-');
static T sta[35];
int top = 0;
do sta[top++] = x % 10, x /= 10; while (x);
while (top) Push(sta[--top] ^ 48);
}
template <typename T>
void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)
using namespace std;
const int MAXN = 1e5 + 10;
int n, m;
struct Edge
{
int u, v;
}g[MAXN];
vector <int> e[MAXN];
int fa[MAXN], sz[MAXN], rt[MAXN];
void DFS(int u, int t, int tt)
{
fa[u] = t, sz[u] = 1, rt[u] = tt;
for (auto v : e[u])
if (v != t) DFS(v, u, tt), sz[u] += sz[v];
}
int cnt[2];
int main()
{
#if DEBUG
#else
ios::sync_with_stdio(0), cin.tie(0);
#endif
Read(n), Read(m);
for (int i = 1; i <= m; i++) Read(g[i].u), Read(g[i].v), e[g[i].u].push_back(g[i].v), e[g[i].v].push_back(g[i].u);
for (int i = 1; i <= n; i++)
if (!sz[i]) DFS(i, 0, i);
for (int i = 1; i <= n; i++)
if (rt[i] == i) cnt[sz[i] & 1]++;
int ans = 0;
for (int i = 1; i <= m; i++)
{
cnt[sz[rt[g[i].u]] & 1]--;
if (fa[g[i].u] == g[i].v) cnt[sz[g[i].u] & 1]++, cnt[(sz[rt[g[i].u]] - sz[g[i].u]) & 1]++;
else cnt[sz[g[i].v] & 1]++, cnt[(sz[rt[g[i].u]] - sz[g[i].v]) & 1]++;
if ((cnt[0] & 1) || (cnt[1] & 1)) ans++;
if (fa[g[i].u] == g[i].v) cnt[sz[g[i].u] & 1]--, cnt[(sz[rt[g[i].u]] - sz[g[i].u]) & 1]--;
else cnt[sz[g[i].v] & 1]--, cnt[(sz[rt[g[i].u]] - sz[g[i].v]) & 1]--;
cnt[sz[rt[g[i].u]] & 1]++;
}
for (int i = 1; i <= n; i++)
{
cnt[sz[rt[i]] & 1]--;
for (auto v : e[i])
if (v == fa[i]) cnt[(sz[rt[i]] - sz[i]) & 1]++;
else cnt[sz[v]]++;
if ((cnt[0] & 1) || (cnt[1] & 1)) ans++;
for (auto v : e[i])
if (v == fa[i]) cnt[(sz[rt[i]] - sz[i]) & 1]--;
else cnt[sz[v]]--;
cnt[sz[rt[i]] & 1]++;
}
cout << ans << "\n";
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7904kb
input:
3 1 1 2
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 8032kb
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: 9320kb
input:
100000 1 4647 17816
output:
100000
result:
wrong answer 1st numbers differ - expected: '1', found: '100000'