zoukankan      html  css  js  c++  java
  • AT4752 [ABC131E] Friendships

    题目链接

    • 题解:想到了菊花图,没想到最值就是一棵树,然后是菊花图,然后要让合法点对减少的操作,就是给两个叶子连边,这样就删除了。
    • 代码:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const ll N = 1e6 + 9, mod = 1e9 + 7;
    struct node {
        int a, b;
    }a[N];
    bool cmp(node a, node b) {
        if (a.b == b.b) {
            return a.a > b.a;
        }
        return a.b < b.b;
    }
    int judge(int n, int k) {
        int s = n-2;
        int ret = 0;
        while (ret <= n - 2) {
            if (s == k)return ret;
            ret ++;
            s += ret;
        }
        return -1;
    }
    int main() {
        int n, k;cin >> n >> k;
        int sum = 0;
        sum = (n-1) * (n-2)/2;
        if (k > sum) {
            cout << -1 << endl;
        } else {
            int ok = sum - k;
            int ans = n-1;
            cout << n-1 + ok << endl;
            for (int i = 2; i <= n; i ++) {
                cout << 1 << " " << i << endl;
            }
            if (ok == 0)return 0;
            for (int i = 2; i <= n; i ++) {
                for (int j = i + 1; j <= n; j ++) {
                    cout << i << " " << j << endl;
                    ok--;
                    if (!ok)return 0;
                }
            }
        } 
    } 
    
  • 相关阅读:
    Luogu P2633 Count on a tree
    Luogu P4011 孤岛营救问题
    Luogu P3157 [CQOI2011]动态逆序对
    SCOI2015 国旗计划
    AT2165 Median Pyramid Hard
    BZOJ2959 长跑
    SCOI2015 情报传递
    SDOI2011 染色
    SCOI2010 幸运数字
    SHOI2016 黑暗前的幻想乡
  • 原文地址:https://www.cnblogs.com/Xiao-yan/p/14661863.html
Copyright © 2011-2022 走看看