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

【题意】

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

【Codeforces 404C】Restore Graph的更多相关文章

  1. 【11.61%】【codeforces 670F】Restore a Number

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【Codeforces 246D】Colorful Graph

    [链接] 我是链接,点我呀:) [题意] 让你找到所有和x颜色的点中,和该颜色的点颜色不同的相邻的点的个数(重复颜色算一次) 求出哪种颜色的所要求的点的数量最多. [题解] 对于每一条边只会被查到两次 ...

  3. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  4. 【codeforces 755E】PolandBall and White-Red graph

    [题目链接]:http://codeforces.com/contest/755/problem/E [题意] 给你n个节点; 让你在这些点之间接若干条边;构成原图(要求n个节点都联通) 然后分别求出 ...

  5. 【codeforces 716D】Complete The Graph

    [题目链接]:http://codeforces.com/problemset/problem/716/D [题意] 给你一张图; 这张图上有一些边的权值未知; 让你确定这些权值(改成一个正整数) 使 ...

  6. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【27.91%】【codeforces 734E】Anton and Tree

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【30.36%】【codeforces 740D】Alyona and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【codeforces 755C】PolandBall and Forest

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. bzoj 1778: [Usaco2010 Hol]Dotp 驱逐猪猡【dp+高斯消元】

    算是比较经典的高斯消元应用了 设f[i]为i点答案,那么dp转移为f[u]=Σf[v]*(1-p/q)/d[v],意思是在u点爆炸可以从与u相连的v点转移过来 然后因为所有f都是未知数,高斯消元即可( ...

  2. loadrunner11 安装破解,汉化包

    说说自己的心痛史,好不容易安装了loadrunner 11 居然浏览器不支持,我的系统是win8.1,ie浏览器最低支持ie11,我还能说啥子...其他浏览器试过了依旧是不可以!!所以我安装了一个虚拟 ...

  3. Maven之项目搭建与第一个helloworld(多图)

    这次记录第一个搭建一个maven的helloworld的过程. 转载 1.搭建web工程肯定得new 一个 maven工程,假如project中没有直接看到maven工程,那么选择Other,然后在W ...

  4. 232 Implement Queue using Stacks 用栈来实现队列

    使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...

  5. SQL数据库基础————委托

    委托:也称为代理,事件也是一种委托:定义在类的最外面 1.定义委托关键字:delegate函数签名:签名和函数保持一致定义委托的时候要根据函数来定义public delegate int First( ...

  6. eclipse中folder、source folder和package的区别

    今天做ssm项目时,突然发现了这个问题,特别好奇,sqlSessionFactory.xml文件如何找到: 1.放在src/hello目录下: InputStream inputStream = Re ...

  7. Git在工作中对项目的操作流程

    Git 的准备工作 第一步:Git初始化 第一次在电脑上使用时,应该初始化操作,以后再使用Git操作,无需初始化,直接进行Git其他操作 git config --global user.name & ...

  8. Farseer.net轻量级开源框架 中级篇:DbFactory数据工厂

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 执行SQL语句 下一篇:Farseer.net轻量级开源框架 中级篇: 数据绑定 ...

  9. jq封装插件

    $.extend()拓展方法: $(function(){ $.extend({ money:function(){ alert("我要努力赚钱") }, money:functi ...

  10. JMeter怎样测试WebSocket,示例演示(二)

    一.测试案例演示 以  http://www.websocket.org/echo.html 网站为例. 地址为:ws://echo.websocket.org 二.长连接的影响 1.没有勾选stre ...