zoukankan      html  css  js  c++  java
  • [CF1082D]Maximum Diameter Graph

    题目描述 Description###

    Graph constructive problems are back! This time the graph you are asked to build should match the following properties.
    The graph is connected if and only if there exists a path between every pair of vertices.
    The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices.
    The degree of a vertex is the number of edges incident to it.
    Given a sequence of n integers (a_1,a_2,…,a_n) construct a connected undirected graph of n vertices such that:

    the graph contains no self-loops and no multiple edges;
    the degree di of the (i) -th vertex doesn't exceed ai (i.e. (d_i ≤a_i) );
    the diameter of the graph is maximum possible.
    Output the resulting graph or report that no solution exists.

    输入描述 Input Description###

    The first line contains a single integer n (3≤n≤500) — the number of vertices in the graph.

    The second line contains n integers $a_1,a_2,…,a_n $ ((1≤a_i≤n−1) ) — the upper limits to vertex degrees.

    输出描述 Output Description###

    Print "NO" if no graph can be constructed under the given conditions.

    Otherwise print "YES" and the diameter of the resulting graph in the first line.

    The second line should contain a single integer m — the number of edges in the resulting graph.

    The i-th of the next m lines should contain two integers (v_i,u_i) ((1≤v_i,u_i≤n, v_i≠u_i) ) — the description of the i-th edge. The graph should contain no multiple edges — for each pair (x,y) you output, you should output no more pairs (x,y) or (y,x).

    样例输入 Sample Input###

    5
    1 4 1 1 1
    

    样例输出 Sample Output###

    YES 2
    4
    1 2
    3 2
    4 2
    5 2
    

    数据范围及提示 Data Size & Hint###

    见上面

    之前的一些废话###

    明天考高代,所以这个比赛根本没人参加,我也是就把这道题首刀了之后就去复习了

    题解###

    为了使我们的直径最长,我们通过画图发现直径两端的点度数为1,所以我们需要把度数最小的两个点放到直径两端,然后剩下的所有度数大于1的点都可以充当直径上的点,(可以证明这些点不放在直径上都是浪费),然后剩下的那些度数为1的点就用来充当直径上的分岔就好了。
    最后一定是一棵树。

    代码###

    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<queue>
    using namespace std;
    #define mem(a,b) memset(a,b,sizeof(a))
    typedef long long LL;
    typedef pair<int,int> PII;
    inline int read()
    {
    	int x=0,f=1;char c=getchar();
    	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
    	while(isdigit(c)){x=x*10+c-'0';c=getchar();}
    	return x*f;
    }
    bool e[510][510];
    PII a[510];
    int n,first,last,list[510],dm,val[510];
    bool ok;
    void add(int A,int B){e[A][B]=e[B][A]=1;}//printf("add:%d %d
    ",A,B);}
    int main()
    {
    	n=read();
    	for(int i=1;i<=n;i++)a[i].first=read(),a[i].second=i;
    	sort(a+1,a+n+1);
    	first=a[1].second;last=a[2].second;
    	list[++dm]=first;val[dm]=a[1].first;
    	for(int i=3;i<=n;i++)
    		if(a[i].first>=2)list[++dm]=a[i].second,val[dm]=a[i].first,add(list[dm],list[dm-1]);
    	list[++dm]=last;val[dm]=a[2].first;add(list[dm],list[dm-1]);
    	int pos=3;
    	for(int i=2;i<=dm-1;i++)
    		for(int j=1;j<=val[i]-2;j++)
    		{
    			if(a[pos].first==1)add(list[i],a[pos].second),pos++;//printf("pos %d
    ",pos);
    			if(a[pos].first>1)break;
    		}
    	if(a[pos].first==1){
    		printf("NO
    ");
    		return 0;
    	}
    	int cnt=0;
    	printf("YES %d
    ",dm-1);
    	for(int i=1;i<=n;i++)
    		for(int j=i+1;j<=n;j++)if(e[i][j])cnt++;
    	printf("%d
    ",cnt);
    	for(int i=1;i<=n;i++)
    		for(int j=i+1;j<=n;j++)if(e[i][j])printf("%d %d
    ",i,j); 
    	return 0;
    }
    
    

    总结###

    虽然构造题做的少(基本没做过),但是这道题我觉得还是比较简单,多多画图,就知道咋做了。

  • 相关阅读:
    vmware vSphere client中,选择文件->部署OVF模板,报错处理方法
    [POJ 1521]--Entropy(哈夫曼树)
    [HDU 1016]--Prime Ring Problem(回溯)
    [HDU 2553]--N皇后问题(回溯)/N皇后问题的分析
    平面最近点对问题(分治)
    [GDUT 决赛]--GCD,LCM——我是好人(数论)
    [HDU 1428]--漫步校园(记忆化搜索)
    [Swust OJ 643]--行列式的计算(上三角行列式变换)
    [Swust OJ 491]--分数的位置(简单版)
    [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
  • 原文地址:https://www.cnblogs.com/FYH-SSGSS/p/11876234.html
Copyright © 2011-2022 走看看