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;
            }
        }
    } 
} 
原文地址:https://www.cnblogs.com/Xiao-yan/p/14661863.html