• FZU2141——贪心——Sub-Bipartite Graph


    http://acm.fzu.edu.cn/problem.php?pid=2141

    /*
    大意:把一张图分成一个二分图,使得任意相邻的顶点不在同一个集合里面,
    */
    /************************************************
    * Author        :Powatr
    * Created Time  :2015-8-24 14:56:55
    * File Name     :FZU2141.cpp
     ************************************************/
    
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <sstream>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <queue>
    #include <deque>
    #include <stack>
    #include <list>
    #include <map>
    #include <set>
    #include <bitset>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    typedef long long ll;
    const int MAXN = 1e2 + 10;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9 + 7;
    
    int p1, p2;
    int mp[MAXN][MAXN];
    int pp[MAXN];
    int sum1, sum2;
    void check(int x)
    {
        int cout1 = 0, cout2 = 0;
        pp[x] = 1;
        for(int i = 1; i < x; i++){
            if(mp[i][x] && pp[x] != pp[i])
                cout1++;
        }
        pp[x] = 2;
        for(int i = 1; i < x; i++){
            if(mp[i][x] && pp[x] != pp[i])
                cout2++;
        }
        if(cout1 > cout2){
            pp[x] = 1;
            sum1++;
        }
        else {
            pp[x] = 2;
            sum2++;
        }
    }
    int main(){
        int T;
        int n, m, x, y;
        for(scanf("%d", &T); T--; ){
            scanf("%d%d", &n, &m);
            sum1 = sum2 = 0;
            memset(pp, 0, sizeof(pp));
            memset(mp, 0, sizeof(mp));
            for(int i = 1; i <= m; i++){
                scanf("%d%d", &x, &y);
                mp[x][y] = mp[y][x] = 1;
            }
            for(int i = 1; i <= n; i++){
                check(i);
            }
            int c1 = 0, c2 = 0;
            printf("%d%c", sum1, sum1 == 0 ? '
    ' : ' ');
            for(int i = 1; i <= n; i++){
                if(pp[i] == 1){
                    c1++;
                    printf("%d%c", i, c1 == sum1 ? '
    ' : ' ');
                }
            }
            printf("%d%c", sum2, sum2 == 0 ? '
    ' : ' ');
            for(int i = 1; i <= n; i++){
                if(pp[i] == 2){
                    c2++;
                    printf("%d%c", i, c2 == sum2 ? '
    ' : ' ');
                }
            }
        }
        return 0;
    }
                        
    

      

  • 相关阅读:
    程序员的学习和积累
    【STL】-迭代器的用法
    【STL】-list的用法
    【STL】-deque的用法
    【STL】- vector的用法
    数据结构-各种排序算法
    【STL】-priority_queue的用法
    数据结构-二项队列
    IT公司100题-8-智力题
    IT公司100题-7-判断两个链表是否相交
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4754802.html
Copyright © 2020-2023  润新知