您的位置:首页 > 其它

ural 1156. Two Rounds

2015-10-22 22:08 218 查看

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

inputoutput
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

Tags: graph theory (hide tags for unsolved problems)
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 = 0;
char Ch = ' ';
while(!(Ch >= '0' && Ch <= '9')) Ch = getchar();
while(Ch >= '0' && Ch <= '9') {
Ret = Ret*10+Ch-'0';
Ch = getchar();
}
return Ret;
}

const int N = 110;
int n, m;
bool Map

;
int Color
, Pair
[2], Len, Who
;
bool Dp

, Flag, Visit
;
int Ans
;
vector<int> Day[2];

inline void Input() {
scanf("%d%d", &n, &m);
For(i, 1, m) {
int a, b;
scanf("%d%d", &a, &b);
Map[a][b] = Map[b][a] = 1;
}
}

inline void Search(int x, int C) {
if(Flag) return;
Color[x] = C, Pair[Len][C-1]++;
int _C = ((C-1)^1)+1;
For(i, 1, n*2)
if(Map[x][i]) {
if(Color[i] && Color[i] != _C) {
Flag = 1;
break;
}

if(!Color[i]) Search(i, _C);
}
}

inline void Solve() {
For(i, 1, n*2)
if(!Color[i]) {
Who[++Len] = i;
Search(i, 1);
if(Flag) break;
}

if(Flag) {
puts("IMPOSSIBLE");
return;
}

Dp[0][0][0] = 1;
For(i, 1, Len) {
int a = Pair[i][0], b = Pair[i][1];
For(j, 0, n)
For(k, 0, n) {
if(j+a <= n && k+b <= n)
Dp[i][j+a][k+b] |= Dp[i-1][j][k];
if(j+b <= n && k+a <= n)
Dp[i][j+b][k+a] |= Dp[i-1][j][k];
}
}

if(!Dp[Len]

) {
puts("IMPOSSIBLE");
return;
}

int a = n, b = n;
Ford(i, Len, 1) {
if(a >= Pair[i][0] && b >= Pair[i][1] && Dp[i-1][a-Pair[i][0]][b-Pair[i][1]]) {
Ans[Who[i]] = 1, Visit[i] = 1;
a -= Pair[i][0], b -= Pair[i][1];
} else if(a >= Pair[i][1] && b >= Pair[i][0] && Dp[i-1][a-Pair[i][1]][b-Pair[i][0]]) {
Ans[Who[i]] = 2, Visit[i] = 1;
a -= Pair[i][1], b -= Pair[i][0];
}
}

clr(Color, 0);
For(i, 1, n*2)
if(Ans[i]) Search(i, Ans[i]);

For(i, 1, n*2)
Day[Color[i]-1].pub(i);
Rep(i, 2) {
int Length = sz(Day[i]);
Rep(j, Length-1) printf("%d ", Day[i][j]);
printf("%d\n", Day[i].back());
}
}

int main() {
#ifndef ONLINE_JUDGE
SetIO("G");
#endif
Input();
Solve();
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: