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 > ...
 
随机推荐
- date.plugin.js 日期插件
			
//定义命名空间 var DatePlugin; if (!DatePlugin) DatePlugin = {}; /*整理时间:2015-05-28*/ var defaultFormat = & ...
 - oracle-Oracle试题
			
ylbtech-doc:oracle-Oracle试题 oracle-Oracle试题 1.A,返回顶部 01.{Oracle题目}你判断下面语句,有什么作用?(选择1项) GRANT upd ...
 - How to easily create popup menu for DevExpress treelist z
			
http://www.itjungles.com/how-to-easily-create-popup-menu-for-devexpress-treelist.html Adding popup m ...
 - 七中滤波方法测试matlab实现
			
http://blog.163.com/xiaheng0804@126/blog/static/1205282120132129471816/ 创建两个混合信号,便于更好测试滤波器效果.同时用七中滤波 ...
 - dell optiplex台式机 安装win7 清楚分区的方法
			
http://jingyan.baidu.com/article/92255446e1065f851648f42b.html
 - C++指针的引用
			
[1]指针的引用,必须加上头文件<iomanip>因为调用类setw() 对一个数据可以使用“引用”(reference)这是C++ 对C的一个重要扩充,引用是一种新的 ...
 - 通过gdb调试分析Linux内核的启动过程
			
作者:吴乐 山东师范大学 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验流程 1.打开环境 执 ...
 - ASP.NET MVC中使用ASP.NET AJAX异步访问WebService
			
使用过ASP.NET AJAX的朋友都知道,怎么通过ASP.NET AJAX在客户端访问WebService,其实在ASP.NET MVC中使用ASP.NET AJAX异步访问WebService 也 ...
 - CentOS 7 最小化安装之后安装Mysql
			
启动网卡 验证网络管理器和网卡接口状态 # systemctl status NetworkManager.service # nmcli dev status 修改网卡配置文件 通过上一步可以看到有 ...
 - RStudio Keyboard Shortcuts
			
Console Description Windows & Linux Mac Move cursor to Console Ctrl+2 Ctrl+2 Clear console Ctrl+ ...