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 ...
随机推荐
- 分布式存储之MogileFS分布式文件系统简单应用
一.分布式存储原理: 分布式存储系统,是将数据分散存储在多台独立的设备上.传统的网络存储系统采用集中的存储服务器存放所有数据,存储服务器成为系统性能的瓶颈,也是可靠性和安全性的焦点,不能满足大规模存储 ...
- cocos进阶教程(5)各种动画使用心得
Node类 不解释 ActionTimeline类是一个3.0时代的动画类, 案例一 //建立node方案一Data data = FileUtils::getInstance()->getDa ...
- appcmd创建站点、应用程序、虚拟目录批处理程序
创建站点(放置在站点下运行): @echo off cls :start echo start set /p sitename="sitename:" @set "phy ...
- vue-router的hash模式与history模式的对比
Vue-router 中hash模式和history模式的关系在vue的路由配置中有mode选项 最直观的区别就是在url中 hash 带了一个很丑的 # 而history是没有#的mode:&quo ...
- Bootstrap抽样(自展法)
Bootstrap又称自展法,是用小样本估计总体值的一种非参数方法,在进化和生态学研究中应用十分广泛.例如进化树分化节点的自展支持率等. Bootstrap的思想,是生成一系列bootstrap伪样本 ...
- LA 4287 有相图的强连通分量
大白书P322 , 一个有向图在添加至少的边使得整个图变成强连通图, 是计算整个图有a个点没有 入度, b 个点没有出度, 答案为 max(a,b) ; 至今不知所云.(求教) #include &l ...
- linux 常用命令总结(二)
1. linux下以指定的编码打开文件:LANG=zh_CN vi fileName 2. 查看系统内存使用,可以使用free -m 或 top 3. 使用env查看所有环境变量 4. df –h 查 ...
- 按月、按日进行数据统计的Mysql语句
<select id="getCustomerTJByUser" parameterType="map" resultType="map&quo ...
- Rabbitmq安装、集群与高可用配置
历史: RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多 ...
- 嵌入式 Linux 如何操作 GPIO ?
作者:刘凯链接:https://www.zhihu.com/question/19704852/answer/19760467来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...