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. [Effective JavaScript 笔记]第5章:数组和字典--个人总结

    前言 这节里其实一直都在讨论对象这个在js中的万能的数据结构.对象可以表式为多种的形式,表示为字典和数组之间的区别.更多的我觉得这章讨论多的是一些对应实现功能的相关操作,有可能出现的bug以及如何避免 ...

  2. Unity 3D学习之 Prime31 Game Center插件用法

    http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...

  3. 他们在军训,我在搞 OI(一)

    Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...

  4. 百度图片爬虫-python版-如何爬取百度图片?

    上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...

  5. Linux 实现rsyslog日志里面的IP地址记录 未测试

    之前我是在bashrc中添加了一句,让系统操作日志时向rsyslog发送一份内容,现在只要在发送的时候,自己再获取下当前的远程登录IP加进去就可以,像这样 /etc/bashrc sshClientI ...

  6. Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. extern关键字总结

    [本文链接] http://www.cnblogs.com/hellogiser/p/extern.html [extern 变量/函数] extern是C/C++语言中表明函数和全局变量作用范围(可 ...

  8. 1.10 编程之美-双线程下载[double threads to download]

    [本文链接] http://www.cnblogs.com/hellogiser/p/double-threads-to-download-and-write.html [题目] 网络上下载数据,然后 ...

  9. ShortestPath:Silver Cow Party(POJ 3268)

    牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...

  10. codeforces C. Arithmetic Progression 解题报告

    题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...