link: http://codeforces.com/contest/330/problem/E

 /*
ID: zypz4571
LANG: C++
TASK: 192e.cpp
*/ #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
map<pair<int,int>,bool> coll;
int a[];
int main ( int argc, char *argv[] )
{
#ifndef ONLINE_JUDGE
//freopen("in.txt", "r", stdin);
#endif
int n, m; cin>>n>>m;
double d=clock(); srand(time());
for (int i = , x, y; i < m; ++i) {
cin>>x>>y;
coll.insert(make_pair(make_pair(x,y),true));
coll.insert(make_pair(make_pair(y,x),true));
}
for (int i = ; i < n; ++i) a[i] = i+;
bool t;
while ((clock()-d)/CLOCKS_PER_SEC < 2.9) {
random_shuffle(a, a+n);
a[n] = a[]; t = false;
for (int i = ; i < m; ++i)
if (coll[make_pair(a[i],a[i+])]) {
t = true; break;
}
if (!t) break;
}
if (t) printf("-1\n");
else for (int i = ; i < m; ++i) printf("%d %d\n", a[i], a[i+]);
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

note the use of random_shuffle.

the possibility of each try of success is about (1-2/n)^n which is big enough when n is very large so we can use this nondeterminisitic solution.

codeforces 192e的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. windows下svn自动更新

    配置hooks下post-commit.bat文件,文件内容如下 @echo offSET REPOS=%1SET REV=%2SET DIR=%REPOS%/hooksSET PATH=%PATH% ...

  2. Apache虚拟主机(三)

    一.启用 httpd-vhosts.conf 在httpd.conf文件中启用 在文件中搜索:Virtual hosts #Virtual hosts虚拟主机 Include conf/extra/h ...

  3. 获得servletContext的路径的方法

    request.getSession().getServletContext().getRealPath("")或 System.getProperty("webapp. ...

  4. Arrays.equals()

    我们知道判断字符串相等使用的是equals方法,那么要是判断两个字符串数组是否相等呢,要是char数组呢,同样的java.util.Arrays类提供了equals()方法,如下是官方API: /** ...

  5. sql 如何把查询得到的结果如何放入一个新表中

    如何把这个查询到的结果放到一张新表中? 2014-03-13 15:26   提问者采纳   表已经存在:insert into 表名 (列名1... 列名n) select 列名1....列名n f ...

  6. php大力力 [004节]PHP常量MAMP环境下加载网页

    我的问题是:“让mamp加载PHP文件”. 这个特别简单的问题,刚才也把我憋了几个钟头,唉....土啊,新学一个东西,学习成本就是高. 刚刚吃了好吃的南邵小龙虾,以及美味的八里桥大螃蟹,痛苦了半天,终 ...

  7. 黑马程序员——【Java基础】——String、StringBuffer和基本数据包装类

    ---------- android培训.java培训.期待与您交流! ---------- 类String---------------------------------------------- ...

  8. 查询某个表或者所有表的字段说明 SQLServer

    查询某个表或者所有表的字段说明SELECT    [Table Name] = OBJECT_NAME(c.object_id),    [Column Name] = c.name,    [Des ...

  9. Thread和Service应用场合的区别

    Thread和Service的区别: 1) Thread 是程序执行的最小单元,它是分配CPU的基本单位,可以用 Thread 来执行一些异步的操作. 如果是Local Service,那么对应的 S ...

  10. 走过路过不要错过~教你用java抓取网页中你想要的东东~~

    学习了正则之后,打算用java玩一玩,所以就决定用它来实现一个好玩的idea import java.io.BufferedReader; import java.io.IOException; im ...