【Codeforces 404C】Restore Graph

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

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

原文地址:https://www.cnblogs.com/AWCXV/p/10562232.html

时间: 2024-08-30 02:59:37

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

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

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

[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=2000),问满足[数列长度是k && 数列中每一个元素arr[i]在1~n之间 && 数列中元素可以重复]的数列有多少个?结果对10^9+7取余 解题思路:dp[i][j]表示长度是j,最后一位是i的种数 if(kk%i==0) dp[kk][j+1]+=dp[i][j] 1 #i

【Codeforces 368A】Brain&#39;s Photos 水题

黑白灰都是#Black&White #include <cstdio> int n,m; int main() { scanf("%d%d",&n,&m); int ok=0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char s[5]; scanf("%s",s); if(s[0]!='W'&&s[0]!='B'&&s[0]!='G')

【Codeforces 1114C】Trailing Loves (or L&#39;oeufs?)

[链接] 我是链接,点我呀:) [题意] 问你n!的b进制下末尾的0的个数 [题解] 证明:https://blog.csdn.net/qq_40679299/article/details/81167283 这题的话m比较大, 做个质因数分解就ok>_< 算n!有多少个x因子的话 以5为例子 (n=25) 25 20 15 10 5 把他们都除5 5 4 3 2 1 然后再除5 1 所以总共有6个 转换成代码就是 while(n>0){ ans+=n/5; n = n/5; } [代码

【Codeforces 332C】Students&#39; Revenge

Codeforces 332 C 我爱对拍,对拍使我快乐... 题意:有\(n\)个议题,学生们会让议会同意\(p\)个,其中主席会执行\(k\)个, 每一个议题执行后主席会掉\(a_i\)的头发,不执行后议会会增加\(b_i\)的不开心值, 然后主席想让议会的不开心值最小,如果有多重方案就选自己头发掉的最少的: 而学生们想让主席的头发掉的最多,如果有多种方案让议会的不开心值最大. 问让议会同意哪\(p\)个会达到最好的效果. 思路1: 这是我的不对的思路. (虽然没提交 我们首先将所有的数按照

【Codeforces 429D】 Tricky Function

[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = Sj - Si f(i,j) = (i-j)^2 + (Si - Sj)^2 观察这个式子,我们发现可以用类似于平面最近点对的算法来求解该问题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 const

【Codeforces 105D】 Bag of mice

[题目链接] http://codeforces.com/contest/148/problem/D [算法] 概率DP f[w][b]表示还剩w只白老鼠,b只黑老鼠,公主胜利的概率,那么 : 1. 公主抓到白老鼠,概率为w/(w+b) 2. 公主抓到黑老鼠,那么龙也抓到黑老鼠,如果跑出来的老鼠是白颜色的,则概率为 : b / (w + b) * b / (w + b - 1) * w / (w + b - 2) * f[w-1][b-2] 否则,概率为 : b / (w + b) * (b -

【Codeforces 258A】 Game With Sticks

[题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bits/stdc++.h> using namespace std; int n,m; int main() { scanf("%d%d",&n,&m); if (min(n,m) % 2 == 0) printf("Malvika\n"); else

【Codeforces 258B】 Sort the Array

[题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即可 [代码] #include<bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int i,n,l,r; bool flag; int a[MAXN]; int main() { scanf("%d"