ural 1156. Two Rounds

时间:2023-03-09 06:12:28
ural 1156. Two Rounds

1156. Two Rounds

Time limit: 2.0 second
Memory limit: 64 MB
There are two rounds in the Urals Championship. The competitors have to solve N problems on each round. The jury had been working hard and finally managed to prepare 2N problems for the championship. But it appeared that among those problems there were some, which have the analogous solutions. One shouldn’t assign such a problems for the same round. Please, help the jury form sets of tasks for each of the rounds.

Input

First line contains two numbers: N, the number of tasks for a round, and M, the number of pairs of tasks which should not be assigned for one round (1 ≤ N ≤ 50; 0 ≤ M ≤ 100). Then M lines follow, each of them contains two numbers of analogous tasks.

Output

Output two lines, containing numbers of tasks assigned for each round. If there is no solution, output the only word “IMPOSSIBLE”. If there are more than one solution you may assume anyone of them.

Sample

input output
2 3
1 3
2 1
4 3
1 4
2 3
Problem Author: Eugene Bryzgalov
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round 
Difficulty: 342
题意:给出n,代表有2*n个点,给出m对关系,每队表示a,b不能分一组。问将2*n个点   平均    分成两组的方案。
分析:首先判断无解情况是很显然的,将点连边,奇偶分层后就行了
一堆相互联系的点(即有边相连或间接相连的点)分开的情况是确定的,比如奇数层的点数a,偶数层的点数b,a,b是确定的,只是a,b哪个去哪个组未确定。
因为每组都要n个点,所以只能背包了。
 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name) {
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint() {
int Ret = ;
char Ch = ' ';
while(!(Ch >= '' && Ch <= '')) Ch = getchar();
while(Ch >= '' && Ch <= '') {
Ret = Ret*+Ch-'';
Ch = getchar();
}
return Ret;
} const int N = ;
int n, m;
bool Map[N][N];
int Color[N], Pair[N][], Len, Who[N];
bool Dp[N][N][N], Flag, Visit[N];
int Ans[N];
vector<int> Day[]; inline void Input() {
scanf("%d%d", &n, &m);
For(i, , m) {
int a, b;
scanf("%d%d", &a, &b);
Map[a][b] = Map[b][a] = ;
}
} inline void Search(int x, int C) {
if(Flag) return;
Color[x] = C, Pair[Len][C-]++;
int _C = ((C-)^)+;
For(i, , n*)
if(Map[x][i]) {
if(Color[i] && Color[i] != _C) {
Flag = ;
break;
} if(!Color[i]) Search(i, _C);
}
} inline void Solve() {
For(i, , n*)
if(!Color[i]) {
Who[++Len] = i;
Search(i, );
if(Flag) break;
} if(Flag) {
puts("IMPOSSIBLE");
return;
} Dp[][][] = ;
For(i, , Len) {
int a = Pair[i][], b = Pair[i][];
For(j, , n)
For(k, , n) {
if(j+a <= n && k+b <= n)
Dp[i][j+a][k+b] |= Dp[i-][j][k];
if(j+b <= n && k+a <= n)
Dp[i][j+b][k+a] |= Dp[i-][j][k];
}
} if(!Dp[Len][n][n]) {
puts("IMPOSSIBLE");
return;
} int a = n, b = n;
Ford(i, Len, ) {
if(a >= Pair[i][] && b >= Pair[i][] && Dp[i-][a-Pair[i][]][b-Pair[i][]]) {
Ans[Who[i]] = , Visit[i] = ;
a -= Pair[i][], b -= Pair[i][];
} else if(a >= Pair[i][] && b >= Pair[i][] && Dp[i-][a-Pair[i][]][b-Pair[i][]]) {
Ans[Who[i]] = , Visit[i] = ;
a -= Pair[i][], b -= Pair[i][];
}
} clr(Color, );
For(i, , n*)
if(Ans[i]) Search(i, Ans[i]); For(i, , n*)
Day[Color[i]-].pub(i);
Rep(i, ) {
int Length = sz(Day[i]);
Rep(j, Length-) printf("%d ", Day[i][j]);
printf("%d\n", Day[i].back());
}
} int main() {
#ifndef ONLINE_JUDGE
SetIO("G");
#endif
Input();
Solve();
return ;
}