C. Graph Reconstruction

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/329/problem/C

Description

I have an undirected graph consisting of n nodes, numbered 1 through n. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.

I would like to create a new graph in such a way that:

  • The new graph consists of the same number of nodes and edges as the old graph.
  • The properties in the first paragraph still hold.
  • For each two nodes u and v, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.

Help me construct the new graph, or tell me if it is impossible.

Under two situations the player could score one point.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

The first line consists of two space-separated integers: n and m (1 ≤ m ≤ n ≤ 105), denoting the number of nodes and edges, respectively. Then m lines follow. Each of the m lines consists of two space-separated integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting an edge between nodes u and v.

Output

If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly m lines. Each line should contain a description of edge in the same way as used in the input format.

Sample Input

8 7
1 2
2 3
4 5
5 6
6 8
8 7
7 4

Sample Output

1 4
4 6
1 6
2 7
7 5
8 5
2 8

HINT

题意

给你一个n个点m条边的无向图

无自环,无重边,每个点的度数最多为2

然后让你找到一个图,使得性质一样,但是之前相连的边,之后不能相连

题解:

随机化,首先满足题意的图应该有很多个,所以瞎随……

注意随机的时候,用一种保证每个点的度数最多为2的方式随机就好了

代码

#include<iostream>
#include<stdio.h>
#include<map>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std; map<pair<int,int> ,int>H;
int main()
{
srand(time(NULL));
int n,m;
scanf("%d%d",&n,&m);
vector<int> Q;
for(int i=;i<=n;i++)
Q.push_back(i);
for(int i=;i<=m;i++)
{
int x,y;scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
H[make_pair(x,y)]=;
}
int tot = ;
while()
{
tot++;
random_shuffle(Q.begin(),Q.end());
int flag = ;
for(int i=;i<m;i++)
{
int k = Q[i],p = Q[(i+)%n];
if(k==p)
{
flag=;
break;
}
if(k>p)
swap(k,p);
if(H[make_pair(k,p)])
{
flag=;
break;
}
}
if(flag==)
break;
if(tot==)
{
printf("-1\n");
return ;
}
}
for(int i=;i<m;i++)
printf("%d %d\n",Q[(i+)%n],Q[i]); }

Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化的更多相关文章

  1. Codeforces Round #192 (Div. 1) B. Biridian Forest 暴力bfs

    B. Biridian Forest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/pr ...

  2. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  3. [Codeforces Round #192 (Div. 2)] D. Biridian Forest

    D. Biridian Forest time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #550 (Div. 3) F. Graph Without Long Directed Paths

            F. Graph Without Long Directed Paths time limit per test 2 seconds memory limit per test 256 ...

  5. Codeforces Round #192 (Div. 2) (330B) B.Road Construction

    题意: 要在N个城市之间修建道路,使得任意两个城市都可以到达,而且不超过两条路,还有,有些城市之间是不能修建道路的. 思路: 要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目 ...

  6. Codeforces Round #192 (Div. 2) (330A) A. Cakeminator

    题意: 如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕. //cf 192 div2 #include <stdio.h> #include & ...

  7. Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)

    题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...

  8. Codeforces Round #192 (Div. 2)

    吐槽一下,这次的CF好简单啊. 可是我为什么这么粗心这么大意这么弱.把心沉下来,想想你到底想做什么! A 题意:O(-1) 思路:O(-1) #include <iostream> #in ...

  9. Codeforces Round #192 (Div. 2) A. Cakeminator

    #include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...

随机推荐

  1. ActionBarSherlock的学习笔记(三) ------------ ActionBarSherlock中的overflow及item的点击事件

    定义一个自定义的ActionBar的title,并添加一个overflow的Action   Item. 代码实现 如下  : import android.os.Bundle; import and ...

  2. SharePoint 2010中列表Add和Delete事件解析

    转:http://winsystem.ctocio.com.cn/26/11400026_2.shtml [IT专家网独家撰稿]SharePoint 2010与以前的版本相比,天翻地覆的变化并不为过. ...

  3. android.view.ViewRootImpl$CalledFromWrongThreadException错误处理

    一般情况下,我们在编写android代码的时候,我们会将一些耗时的操作,比如网络访问.磁盘访问放到一个子线程中来执行.而这类操作往往伴随着UI的更新操作.比如说,访问网络加载一张图片 new Thre ...

  4. hisi平台mii网络模式和rmii网络模式的uboot制作

    MII网络uboot编译说明 一:编译生成默认的uboot1. 进入到uboot目录a. cd /home/satan/Hi3518_SDK_V1.0.7.0/osdrv/uboot2. 新建临时文件 ...

  5. mac下SSH很快被断开

    解决方法: 1. 切换到root账号:sudo bash -c 'su - root' 2. 修改/etc/ssh_config文件 ServerAliveCountMax 5 ServerAlive ...

  6. redis 和 bloom filter

    今天打算使用redis 的bitset搞一个 bloom filter, 这样的好处是可以节省内存,坏处是可能在会有一些数据因为提示重复而无法保存. bloom filter 的大体原理就是通过不同的 ...

  7. webdriver(python) 学习笔记三

    知识点:简单的对象定位 对象的定位应该是自动化测试的核心,要想操作一个对象,首先应该识别这个对象.一个对象就是一个人一样,他会有各种的特征(属性),如比我们可以通过一个人的身份证号,姓名,或者他住在哪 ...

  8. 通过实例让你真正明白mapreduce---填空式、分布(分割)编程

    本文链接:http://www.aboutyun.com/thread-8303-1-1.html 问题导读: 1.如何在讲mapreduce函数中的字符串等信息,输出到eclipse控制台?2.除了 ...

  9. Struts2运行流程分析

    一.Struts2运行流程图: 二.运行流程分析: 1. 请求发送给StrutsPrepareAndExecuteFilter 2.StrutsPrepareAndExecuteFilter询问Act ...

  10. 安装Sublime Text 2插件的方法

    1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Packages目录(菜单->preferences->packages). 2.使用Package C ...