ural 1156. Two Rounds
1156. Two Rounds
Memory limit: 64 MB
Input
Output
Sample
input | output |
---|---|
2 3 |
1 4 |
Problem Source: Ural Collegiate Programming Contest, April 2001, Perm, English Round
#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的更多相关文章
- 1156. Two Rounds(dfs+背包)
1156 求出每个联通块的黑白块数 然后再背包 二维的背包 要保证每个块都得取一个 写的有些乱.. #include <iostream> #include<cstdio> # ...
- 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 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- 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 ...
- ural 2067. Friends and Berries
2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...
随机推荐
- [Effective JavaScript 笔记]第5章:数组和字典--个人总结
前言 这节里其实一直都在讨论对象这个在js中的万能的数据结构.对象可以表式为多种的形式,表示为字典和数组之间的区别.更多的我觉得这章讨论多的是一些对应实现功能的相关操作,有可能出现的bug以及如何避免 ...
- Unity 3D学习之 Prime31 Game Center插件用法
http://momowing.diandian.com/post/2012-11-08/40041806328 It's my life~: 为app 连入Game Center 功能而困扰的朋友们 ...
- 他们在军训,我在搞 OI(一)
Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...
- 百度图片爬虫-python版-如何爬取百度图片?
上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...
- Linux 实现rsyslog日志里面的IP地址记录 未测试
之前我是在bashrc中添加了一句,让系统操作日志时向rsyslog发送一份内容,现在只要在发送的时候,自己再获取下当前的远程登录IP加进去就可以,像这样 /etc/bashrc sshClientI ...
- Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- extern关键字总结
[本文链接] http://www.cnblogs.com/hellogiser/p/extern.html [extern 变量/函数] extern是C/C++语言中表明函数和全局变量作用范围(可 ...
- 1.10 编程之美-双线程下载[double threads to download]
[本文链接] http://www.cnblogs.com/hellogiser/p/double-threads-to-download-and-write.html [题目] 网络上下载数据,然后 ...
- ShortestPath:Silver Cow Party(POJ 3268)
牛的聚会 题目大意:一群牛在一块农田的不同的点,现在他们都要去到同一个地方开会,然后现在从那个地方回到原来的位置,点与点之间的连线都是单向的,并且通过一个路径需要一定时间,问你现在哪只牛需要最多的时间 ...
- codeforces C. Arithmetic Progression 解题报告
题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...