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 ? '\n' : ' '); for(int i = 1; i <= n; i++){ if(pp[i] == 1){ c1++; printf("%d%c", i, c1 == sum1 ? '\n' : ' '); } } printf("%d%c", sum2, sum2 == 0 ? '\n' : ' '); for(int i = 1; i <= n; i++){ if(pp[i] == 2){ c2++; printf("%d%c", i, c2 == sum2 ? '\n' : ' '); } } } return 0; }