codeforces 22 C. System Administrator

http://codeforces.com/contest/22/problem/C 题意:要求用n个点m条边构造一个不允许有重边的图,满足当去掉点v的时候,剩下的n-1个不联通。如果有答案输出任意,没答案输出-1. 思路:首先如果n个点要联通。。至少有n-1条边,此时为一棵树。但是是不是边越多越好呢?显然是不可以的。满足去掉一个点使得n-1个点不联通的情况为,存在一个点u只和v相连,不和任意任何其他点相连,那么当去掉v点,u点就变成不可到达了。边数最多的情况就是,除了v点以外的n-1个点,每个点的度都是n-2(去掉自身以及u点还有n-2个点),,那么除去u点以外的n-1个点的度数就是(n-1)(n-2),边数则为(n-1)(n-2)/2,再加一条连接u的边,所以图的最大边数为(n-1)*(n-2)/2+1,最小为n-1.

如果有解,那么接下来的问题是构造。

我是按照如下方式构造的:

先构造一条链,将u点放在第一个,v点放在第二个。不妨当v=1时令u=2,否则u=1;

m-=n-1,如果m还有剩余,那么从第二个点开始,一直到第n-2个点,每个点与至少隔1个点的其他点相连,直到边数没有剩余。

/* ***********************************************
Author :111qqz
Created Time :2015年12月30日 星期三 20时36分06秒
File Name :code/cf/problem/22C.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E5+7;
 7int n,m,v;
 8int a[N];
 9int main()
10{
11	#ifndef  ONLINE_JUDGE 
12	freopen("code/in.txt","r",stdin);
13  #endif
14	cin>>n>>m>>v;
15	if (m<n-1||m>((n-1)*(n-2)/2+1))
16	{
17	    puts("-1");
18	    return 0;
19	}
 1	for ( int i = 1 ; i <= n ; i++)
 2	{
 3	    a[i] = i ;
 4	    if (i==2)
 5	    {
 6		a[i]=v;
 7	    }
 8	    if (i==v)
 9	    {
10		a[i]=2;
11	    }
12	}
13	for ( int i = 1 ; i <= n-1 ; i++)
14	{
15	    printf("%d %d\n",a[i],a[i+1]);
16	}
17	m-=n-1;
 1	int cnt =  0 ;
 2	for ( int i = 2 ; i <= n-2 ; i ++)
 3	{
 4	    if (cnt>m) break;
 5	    for ( int j = i+2 ; j <= n ; j++)
 6	    {
 7		cnt++;
 8		if (cnt>m) break;
 9		printf("%d %d\n",a[i],a[j]);
10	    }
11	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}