【链接】 我是链接,点我呀:)

【题意】

每个节点的度数不超过k
让你重构一个图
使得这个图满足 从某个点开始到其他点的最短路满足输入的要求

【题解】

把点按照dep的值分类
显然只能由dep到dep+1连边
设cnt[dep]表示到起点的距离为dep的点的集合
如果cnt[dep].size>cnt[dep+1].size
那么只要把dep层的前cnt[dep+1].size个点和dep+1层的点连就好了
否则
只能让dep层的点每个多连几个dep+1层的点了

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = (int)1e5;
static class Task{ class Pair{
int x,y;
public Pair(int x,int y) {
this.x = x;
this.y = y;
}
} int n,k;
ArrayList<Integer> g[] = new ArrayList[N+10];
ArrayList<Pair> ans = new ArrayList<>(); public void solve(InputReader in,PrintWriter out) {
for (int i = 0;i <= N;i++) g[i]=new ArrayList<Integer>();
n = in.nextInt();k = in.nextInt();
for (int i = 1;i <= n;i++) {
int d;
d = in.nextInt();
g[d].add(i);
}
if ((int)g[0].size()>1) {
out.println(-1);
}else {
for (int i = 1;i <= n;i++)
if ((int)g[i].size()>0 && g[i-1].isEmpty()) {
out.println(-1);
return;
}
for (int i = 1;i <= n;i++) {
if (g[i].isEmpty()) break;
int x = g[i].size();
int y = g[i-1].size();
//out.println(x+" "+y);
if (y>=x) {
for (int j = 0;j < x;j++) {
ans.add(new Pair(g[i-1].get(j),g[i].get(j)));
}
int ma = 1;
if (i-1>=1) {
ma = 2;
}
if (ma>k) {
out.println(-1);
return;
}
}else {
//y<x
int need = x/y;
if (x%y!=0) need++;
int ma = need;
//out.println(ma);
if (i-1>=1) ma++;
if (ma>k) {
out.println(-1);
return;
}
for (int j = 0;j < x;j++) {
int now = j;
now = now % y;
ans.add(new Pair(g[i-1].get(now),g[i].get(j)));
}
}
}
out.println((int)ans.size());
for (int i = 0;i < ans.size();i++) {
out.println(ans.get(i).x+" "+ans.get(i).y);
}
}
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

最新文章

  1. Python Pandas分组聚合
  2. Jquery动态添加的元素绑定事件的3种方法
  3. android Activity生命周期(设备旋转、数据恢复等)与启动模式
  4. Android-Activity使用(2) -传值
  5. Android中Listview实现分页加载效果OnScrollListener
  6. android中的TextView控件
  7. 020ARM家族
  8. DB天气app冲刺二阶段第六天
  9. 开发日志_Jan.9
  10. Aix db2 经user a using b连接时报SQL30082N Security processing failed with reason &amp;quot;42&amp;quot;
  11. 解决mysql启动时报The server quit without updating PID file 的错误(转)
  12. mongoose简单使用样例
  13. Android P添加一个可以让system_server进程访问的hal service需要改动的sepolicy文件
  14. Python中的可变、不可变对象和赋值技巧序列解包
  15. 【转】curl命令总结,Http Post_Get 常用
  16. HttpClient(五)-- 模拟表单上传文件
  17. 原生js--鼠标事件
  18. css学习之LInk &amp; import
  19. hdu1754 I Hate It(线段树单点更新,区间查询)
  20. mybatis三剑客之mybatis-pagehelper分页插件

热门文章

  1. jquery对所有&lt;input type=&quot;text&quot;的控件赋值
  2. Objective-C程序
  3. 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)
  4. js angular 时间戳转换成日期格式 年月日 yyyy-MM-dd
  5. VBNET AutoCAD Activex 切换图层为当前图层失效
  6. 树形DP Gym 100496H House of Representatives
  7. Visual C++ Windows 桌面应用程序样例(摘抄)
  8. 在struct 中使用string,赋值会报错
  9. Font Awesome 图标使用总结
  10. 60使用nanopim1plus查看HDMI显示分辨率的问题(分色排版)V1.0