题目大意
有n个城市,构造一个图,从1到n的所有路径长为l到r且不重复
题解
这位大佬的题解非常good,膜拜大佬Orz
大佬传送门
魔改的代码
#include<cstdio>
const int MAX=1e6+5;
struct node{
int from;
int to;
int value;
};
node ans[MAX];
int ansn;
int rnum[MAX],rn;
int two[MAX];
int main(){
int l,r;
scanf("%d%d",&l,&r);
printf("YES\n");
r-=l-1;
int mid=1,k=0,keep=r;
while(r>=mid){
two[mid]=k+2;
mid*=2;
k++;
}
mid/=2;
if(r!=mid){
r--;
while(r){
rnum[rn++]=mid;
r-=mid;
while(r<mid){
mid/=2;
}
}
}
for(int i=2;i<=k+1;i++){
ans[ansn].from=1;
ans[ansn].to=i;
ans[ansn++].value=l;
}
int val=1;
for(int i=2;i<=k+1;i++){
for(int j=i+1;j<=k+1;j++){
ans[ansn].from=i;
ans[ansn].to=j;
ans[ansn++].value=val;
}
val*=2;
}
r=keep;
if(rn){
ans[ansn].from=1;
ans[ansn].to=k+2;
ans[ansn++].value=ans[0].value;
ans[ansn].from=k+1;
ans[ansn].to=k+2;
ans[ansn++].value=1;
}
for(int i=rn-1;i>=1;i--){
ans[ansn].from=two[rnum[i]];
ans[ansn].to=k+2;
r-=rnum[i];
ans[ansn++].value=r;
}
printf("%d %d\n",rn<1?k+1:k+2,ansn);
for(int i=0;i<ansn;i++){
printf("%d %d %d\n",ans[i].from,ans[i].to,ans[i].value);
}
}