Codeforces Round #192 (Div. 1) C. Graph Reconstruction 随机化
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.
⋅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 ≤ n; u ≠ 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 随机化的更多相关文章
- 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 ...
- Codeforces Round #192 (Div. 1) A. Purification 贪心
A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...
- [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 ...
- 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 ...
- Codeforces Round #192 (Div. 2) (330B) B.Road Construction
题意: 要在N个城市之间修建道路,使得任意两个城市都可以到达,而且不超过两条路,还有,有些城市之间是不能修建道路的. 思路: 要将N个城市全部相连,刚开始以为是最小生成树的问题,其实就是一道简单的题目 ...
- Codeforces Round #192 (Div. 2) (330A) A. Cakeminator
题意: 如果某一行没有草莓,就可以吃掉这一行,某一列没有也可以吃点这一列,求最多会被吃掉多少块蛋糕. //cf 192 div2 #include <stdio.h> #include & ...
- Codeforces Round #509 (Div. 2) E. Tree Reconstruction(构造)
题目链接:http://codeforces.com/contest/1041/problem/E 题意:给出n - 1对pair,构造一颗树,使得断开其中一条边,树两边的最大值为 a 和 b . 题 ...
- Codeforces Round #192 (Div. 2)
吐槽一下,这次的CF好简单啊. 可是我为什么这么粗心这么大意这么弱.把心沉下来,想想你到底想做什么! A 题意:O(-1) 思路:O(-1) #include <iostream> #in ...
- Codeforces Round #192 (Div. 2) A. Cakeminator
#include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...
随机推荐
- 【转】Android Studio系列教程一--下载与安装
原文网址:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1/ 背景 相信大家对Android Studio已经不陌生 ...
- 也谈http中get和post
1.get和post区别: 从设计初衷考虑get是为了查询服务器资源(不改变服务器数据及状态,因此说它是安全和幂等的,但get请求参数一般是直接在url后面,浏览器地址栏中会被看到能保存书签及历史记录 ...
- (function(){})()这个是什么?有不明觉厉的感觉么?
今天在RunJs上看到一个人分享的一个jquery代码,写的是jquery弹性滑动效果.不过,看着看着,发现一句代码(function{})(),突然有种不明觉厉的感觉. 事实上,只是因为我们没有用过 ...
- T-SQL查询进阶-10分钟理解游标
转:http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的 ...
- AFNetworking教程
转:http://www.lanrenios.com/tutorials/network/2012/1126/527.html AFNETWORKING AFNetworking他是一个现在非常用得多 ...
- [原创]个人工具 - YE快速复制助手(YeFastcopyHelper)
版本:v1.3.216 更新时间:2014/02/16 * 代码完善 + 右键关于显示当前版本号,点击并链接到软件帮助页 Technorati 标签: NET,.NET 3.5,asion C#,Ch ...
- python easy_install centos 下安装过程和原理解析
一.easy_install 安装过程 其安装过程有很多种,我也找了很多的例子,但是结果都不太好,以下方法的结果是不错的. easy_install与yum类似,使用easy_install,可以轻松 ...
- Cocos2d-x使用iOS游戏内付费IAP(C++篇)
本文章转载 http://www.ityran.com/archives/5515.非本人原创! 前期准备 设备与账号 在开始编码之前我们需要准备测试环境. IAP只能真机测试,准备一台iOS设备是必 ...
- 使用rpmbuild来创建自己的RPM
1. 进行创建必须的目录 在6.2的版本中,路径发生了变化,必须在此路径中,否则必须要修改配置文件. 2. 创建脚本文件 编写一个简单的脚本,然后将脚本进行打包为tar.gz格式的压缩文件,并且将其放 ...
- 3669 [Noi2014]魔法森林(LCT,最小生成树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3669 [题意] 给定一个无向图,求1-n的路径中最小的max{ai}+max{bi} ...