UVA11396-Claw decomposition (bipartite graph determination)

Source: Internet
Author: User

Question Link


Question: Can a undirected connected graph be divided into multiple claw shapes. Each edge can belong to only one claw type, and the degree of each vertex is 3.

Idea: When a graph is broken down into a type of stem claw, each side only belongs to one paw, so the two points of each side must be in two different chicken claws.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int MAXN = 305;vector<int> g[MAXN];int n, color[MAXN];void init() {    for (int i = 0; i < n; i++)         g[i].clear();}bool bipartite(int u) {    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];         if (color[u] == color[v]) return false;         if (!color[v]) {            color[v] = 3 - color[u];            if (!bipartite(v)) return false;        }    }    return true;}int main() {    while (scanf("%d", &n) && n) {        init();        int u, v;         while (scanf("%d%d", &u, &v)) {            if (u == 0 && v == 0)                break;            u--;            v--;            g[u].push_back(v);            g[v].push_back(u);        }        memset(color, 0, sizeof(color));        color[0] = 1;          if(bipartite(0))              printf("YES\n");          else              printf("NO\n");      }     return 0;}


UVA11396-Claw decomposition (bipartite graph determination)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.