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

每个节点的度数不超过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 herenew 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<xint 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());}}
}

转载于:https://www.cnblogs.com/AWCXV/p/10562232.html

【Codeforces 404C】Restore Graph相关推荐

  1. 【CodeForces - 246D】Colorful Graph (暴力,图,存边,STL)

    题干: You've got an undirected graph, consisting of n vertices and m edges. We will consider the graph ...

  2. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

    题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...

  3. 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

    题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...

  4. 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

    题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...

  5. 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)

    题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...

  6. 【论文阅读】Spatio-Temporal Graph Convolutional Networks:...Traffic Forecasting[时空图卷积网络:用于交通预测的深度学习框架](1)

    [论文阅读]Spatio-Temporal Graph Convolutional Networks: A Deep Learning Framework for Traffic Forecastin ...

  7. 【论文阅读】Attributed Graph Clustering with Dual Redundancy Reduction(AGC-DRR)

    [论文阅读]Attributed Graph Clustering with Dual Redundancy Reduction(AGC-DRR) 文章目录 [论文阅读]Attributed Grap ...

  8. 【代码复现】AGC-DRR__Attributed Graph Clustering with Dual Redundancy Reduction(IJCAI-2022)

    [代码复现]AGC-DRR__Attributed Graph Clustering with Dual Redundancy Reduction(IJCAI-2022) 文章目录 [代码复现]AGC ...

  9. 【CodeForces 997C】Sky Full of Stars(组合计数)

    题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...

  10. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

最新文章

  1. iOS 13 如何删除SceneDelegate
  2. error:“ACCESS_MASK”: 不明确的符号
  3. Spring.NET性能
  4. 101个MySQL的调节和优化的Tips
  5. Openwrt配置ssh自动登录服务器socks5转发
  6. 小米盒子老是服务器无响应,教你解决小米盒子黑屏死机等故障解决办法!
  7. MATLAB库函数resample(重新采样序列)的C语言实现
  8. SpringBoot 自定义监听器(Listener)
  9. 小米6android版本更新,钉子户小米6的新生,换电池、背盖,升级android11
  10. 免费下载谷歌maps软件_Android Google Maps当前位置,夜间模式功能
  11. 2019 ICPC西安邀请赛 游记
  12. Hej Stylus for Mac(手写笔画图工具)
  13. 金山现任CEO张宏江将退休 西山居CEO继任
  14. php++i和i++区别_PHP和i,第1部分
  15. 万能计算机作文,万能机器人作文9篇
  16. Google首席Java架构师访谈:选择编程语言就像选择酒吧
  17. 访问他人mysql数据库_怎么连接访问他人的MYSQL数据库?
  18. 《货币商人》读后感作文选登3800字
  19. 假设人的心率为每分钟75下,写一个程序,询问用户的年龄(以年为单位),然后计算并输出该用户到目前为止的生命中已有的心跳总数。(不考虑闰年,假定一年均为365天)
  20. 软考高级-系统分析师-案例分析-系统设计

热门文章

  1. 《精通javascript》5,6章小结(一)
  2. 简述移动端常见的布局方式
  3. 《机器学习实战》代码实现学习一 使用K-近邻算法改进约会网站的配对效果(数据准备)...
  4. wildfly access log 开启
  5. highcharts 使用实例
  6. 我所认为的KVC和KVO
  7. mozilla开源项目学习----智能指针(AutoPtrAutoArrayPtr)(上)
  8. 处理文章附件路径问题
  9. Asp.Net 2.0 发布问题(转)
  10. Python请求外部POST请求,常见四种请求体