Codeforces 1009D:Relatively Prime Graph
2 seconds
256 megabytes
standard input
standard output
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E(v,u)∈E GCD(v,u)=1GCD(v,u)=1 (the greatest common divisor of vv and uu is 11). If there is no edge between some pair of vertices vv and uu then the value of GCD(v,u)GCD(v,u) doesn't matter. The vertices are numbered from 11 to |V||V|.
Construct a relatively prime graph with nn vertices and mm edges such that it is connected and it contains neither self-loops nor multiple edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
If there are multiple answers then print any of them.
The only line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) — the number of vertices and the number of edges.
If there exists no valid graph with the given number of vertices and edges then output "Impossible".
Otherwise print the answer in the following format:
The first line should contain the word "Possible".
The ii-th of the next mm lines should contain the ii-th edge (vi,ui)(vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui1≤vi,ui≤n,vi≠ui). For each pair (v,u)(v,u)there can be no more pairs (v,u)(v,u) or (u,v)(u,v). The vertices are numbered from 11 to nn.
If there are multiple answers then print any of them.
5 6
Possible
2 5
3 2
5 1
3 4
4 1
5 4
6 12
Impossible
Here is the representation of the graph from the first example:
题意:有n个点,编号为1~n。有m条边,要求每条边的顶点的最大公约数为1(并且没有平行边和环),如果这些点和边能组成无向的连通图,并且边和顶点都没有剩余,则输出Possible,并输出可能的边(用顶点表示),否则输出Impossible
AC代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e6+10;
using namespace std;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll vis[maxn][2];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
ll n,m;
cin>>n>>m;
ms(vis);
if(m<n-1)//只有两个点,一条边
cout<<"Impossible"<<endl;
else
{
ll k=0;
for(ll i=1;i<n;i++)
for(ll j=i+1;j<=n;j++)
{
if(gcd(i,j)==1)//i和j的最大公约数为1,说明两点可以相连
{
vis[k][0]=i;
vis[k][1]=j;
k++;//i,j看做边的两点,并更新k的值
if(k>m)
break;//这个停止不能少!!!要不然数太多,数组存不下!!
}
}
if(k<m)
cout<<"Impossible"<<endl;
else
{
cout<<"Possible"<<endl;
for(int i=0;i<m;i++)
{
cout<<vis[i][0]<<" "<<vis[i][1]<<endl;
}
}
}
return 0;
}
Codeforces 1009D:Relatively Prime Graph的更多相关文章
- 【Codeforces 1009D】Relatively Prime Graph
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 1000以内就有非常多组互质的数了(超过1e5) 所以,直接暴力就行...很快就找完了 (另外一开始头n-1条边找1和2,3...n就好 [代 ...
- Codeforces Global Round 4 Prime Graph CodeForces - 1178D (构造,结论)
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wan ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- 译:Local Spectral Graph Convolution for Point Set Feature Learning-用于点集特征学习的局部谱图卷积
标题:Local Spectral Graph Convolution for Point Set Feature Learning 作者:Chu Wang, Babak Samari, Kaleem ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- 算法:图(Graph)的遍历、最小生成树和拓扑排序
背景 不同的数据结构有不同的用途,像:数组.链表.队列.栈多数是用来做为基本的工具使用,二叉树多用来作为已排序元素列表的存储,B 树用在存储中,本文介绍的 Graph 多数是为了解决现实问题(说到底, ...
- D. Relatively Prime Graph
Let's call an undirected graph G=(V,E)G=(V,E) relatively prime if and only if for each edge (v,u)∈E( ...
- Relatively Prime Graph CF1009D 暴力 思维
Relatively Prime Graph time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- cocos代码研究(16)Widget子类RadioButton学习笔记
理论基础 RadioButton是一种特定类型的两状态按钮,它与复选框相似.它可以 和RadioButtonGroup一起使用,形成一个"组".继承自AbstractCheckBu ...
- devise 自定义手机号登录
user model中配置 validates_uniqueness_of :phone def email_required? false end 修改user migration文件,给phone ...
- [ 翻译]ruby rails相关的常见服务器
原文:http://stackoverflow.com/questions/4113299/ruby-on-rails-server-options 一,Apache vs Nginx ...
- A Practical Guide to Support Vector Classication
<A Practical Guide to Support Vector Classication>是一篇libSVM使用入门教程以及一些实用技巧. 1. Basic Kernels: ( ...
- postman--实现接口自动化测试
postman使用 开发中经常用postman来测试接口,一个简单的注册接口用postman测试: 接口正常工作只是最基本的要求,经常要评估接口性能,进行压力测试. postman进行简单压力测试 下 ...
- 20145122 《Java程序设计》第二周学习总结
20145122 <Java程序设计>第2周学习总结 教材学习内容总结 在大一的时候我们学习了C语言,所以对第三章的知识不是很陌生,但有些新知识需要记忆. java常用的数据类型: byt ...
- 20145319 《网络渗透》MS11-050漏洞渗透
20145319 <网络渗透>MS11-050漏洞渗透 一 实验内容 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 了解MS11_050相关知识: - 安全公告:KB ...
- JAVA_返回一个数值的相反数的几种方式.
一个方法接收一个int类型值,需要返回它的相反数. 如传入1,返回-1 传入-22,返回22 最简单的方式是return 0-number; 还有其他方式: public class Kata { p ...
- BZOJ 1853: [Scoi2010]幸运数字(容斥原理)
http://www.lydsy.com/JudgeOnline/problem.php?id=1853 题意: 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运 ...
- python 将一个列表乱序
import random nums = [, , , , , , ] random.shuffle(nums) print(nums)