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. POJ 1739

    楼教主男人八题之一... 题目大意: 求从左下角经过所有非障碍点一次到达右下角的方案数 这里不是求回路,但是我们可以考虑,在最下面一行再增加一行,那么就可以当做求此时左下角到右下角的回路总数,那么就转 ...

  2. LIS-Program E

    最大上升子序列 Description The world financial crisis is quite a subject. Some people are more relaxed whil ...

  3. 【转】linux /centos 中OpenSSL升级方法详解

    相关软件下载地址 Apache:http://httpd.apache.org/ Nginx:http://nginx.org/en/download.html OpenSSL:http://www. ...

  4. [转]建立swap分区

    1,fdisk 时设置id号82(t选项中L可查) 2,mkswap  /dev/sdXx 3,swapon /devsdXx 4,cat /proc/swap or swapon -s 就可以看到了 ...

  5. [Windows驱动]驱动包(Driver Packages)

    在windows下安装一个驱动,我们你需要把所有需要的软件打包-称为驱动包.驱动包里包括系统提供的给所有设备类使用的一般安装工具,还包括了设备商提供的设备特定的组件.下面我们就来看看驱动包里具体需要哪 ...

  6. stm32定义GPIO口方向和操作的代码

    #include "stm32f10x.h" #define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+(( ...

  7. 重拾java系列一java基础(4)

    本章主要回顾一些类的相关知识: (1)static: static 静态的: 属于类的资源, 使用类名访问.  静态属性: 只有一份的变量  静态方法: 是属于类方法, 可以使用类名直接访问. 静态方 ...

  8. Note_Master-Detail Application(iOS template)_04_ YJYMasterViewController.h

    //YJYMasterViewController.h #import <UIKit/UIKit.h> @classYJYDetailViewController; #import < ...

  9. 2016 - 1- 14 UI阶段学习补充 transform属性详解

    UIView的transform属性 transform是view的一个重要属性,它在矩阵层面上改变view的显⽰状态,能实现view的缩放.旋转.平移等功能.transform是CGAffineTr ...

  10. USB peripherals can turn against their users

    Turning USB peripherals into BadUSB USB devices are connected to – and in many cases even built into ...