AtCoder Grand Contest 032-B - Balanced Neighbors (构造)

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 700700 points

Problem Statement

You are given an integer NN. Build an undirected graph with NN vertices with indices 11 to NN that satisfies the following two conditions:

  • The graph is simple and connected.
  • There exists an integer SS such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is SS.

It can be proved that at least one such graph exists under the constraints of this problem.

Constraints

  • All values in input are integers.
  • 3N1003≤N≤100

Input

Input is given from Standard Input in the following format:

NN

Output

In the first line, print the number of edges, MM, in the graph you made. In the ii-th of the following MM lines, print two integers aiai and bibi, representing the endpoints of the ii-th edge.

The output will be judged correct if the graph satisfies the conditions.


Sample Input 1 Copy

Copy
3

Sample Output 1 Copy

Copy
2
1 3
2 3
  • For every vertex, the sum of the indices of the vertices adjacent to that vertex is 33.

 

题意:

给你一个数字n,

让你构造你简单图(无重边)

要求这个图的每一个节点所连接的节点的id值加起来相等。*(不用加自己的id值)

思路:

显然找规律的构造题。

我们反过来想,先构建一个完全图,设法去掉一些有规律的边,使整个图满足条件。

通过分析可以发现规律。

当n是奇数的时候,

删除i+j=n的边

否则

删除i+j=n+1的边

 

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    int n;
    cin>>n;
    std::vector<pii> v;
    int ans=0;
    repd(i,1,n)
    {
        repd(j,i+1,n)
        {
            if(n&1)
            {
                if(i+j!=n)
                {
                    ans++;
                    v.push_back(mp(i,j));
                    // cout<<i<<" "<<j<<endl;
                }
            }else
            {
                if(i+j!=n+1)
                {
                    ans++;
                    v.push_back(mp(i,j));
                    // cout<<i<<" "<<j<<endl;
                }
            }
        }
    }
    cout<<ans<<endl;
    for(auto x: v)
    {
        cout<<x.fi<<" "<<x.se<<endl;
    }
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 

转载于:https://www.cnblogs.com/qieqiemin/p/10764647.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值