QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#688445#7755. Game on a Foresticpc_zhzx034#WA 2ms6612kbC++142.3kb2024-10-30 09:21:442024-10-30 09:21:44

Judging History

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

  • [2024-10-30 09:21:44]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:6612kb
  • [2024-10-30 09:21:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e5 + 5;

struct edge {
	int to, Next;

	edge() {}
	edge(int to, int Next): to(to), Next(Next) {}
} G[N << 1];

int head[N], cnt;

inline void add_edge(int u, int v) {
	G[++cnt] = edge(v, head[u]);
	head[u] = cnt;
}

int n, m, ans, cnt1, cnt2, fa[N], siz[N];

inline int _find(int u) {
	return (fa[u] == u) ? u : (fa[u] = _find(fa[u]));
}

inline void merge(int u, int v) {
	u = _find(u), v = _find(v);
	if (u == v) {
		return;
	}
	fa[u] = v;
	siz[v] += siz[u];
}

int sp, S[N];

inline void init(int fa, int u) {
	S[u] = 1;
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa) {
			continue;
		}
		init(u, v);
		S[u] += S[v];
	}
}

inline void dfs(int fa, int u) {
	int t1 = cnt1, t2 = cnt2;
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa) {
			continue;
		}
		if (siz[v] & 1) {
			t1 ^= 1;
		} else {
			t2 ^= 1;
		}
	}
	if ((sp - S[u]) & 1) {
		t1 ^= 1;
	} else {
		t2 ^= 1;
	}
	if (!t1 && !t2) {
		++ans;
	}
	for (int i = head[u]; i; i = G[i].Next) {
		int v = G[i].to;
		if (v == fa) {
			continue;
		}
		dfs(u, v);
		t1 = cnt1, t2 = cnt2;
		if (S[v] & 1) {
			t1 ^= 1;
		} else {
			t2 ^= 1;
		}
		if ((sp - S[v]) & 1) {
			t1 ^= 1;
		} else {
			t2 ^= 1;
		}
		if (!t1 && !t2) {
			++ans;
		}
	}
}

int main() {
	#ifdef LOCAL
		assert(freopen("test.in", "r", stdin));
		assert(freopen("test.out", "w", stdout));
	#endif
	ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		fa[i] = i;
		siz[i] = 1;
	}
	while (m--) {
		int u, v;
		cin >> u >> v;
		merge(u, v);
		add_edge(u, v);
		add_edge(v, u);
	}
	for (int i = 1; i <= n; ++i) {
		if (_find(i) != i) {
			continue;
		}
		if (siz[i] & 1) {
			cnt1 ^= 1;
		} else {
			cnt2 ^= 1;
		}
	}
	if (!cnt1 && !cnt2) {
		cout << "0\n";
		return 0;
	}
	for (int i = 1; i <= n; ++i) {
		if (_find(i) == i) {
			init(0, i);
		}
	}
	for (int i = 1; i <= n; ++i) {
		if (_find(i) == i) {
			sp = siz[i];
			if (sp & 1) {
				cnt1 ^= 1;
			} else {
				cnt2 ^= 1;
			}
			dfs(0, i);
			if (sp & 1) {
				cnt1 ^= 1;
			} else {
				cnt2 ^= 1;
			}
		}
	}
	cout << ans << "\n";
	return 0;
}

详细

Test #1:

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

input:

3 1
1 2

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

4 3
1 2
2 3
3 4

output:

3

result:

ok 1 number(s): "3"

Test #3:

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

input:

100000 1
4647 17816

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

100000 2
64075 72287
63658 66936

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 2ms
memory: 4812kb

input:

100000 3
59930 72281
31689 59132
20469 33165

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 1ms
memory: 4500kb

input:

100000 10
20391 78923
27822 80617
21749 25732
12929 79693
42889 52515
59064 99869
29031 41875
4463 17813
13407 42498
19120 20957

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: -100
Wrong Answer
time: 2ms
memory: 6612kb

input:

99999 101
34378 94161
67696 83255
24557 25591
11476 58475
5684 38157
33843 35321
9046 24028
14293 77681
587 42098
9402 27228
6999 13980
27118 84005
3622 8353
13545 51621
16998 63647
32912 53178
15206 15815
56517 86335
5563 93770
153 278
11242 41753
75098 76792
1695 22836
25936 33352
2765 6778
19597 ...

output:

99898

result:

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