Codeforces 1009D: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(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.
InputThe only line contains two integers?nn?and?mm?(1≤n,m≤1051≤n,m≤105) — the number of vertices and the number of edges.
OutputIf 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.
ExamplesinputCopy5 6 outputCopyPossible 2 5 3 2 5 1 3 4 4 1 5 4 inputCopy6 12 outputCopyImpossible NoteHere 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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在linux系统下把多个终端合并在一个窗
- 下一篇: Java 基础 之 关系运算符