题目链接:点击打开链接
题目大意:给定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;
}