2018.09.02 【Atcoder Regular Contest 102】D.All Your Paths are Different Lengths(构造算法)(二进制拆分)

传送门


解析:

好一道二进制拆分。。。

n = f l o o r ( l o g 2 L ) n=floor(log_2L) n=floor(log2L)
其实这道题很容易考虑到二进制拆分,就是每两个点之间加边, i i i i + 1 i+1 i+1连一条0的边和一条 2 i − 1 2^{i-1} 2i1的边。那么在 2 n − 1 2^{n}-1 2n1以下的所有数都有路径满足并且唯一满足。(注意题目要求只有精确的L条路径,不能多,也不能少)。

剩下的怎么办?为了不让添加的边产生新的路径,我们直接从中间点向最后一个节点加边。。


代码(本来想写 e m p l a c e emplace emplace_ b a c k back back,结果自己的编译器版本太老用不了):

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const
#define st static
#define mp make_pair
#define emplace_back push_back

inline
void outint(int a){
	st char ch[13];
	if(a==0)pc('0');
	while(a)ch[++ch[0]]=(a-a/10*10)^48,a/=10;
	while(ch[0])pc(ch[ch[0]--]);
}

vector<pair<pair<int,int> ,int > > e;

int main(){
	int L;
	cin>>L;
	int n=0,tmp=L;
	while(tmp){
		++n,tmp>>=1;
	}
	for(int re i=1;i<n;++i){
		e.emplace_back(mp(mp(i,i+1),0));
		e.emplace_back(mp(mp(i,i+1),1<<(i-1)));
	}
	int w=1<<(n-1),cnt=1;
	while(L^1){
		if(L&1){
			e.emplace_back(mp(mp(cnt,n),w));
			w|=1<<(cnt-1);
		}
		L>>=1;
		++cnt;
	}
	outint(n),pc(' '),outint(e.size()),pc('\n');
	for(vector<pair<pair<int,int>,int> >:: iterator it=e.begin();it!=e.end();++it){
		outint(it->first.first),pc(' '),outint(it->first.second),pc(' '),outint(it->second),pc('\n');
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值