• FZU 2141 Sub-Bipartite Graph


    Sub-Bipartite Graph
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Given a simple undirected graph G with n vertices and m edges, your task is to select a sub-bipartite graph of G with at least m/2 edges.

    In the mathematical field of graph theory, a bipartite graph (or bigraph) is a graph whose vertices can be divided into two disjoint sets U and V such that every edge connects a vertex in U to one in V; that is, U and V are each independent sets. Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

    Equivalently, a bipartite graph is a graph that does not contain any odd-length cycles.

    In the mathematical field of graph theory, a subgraph is a graph G whose graph vertices and graph edges form subsets of the graph vertices and graph edges of a given graph G..

    In graph theory, a simple graph is a graph containing no self-loops or multiple edges.

    from wikipedia

    Input

    The first line of the date is an integer T, which is the number of the text cases.

    Then T cases follow, each case starts of two numbers N and M, representing the number of vertices and the number of edges, then M lines follow. Each line contains two integers x and y, means that there is an edge connected x and y. The number of nodes is from 1 to N.

    1 <= T <= 100, 1 <= N <= 100, 0 <= M <= 10086

    Output

    For each case, you should output two lines to describe your sub-graph, the first line is the set of U and the second line is the set of V.

    Each line should output an integer F first, which is the total number of the vertices in this set, then F integers follow which are the number of each vertex of this part, see sample input and sample output for more details.

    You can assume that the answer is always existed.

    Sample Input

    3 1 0 2 1 1 2 3 3 1 2 2 3 1 3

    Sample Output

    1 1 0 1 1 1 2 2 1 2 1 3

    Hint

    This problem is special judge.

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int g[110][110],color[110],B,W;
     7 
     8 int cal(int p)
     9 {
    10     int b=0,w=0;
    11     int i;
    12     color[p]=1;
    13     for(i=1;i<p;++i)
    14         if(g[i][p] && color[i]!=color[p]) b++;
    15     color[p]=2;
    16     for(i=1;i<p;++i)
    17         if(g[i][p] && color[i]!=color[p]) w++;
    18     if(b>w) color[p]=1,B++;
    19     else color[p]=2,W++;
    20 }
    21 
    22 int main()
    23 {
    24     int T,n,m;
    25     scanf("%d",&T);
    26     while(T--)
    27     {
    28         int i,j;
    29         scanf("%d%d",&n,&m);
    30         memset(color,0,sizeof(color));
    31         memset(g,0,sizeof(g));
    32         int u,v;
    33         for(i=1;i<=m;++i)
    34         {
    35             scanf("%d%d",&u,&v);
    36             g[u][v]=1;
    37             g[v][u]=1;
    38         }
    39         
    40         B=W=0;
    41         for(i=1;i<=n;++i)
    42             cal(i);
    43         printf("%d",B);
    44         for(i=1;i<=n;++i)
    45             if(color[i]==1) printf(" %d",i);
    46         printf("
    ");
    47         printf("%d",W);
    48         for(i=1;i<=n;++i)
    49             if(color[i]==2) printf(" %d",i);
    50         printf("
    ");
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    如何删除.DS_Store文件?
    STL 技巧整理 7/22
    贪心
    防线
    bfs&dfs模板
    并查集学习总结
    二叉树相关代码
    区间DP学习笔记
    多算法模板整理
    防线题解
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771425.html
Copyright © 2020-2023  润新知