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. checkbox全选功能

    $("#cb_classType_all").change(function () { if ($(this).is(":checked")) { $(&quo ...

  2. jquery插件理解看这

    zepto 插件写法 一个更换背景颜色的小插件 html 1 <div id="box">content</div> javascript 12345678 ...

  3. UITableViewStyleGrouped 模式下 headview 多出一块高度问题

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView ...

  4. 国产ProcessOn和国外gliffy的对比区别【原创】

    之前一直在用国外的作图工具gliffy,不足之处gliffy是英文的,很多国内相关从业者使用起来就有一定门槛,今天我给大家再推荐一款比gliffy更方便的作图工具ProcessOn,除了绘制UML建模 ...

  5. 手机app常见bug积累

    经过一年的测试工作,以下是手机APP比较容易出现的错误.之后如果发现了还会继续添加,修改.1.翻页手机客户端,内容超过一页时,上拉加载更多内容,加载错误(容易出现数据重复,图片和文章不匹配,图片重复加 ...

  6. 移动设备和SharePoint 2013 - 第2部分:设备管道和SharePoint页面模型

    博客地址:http://blog.csdn.net/foxdave 原文地址 在该系列文章中,作者展示了SharePoint 2013最显著的新功能概观--对移动设备的支持. 该系列文章: 移动设备和 ...

  7. c++的调试与运行

    编译F9:运行F10:编译运行F11. 设置断点:在代码所在行的行首单击,该行即被加亮.注意:设置断点后,此时程序运行进入调试状态,要想运行程序,就不能使用F10或者F11,而是要使用F5调试,然后使 ...

  8. (转)iOS消息推送机制的实现

    原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...

  9. 深入分析:Fragment与Activity交互的几种方式(二,使用Bundle)

    首先我们需要在Activity中动态添加Fragment时,用Bundle封装我们需要传递的数据. public void button(View view) { ArgFragment arg = ...

  10. 后序/中序--->前序

    preOrder 5 3 2 4 8 6 9   midOrder 2 3 4 5 6 8 9  postOrder 2 4 3 6 9 8 5 #include <iostream> # ...