cheran-senthil / PyRival

⚡ Competitive Programming Library
https://pyrival.readthedocs.io/
Apache License 2.0
1.17k stars 312 forks source link

is_bipartite() got wrong color #43

Closed Terryobeyes closed 4 years ago

Terryobeyes commented 4 years ago

Python Version: 3.7

Describe the bug I was using is_bipartite() on https://codeforces.com/contest/862/problem/B this is my code (omit def is_bipartite):

N = int(input())
g = [[] for _ in range(N)]
for i in range(N - 1):
    u, v = map(int, input().split())
    g[u - 1].append(v - 1)
colors = is_bipartite(g)[1]
left = colors.count(0)
right = N - left
print(left * right - N + 1)
# print(colors)

And I got WA on test 3 :(

The input data is:

10
3 8
6 2
9 7
10 1
3 5
1 3
6 7
5 4
3 6

and the colors in my program is

[0, 1, 1, 1, 0, 0, 1, 0, 0, 0]

Expected

[0, 1, 1, 1, 0, 0, 1, 0, 0, 1]

Additional context It seen the bug will be happend if there are edge start from the last point. like:

3
1 2
3 2

Maybe my code or usage was wrong. idk I'm a cf specialist from Taiwan. Sorry to bother.

bjorn-martinsson commented 4 years ago

An undirected graph needs edges in both direction, so you need

g[u - 1].append(v - 1)
g[v - 1].append(u - 1)