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 ;
}

ural 1156. Two Rounds的更多相关文章

  1. 1156. Two Rounds(dfs+背包)

    1156 求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个 写的有些乱.. #include <iostream> #include<cstdio> # ...

  2. ural 1246. Tethered Dog

    1246. Tethered Dog Time limit: 1.0 secondMemory limit: 64 MB A dog is tethered to a pole with a rope ...

  3. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  4. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  5. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  6. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  7. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  8. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  9. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

随机推荐

  1. HDU 4915 Parenthese sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915 解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从 ...

  2. NGUI 粒子显示在上级

    http://bbs.taikr.com/thread-2272-1-1.html [NGUI]3.0+版本,粒子在UI后面显示 -- : 48人阅读 评论() 收藏 举报 [Unity3D][NGU ...

  3. convert jar to java

    (文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 首先解压一下下载的jar文件,extract jar file , $jar -xvf file.j ...

  4. [LA3026]Period

    [LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...

  5. Coursera台大机器学习技法课程笔记02-Dual Support Vector Machine

    这节课讲的是SVM的对偶问题,比较精彩的部分:为何要使用拉格朗日乘子以及如何进行对偶变换. 参考:http://www.cnblogs.com/bourneli/p/4199990.html http ...

  6. Linux CPU负载

    昨天查看Nagios警报信息,发现其中一台服务器CPU负载过重,机器为CentOS系统.信息如下: 2011-2-15 (星期二) 17:50 WARNING - load average: 9.73 ...

  7. WCDMA是什么意思?CDMA是什么意思?GSM是什么意思

    有些朋友在购买3G智能手机的时候会遇到这样的困惑,为什么相同的手机会有不同手机网络制式之分呢?有的支持WCDMA/GSM,有的支持CDMA/GSM,到底自己应该选购哪一种手机好呢?WCDMA是什么意思 ...

  8. ssh: connect to host localhost port 22: Connection refused 问题

    错误原因:1.sshd 未安装2.sshd 未启动 3.防火墙 4需重新启动ssh 服务 解决方法:1.确定安装sshd: $ sudo apt-get install openssh-server ...

  9. android.content.ActivityNotFoundException: Unable to find explicit activity class have you declared this activity in your AndroidManifest.xml?

    在整合PullToRefresh的时候出现如下异常 10-22 23:20:01.826 32331-32331/com.example.news.andoridnewsapp E/AndroidRu ...

  10. iOS 利用self.navigationItem.backBarButtonItem修改后退按钮文字

    @property(nonatomic,retain) UIBarButtonItem *backBarButtonItem; // Bar button item to use for the ba ...