NC221749. CatVirus
描述
输入描述
The only line contains an integer -- the number of valid ways to mark the vertices in the tree.
输出描述
Let's denote the number of vertices of the tree you construct as , and label the vertices from to where vertex is the root.
The output should contain lines. In the first line, print one integer . In the each of the next lines, print two integers describing an edge in your rooted tree.
It is guaranteed that at least one solution exists. If there are several possible valid solutions, you can print any of them.
示例1
输入:
2
输出:
1
示例2
输入:
3
输出:
2 1 2
示例3
输入:
5
输出:
3 1 2 1 3
示例4
输入:
6
输出:
4 1 2 2 3 2 4
C(clang11) 解法, 执行用时: 8ms, 内存消耗: 384K, 提交时间: 2021-05-10 21:48:27
#include<stdio.h> typedef long long ll; struct YPO {ll u,v; }gg[500050]; ll ci=1; int main() { ll n,k=1; scanf("%lld",&n); n-=1; while(n>1) { if(n&1!=0) { gg[ci].u=k; gg[ci].v=ci+1; k=ci+1; ci++; } gg[ci].u=k; gg[ci].v=ci+1; ci++; n>>=1; } printf("%lld\n",ci); for(int i=1;i<ci;i++) printf("%lld %lld\n",gg[i].u,gg[i].v); }
C++ 解法, 执行用时: 7ms, 内存消耗: 508K, 提交时间: 2022-04-06 21:16:03
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[1]; int main(){ ll n,t=1,x=1; cin>>n; n--; while(n!=1){ if(n&1){ a[t]=x; x=++t; } a[t++]=x; n/=2; } cout<<t<<endl; for(int i=1;i<t;i++) cout<<a[i]<<" "<<i+1<<'\n'; }