What's the problem in the following code? It gets wrong answer on test case 3.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n,m,v,i;
scanf("%d %d %d",&n,&m,&v);
if(m<n-1)
{
printf("-1");
return 0;
}
if((m-n+1)>n-3)
{
printf("-1");
return 0;
}
for(i=1;i<n;i++)
printf("%d %d\n",i,i+1);
int count=0;
for(i=1;count<m-n+1;i++)
{
if(i!=v-1 && i!=v && i!=v+1)
{
//printf("%d %d\n",i,v);
printf("%d %d\n",min(i,v),max(i,v));
count++;
}
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n,m,v,i;
scanf("%d %d %d",&n,&m,&v);
if(m<n-1)
{
printf("-1");
return 0;
}
if((m-n+1)>n-3)
{
printf("-1");
return 0;
}
for(i=1;i<n;i++)
printf("%d %d\n",i,i+1);
int count=0;
for(i=1;count<m-n+1;i++)
{
if(i!=v-1 && i!=v && i!=v+1)
{
//printf("%d %d\n",i,v);
printf("%d %d\n",min(i,v),max(i,v));
count++;
}
}
return 0;
}
{
printf("-1");
return 0;
}
This part is incorrect.
M can be much larger.
Then, you add one more edge from v to the vertex you left out of your complete graph.
So in total, (n-1)(n-2)/2 + 1 edges is the most you may be able to use.