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. 火车安排问题(dp好题)

    火车站内往往设有一些主干线分叉出去的铁路支路,供火车停靠,以便上下客或装载货物.铁路 支路有一定长度:火车也有一定的长度,且每列火车的长度相等. 假设某东西向的铁路上,有一小站.该站只有一条铁路支路可 ...

  2. Android 系统基础

    当系统启动一个组件,它其实就启动了这个程序的进程(如果这个进程还未被启动的话)并实例化这个组件所需要的类. 例如,如果你的程序启动了相机程序里的activity去拍照,这个activity实际上是运行 ...

  3. [windows驱动]内核态驱动架构

    1.windows驱动简介: 1.1 windows组件简介: 1.2 windows驱动类型: windows驱动分为两种基本类型: 用户态驱动在用户态下执行.它们一般提供一套win32应用程序和内 ...

  4. win8系统 host文件无法修改解决之道

    host文件,路径为:C:\windows\system32\drivers\etc\hosts 方法/步骤: 方法1:用notepad++打开host文件,修改和保存 方法2:(1)首先用管理管权限 ...

  5. for循环进阶

    [引例] 输出一行10个“*” #include<cstdio> int main(){ printf("**********\n"); ; } 思考: (1)输出一行 ...

  6. Core Animation系列之CADisplayLink

    一直以来都想好好学习下CoreAnimation,奈何涉及的东西太多,想要一次性全部搞定时间上不允许,以后会断断续续的补全.最近项目里用到了CADisplayLink,就顺便花点时间看了看. 一.简介 ...

  7. Oracle数据库中序列用法讲解

    序列(SEQUENCE)是序列号生成器,可以为表中的行自动生成序列号,产生一组等间隔的数值(类型为数字).其主要的用途是生成表的主键值,可以在插入语句中引用,也可以通过查询检查当前值,或使序列增至下一 ...

  8. 无法为表空间 ***中的段创建 INITIAL 区

    这是由于表空间不足引起的. 具体错误: 解决方案:扩展表空间

  9. 数据结构《14》----并查集 Union-Find

    描述: 并查集是一种描述解决等价关系.能够方便地描述不相交的多个集合. 支持如下操作    1. 建立包含元素 x 的集合  MakeSet(x) 2. 查找给定元素所在的集合 Find(x), 返回 ...

  10. linux常用命令:4文件压缩和解压命令

    文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...