Codeforces 828 D High Load

来源:互联网 发布:linux系统编程 第2版 编辑:程序博客网 时间:2024/06/08 16:39

题目地址:http://codeforces.com/contest/828/problem/D
题意:给你n个点,只允许有m个点只有一条边,求连接的边的情况,已经两个端点的最小最长距离
思路:因为就只有m个分支,直接连下去就好了,没什么难度,最后最长距离判断下条件就好了。

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#define LL long long #define N 10000010#define M 50010#define inf 0x3f3f3f3fusing namespace std;const LL mod = 1e9 + 7;const double eps = 1e-9;int main() {    cin.sync_with_stdio(false);    int n, m;    int num;    while (cin >> n >> m) {        num = 2;        int ans = (n - 1) / m;        int cnt = (n - 1) % m;        if (cnt == 1) {            cout << ans * 2 + 1 << endl;        }        else if (cnt > 1) {            cout << ans * 2 + 2<<endl;        }        else {            cout << ans * 2 << endl;        }        for (int i = 0; i < cnt; i++) {            for (int j = 0; j <= ans; j++) {                if (j == 0) {                    cout << "1 " << num << endl;                    num++;                }                else {                    cout << num - 1 << " " << num << endl;                    num++;                }            }        }        for (int i = cnt; i < m; i++) {            for (int j = 0; j < ans; j++) {                if (j == 0) {                    cout << "1 " << num << endl;                    num++;                }                else {                    cout << num - 1 << " " << num << endl;                    num++;                }            }        }    }    return 0;}
原创粉丝点击