正题
题目链接:http://noip.ybtoj.com.cn/contest/102/problem/2
题目大意
求构造一个包含根节点的联通子图kkk个的树。
解题思路
现在考虑一棵树,如果我们在根节点处加一个点,那么方案数会×2\times 2×2。如果在根节点上加入一个父节点(根会转移上去),那么方案数会+1+1+1。这样点数是log\loglog级别的,能够通过本题。
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{int x,y;
}a[1100];
int n,last,cnt,tot;
int main()
{
// freopen("b.in","r",stdin);
// freopen("b.out","w",stdout);while(scanf("%d",&n)!=EOF){last=cnt=1;tot=0;while(n>1){if(n&1)a[++tot]=(node){last,++cnt},n--,last=cnt;else a[++tot]=(node){last,++cnt},n/=2;}printf("%d\n",cnt);for(int i=1;i<=tot;i++)printf("%d %d\n",a[i].x,a[i].y);}
}