B - Relatively Prime Graph -CodeForces - 1009D-csdn博客

B - Relatively Prime Graph -CodeForces - 1009D-csdn博客

Let’s call an undirected graph G=(V,E)relatively prime if and only if for each edge (v,u)∈E GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v,u) doesn’t matter. The vertices are numbered from 11 to |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.

Input
The only line contains two integers nn and mm (1≤n,m≤10-5) — the number of vertices and the number of edges.

Output
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) of the resulting graph (1≤vi,ui≤n,vi≠ui). For each pair (v,u) there can be no more pairs (v,u)or (u,v). The vertices are numbered from 11 to n.

If there are multiple answers then print any of them.

Examples
Input
5 6
Output
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input
6 12
Output
Impossible
Note
Here is the representation of the graph from the first example:
这里写图片描述

  • 注意,因为m最大为1e5,因此直接暴力
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define _ ios::sync_with_stdio(false)
#define mk make_pair
bool SUBMIT = 1;
typedef long long ll;
using namespace std;
const double PI = acos(-1);
const int inf = 1e5+50;
int n,m,k=0;
struct node{
	int u,v;
	node(int u,int v):u(u),v(v)
	{}
};
vector<node>ans;
int main()
{
	//if(!SUBMIT)freopen("i.txt","r",stdin);else _;
	cin>>n>>m;
	for(int i=1;i<n&&k<m;i++)
		for(int j=i+1;j<=n&&k<m;j++)
			if(__gcd(i,j)==1){
				ans.pb(node(i,j));k++;
			}
	if(m<n-1||k<m){printf("Impossible\n");return 0;}
	printf("Possible\n");
	for(int i=0;i<ans.size();i++)
	{
		cout<<ans[i].u<<" "<<ans[i].v<<endl;
	}
	return 0;
}
posted @ 2018-07-23 13:23 i-Curve 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值