Elegant Construction

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5813

Description

Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, biology, and even musicology as background. And now in this problem, you are being a city architect!

A city with N towns (numbered 1 through N) is under construction. You, the architect, are being responsible for designing how these towns are connected by one-way roads. Each road connects two towns, and passengers can travel through in one direction.

For business purpose, the connectivity between towns has some requirements. You are given N non-negative integers a1 .. aN. For 1 <= i <= N, passenger start from town i, should be able to reach exactly ai towns (directly or indirectly, not include i itself). To prevent confusion on the trip, every road should be different, and cycles (one can travel through several roads and back to the starting point) should not exist.

Your task is constructing such a city. Now it's your showtime!

Input

The first line is an integer T (T <= 10), indicating the number of test case. Each test case begins with an integer N (1 <= N <= 1000), indicating the number of towns. Then N numbers in a line, the ith number ai (0 <= ai < N) has been described above.

Output

For each test case, output "Case #X: Y" in a line (without quotes), where X is the case number starting from 1, and Y is "Yes" if you can construct successfully or "No" if it's impossible to reach the requirements.

If Y is "Yes", output an integer M in a line, indicating the number of roads. Then M lines follow, each line contains two integers u and v (1 <= u, v <= N), separated with one single space, indicating a road direct from town u to town v. If there are multiple possible solutions, print any of them.

Sample Input

3

3

2 1 0

2

1 1

4

3 1 1 0

Sample Output

Case #1: Yes

2

1 2

2 3

Case #2: No

Case #3: Yes

4

1 2

1 3

2 4

3 4

Hint

题意

给你n个城市,告诉你第i个城市恰好能够走到a[i]个城市,让你构造一个有向图,使得满足题意,且不存在环。

题解:

直接暴力去建图就好了,n^2扫一遍,然后只扫编号比自己小的,这样就不会存在环了。

代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct node{
int a,id;
}p[1005];
bool cmp(node a,node b){
return a.a<b.a;
}
int cas = 0;
int ansx[maxn*maxn],ansy[maxn*maxn];
void solve(){
printf("Case #%d: ",++cas);
int sum = 0;
int cnt=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&p[i].a),p[i].id=i;
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++){
if(p[i].a>=i){
printf("No\n");
return;
}
for(int j=1;j<=p[i].a;j++)
ansx[cnt]=p[i].id,ansy[cnt++]=p[j].id;
}
printf("Yes\n");
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
printf("%d %d\n",ansx[i],ansy[i]);
}
}
int main(){
//freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)solve();
}

HDU 5813 Elegant Construction 构造的更多相关文章

  1. HDU 5813 Elegant Construction(优雅建造)

    HDU 5813 Elegant Construction(优雅建造) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65 ...

  2. HDU 5813 Elegant Construction (贪心)

    Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...

  3. HDU 5813 Elegant Construction ——(拓扑排序,构造)

    可以直接见这个博客:http://blog.csdn.net/black_miracle/article/details/52164974. 对其中的几点作一些解释: 1.这个方法我们对队列中取出的元 ...

  4. HDU 5813 Elegant Construction

    构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024 ...

  5. HDU5813 Elegant Construction

    Elegant Construction                                                                         Time Li ...

  6. hdu-5813 Elegant Construction(贪心)

    题目链接: Elegant Construction Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K (J ...

  7. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  8. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  9. P3599 Koishi Loves Construction——构造题

    题目 Task1:试判断能否构造并构造一个长度 $n$ 的 $1...n$ 的排列,满足其 $n$ 个前缀和在模 $n$ 的意义下互不相同 Task2:试判断能否构造并构造一个长度 $n$ 的 $1. ...

随机推荐

  1. 一个ssm综合小案例-商品订单管理-第一天

    项目需求分析: 功能需求:登录,商品列表查询,修改 项目环境及技术栈: 项目构成及环境: 本项目采用 maven 构建 环境要求: IDEA Version: 2017.2.5 Tomcat Vers ...

  2. github for Mac 教程

    Mac系统自带git,所有我们使用Mac搬的github客户端,无需安装git,所以使用github for Mac 超级简单,下载安装就好了. 1github for Mac 下载地址:https: ...

  3. 一主多从+Binlog Server,主库故障无法访问,如何在从库中选举一个新主库

    一.基本环境 VMware10.0+CentOS6.9+MySQL5.7.19 ROLE HOSTNAME BASEDIR DATADIR IP PORT M ZST1 /usr/local/mysq ...

  4. 公共语言运行库(CLR)开发系列课程(1):Pinvoke 简介 学习笔记

    前言 让拖管代码对象和非托管对象协同工作的过程称为互用性(Interoperability),通常简称为 Interop. P/Invoke在托管代码与非托管代码交互式时产生一个事务(Transiti ...

  5. dblinks

    一.Oracle数据库链Database links的作用 当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本 ...

  6. zabbix3.0.4安装趋势图集中显示插件graphtrees

    通过yum方式安装的zabbix 1.将/usr/share/zabbix目录修改权限,因此处我们使用的是apache,所以用户改为apache,如果是nginx需要改为nginx(是否需要修改可以参 ...

  7. 【转】使用import scope解决maven继承(单)问题

    http://blog.csdn.net/mn960mn/article/details/50894022 测试环境 maven 3.3.9 想必大家在做SpringBoot应用的时候,都会有如下代码 ...

  8. vue项目使用vw单位适配移动端方法

    传送门:  https://blog.csdn.net/zjw0742/article/details/79337336

  9. 如何将同一个APP中的不同activity在Recent(最近任务)中显示?

    需求描述 在应用Application1中存在A.B两个activity,当在应用启动了A.B activity,点击Recent键,如何让A.B两个activity都显示在Recent界面(最近任务 ...

  10. 转:Vue-cli proxyTable 解决开发环境的跨域问题

    转:http://www.jianshu.com/p/95b2caf7e0da 和后端联调时总是会面对恼人的跨域问题,最近基于Vue开发项目时也遇到了这个问题,两边各自想了一堆办法,查了一堆资料,加了 ...