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. 【js】js方法中直接跳转到servlet

    function firm_back_state(id,name){ if(confirm("确认返单?")){ location.href="Edit_OrderSer ...

  2. innodb buffer pool

    add page to flush list buffer pool中的page,有三种状态: l  free:      当前page未被使用 l  clean:    当前page被使用,对应于数 ...

  3. java解析XML四种方法

    XML现在已经成为一种通用的数据交换格式,平台的无关性使得很多场合都需要用到XML. XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便 ...

  4. Gridview LookupEdit gridLookUpEdit z

    LookupEdit一般用法: 绑定数据源:                                 lookUpEdit.Properties.ValueMember = 实际要用的字段;  ...

  5. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  6. ubuntu 下 数学库编译链接时找不到各种数学问题解决方法 can not fon atan 等等

    解决参考 http://askubuntu.com/questions/190246/ld-cannot-find-math-library you should use -lm at the end ...

  7. 【windows核心编程】DLL相关(1)

    DLL相关的东西 1.DLL的加载方式 隐式: #pragma comment(lib, "XX.lib"); 编译器去查找名为XX.dll的DLL,除了名字相同,该DLL和该LI ...

  8. CSS定位(CSS定位概述、相对定位、绝对定位、浮动)

    CSS 定位属性 CSS 定位属性允许你对元素进行定位. 属性 描述 position 把元素放置到一个静态的.相对的.绝对的.或固定的位置中. top 定义了一个定位元素的上外边距边界与其包含块上边 ...

  9. [Hive - LanguageManual] Create/Drop/Grant/Revoke Roles and Privileges / Show Use

    Create/Drop/Grant/Revoke Roles and Privileges Hive Default Authorization - Legacy Mode has informati ...

  10. 《Genesis-3D开源游戏引擎-官方录制系列视频教程:进阶实例篇》

    注:本系列教程仅针对引擎编辑器:v1.2.2及以下版本 G3D进阶实例   第四课<2D编辑与脚本的统一入口> 使用G3D完成一个简单的类飞机大战游戏,介绍了G3D2d游戏制作的流程包括: ...