HDU 5813 Elegant Construction(构造)

题目链接:点击打开链接

题目大意:给定n个点,和n个整数,n个整数代表标号为i的顶点要和其他第ni个点相连,输出一个合法地图。

题目解析:将顶点按能到达的点数从小到大排序,排好序之后每个点只能往前面的点连边. 因而如果存在一个排在第i位的点,要求到达的点数大于i-1,则不可行;否则就可以按照上述方法构造出图. 复杂度O(N^2).

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const double esp = 1e-6;
const double PI = 3.1415926535898;
const int INF = 0x3f3f3f3f;
using namespace std;

int t,n;
struct node{
    int x,y;
}arr[1005],ant;

bool cmp(node a,node b){
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}

int main(){
    int cas = 1;
    scanf("%d",&t);
    while(t-- && scanf("%d",&n)){
        for(int i=0;i<n;i++){
            scanf("%d",&arr[i].x);
            arr[i].y = i + 1;
        }
        sort(arr,arr + n,cmp);

        bool f = false;
        queue<node>Q;
        for(int i=0;i<n;i++){
            if(arr[i].x > i)
                f = true;
            for(int j=0;j<arr[i].x;j++){
                ant.x = arr[i].y;
                ant.y = arr[j].y;
                Q.push(ant);
            }
        }

        printf("Case #%d: %s\n",cas++,f?"No":"Yes");
        if(!f){
            printf("%d\n",Q.size());
            while(!Q.empty()){
                ant = Q.front();Q.pop();
                printf("%d %d\n",ant.x,ant.y);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值