判断二分图

bool dfs(int cur, int fa, int color)
{
    col[cur] = color;
    for (int i = head[cur]; i; i = edge[i].next)
    {
        int to = edge[i].to;
        if (to == fa) continue;
        if (col[to] == color)
        {
            return false;
        }
        if (col[to] == 0 && !dfs(to, cur, 3-color))
        {
            return false;
        }
    }
    return true;
}
posted @ 2026-03-24 11:47  msjing  阅读(10)  评论(0)    收藏  举报