cf Educational Codeforces Round 47 D. Relatively Prime Graph

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tengfei461807914/article/details/82191118

原题:
D. Relatively Prime Graph
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 1 to
|V|.
Construct a relatively prime graph with n vertices and m 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 n and m (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
i-th of the next m lines should contain the i-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 1 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:
这里写图片描述

中文:

给你两个数,n和m,让你构造一个m条边n个定点的无向图,要求连接的两个顶点编号互质,而且图连通,不许有边连到定点本身,不许有重边。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

ll n,m;


int gcd(int a,int b)
{
    if(a%b==0)
        return b;
    return gcd(b,a%b);
}

vector<pii> vp;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m)
    {
        vp.clear();

        if(m>(n*(n-1))/2||m<n-1)
        {
            cout<<"Impossible"<<endl;
            continue;
        }
        ll cnt=m;
        if(m<n)
        {

            cout<<"Possible"<<endl;
            for(int i=2;i<=n&&cnt;i++,cnt--)
                cout<<1<<" "<<i<<endl;
            continue;
        }
        else
        {
            cnt-=(n-1);
            for(int i=2;i<=n;i++)
                vp.push_back(make_pair(1,i));
        }

        if(cnt>0)
        {
            for(int i=2;i<=n;i++)
            {
                if(i%2==0)
                {
                    for(int j=i+1;j<=n;j+=2)
                    {
                        if(gcd(i,j)==1)
                        {
                            vp.push_back(make_pair(i,j));
                            cnt--;
                        }
                        if(cnt==0)
                            break;
                    }
                    if(cnt==0)
                        break;
                }
                else
                {
                    for(int j=i+1;j<=n;j++)
                    {
                        if(gcd(i,j)==1)
                        {
                            vp.push_back(make_pair(i,j));
                            cnt--;
                        }
                        if(cnt==0)
                            break;
                    }
                    if(cnt==0)
                        break;
                }
            }
        }
        if(cnt>0)
        {
            cout<<"Impossible"<<endl;
        }
        else
        {
            cout<<"Possible"<<endl;
            for(int i=0;i<vp.size();i++)
                cout<<vp[i].first<<" "<<vp[i].second<<endl;
        }

    }

    return 0;
}

思路:

首先,如果边的数量m小于顶点数量n-1,或者m的数量大于完全图边的数量,那可定不可以。

否则,可以先将定点1,与剩余顶点全部相连,这样保证了是一个连通图。

然后,暴力枚举,判断两个定点是否互质即可。

猜你喜欢

转载自blog.csdn.net/tengfei461807914/article/details/82191118