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.
- 3≤N≤1003≤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
3
Sample Output 1 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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
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=;
repd(i,,n)
{
repd(j,i+,n)
{
if(n&)
{
if(i+j!=n)
{
ans++;
v.push_back(mp(i,j));
// cout<<i<<" "<<j<<endl;
}
}else
{
if(i+j!=n+)
{
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 ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}