日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Codeforces 1009D:Relatively Prime Graph

發布時間:2025/6/15 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 1009D:Relatively Prime Graph 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

D. Relatively Prime Graphtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Input

The only line contains two integers?nn?and?mm?(1n,m1051≤n,m≤105) — 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)(vi,ui)?of the resulting graph (1vi,uin,viui1≤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.

ExamplesinputCopy5 6 outputCopyPossible 2 5 3 2 5 1 3 4 4 1 5 4 inputCopy6 12 outputCopyImpossible Note

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; }

轉載于:https://www.cnblogs.com/Friends-A/p/10324457.html

總結

以上是生活随笔為你收集整理的Codeforces 1009D:Relatively Prime Graph的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。