ARC103E - Tr/ee 构造题

版权声明:_ https://blog.csdn.net/lunch__/article/details/82912768

题意

  • 给你一个 01 01 串,请你构造出一颗大小和 01 01 串长度相等的树,如果 01 01 串第 i i 位为 1 1 代表这棵树断开一条边后可以形成大小为 i i 的联通块,为 0 0 代表不会形成,如果无解输出 1 -1 .

首先来看无解的情况

显然断开一条边后不可能存在大小为 n n 的联通块,也一定会存在大小为 1 1 的联通块,所以 S [ 1 ] = 1 , S [ n ] = 0 S[1]=1,S[n]=0 ,然后如果 S [ i ] S [ n i ] , S[i]≠S[n - i], 也不会有解,这些情况判掉后就可以构造了

扫把图有一个很棒的性质,那就是把它分离出来除了大小为 1 1 ,就只有它子树大小的联通块了,那么每次从小到大构造扫把,遇到大的把原来的小的接在这上面,再把多出来的大小直接接在根上,然后就做完了

Codes

#include<bits/stdc++.h>
#include<bits/extc++.h>

#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define go(x, i) for(register int i = head[x]; i; i = nxt[i])
#define For(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define FOR(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define debug(x) cout << #x << " = " << x << endl
#define mem(a, b) memset(a, b, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(a))
#define min(a, b) (a < b ? a : b)
#define max(a, b) (b < a ? a : b)
#define inf (0x3f3f3f3f)
#define INF (1e18)
#define pb push_back
#define mp make_pair
#define x first
#define y second

typedef unsigned long long ull;
typedef unsigned int uint;
typedef long long ll;
typedef std::pair<ll, int> PLI;
typedef std::pair<int, int> PII;
typedef long double ldb;
typedef double db;

namespace IO {
#define getc() ((S_ == T_) && (T_ = (S_ = Ch_) + fread(Ch_, 1, Buffsize, stdin), S_ == T_) ? 0 : *S_ ++)
#define putc(x) *nowps ++ = (x)
	const uint Buffsize = 1 << 15, Output = 1 << 23;
	static char Ch_[Buffsize], *S_ = Ch_, *T_ = Ch_;
	static char Out[Output], *nowps = Out;
	inline void flush(){fwrite(Out, 1, nowps - Out, stdout); nowps = Out;}
	template<class T>inline void read(T &_) {
		_ = 0; static char __; T ___ = 1;
		for(__ = getc(); !isdigit(__); __ = getc()) if(__ == '-') ___ = -1;
		for(; isdigit(__); __ = getc()) _ = (_ << 3) + (_ << 1) + (__ ^ 48);
		_ *= ___;
	}
	template<class T>inline void write(T _, char __ = '\n') {
		if(!_) putc('0');
		if(_ < 0) putc('-'), _ = -_;
		static uint sta[111], tp;
		for(tp = 0; _; _ /= 10) sta[++ tp] = _ % 10;
		for(; tp; putc(sta[tp --] ^ 48)); putc(__);
	}
	template<class T>inline bool chkmax(T &_, T __) {return _ < __ ? _ = __, 1 : 0;}
	template<class T>inline bool chkmin(T &_, T __) {return _ > __ ? _ = __, 1 : 0;}
}

using namespace std;
using namespace IO;

const int N = 1e5 + 10;

char S[N]; int n;

int main() {
#ifdef ylsakioi
	file("e");
#endif
	scanf("%s", S + 1), n = strlen(S + 1);
	if(S[1] == '0' || S[n] == '1') return puts("-1"), 0;
	For(i, 1, n - 1) if(S[i] ^ S[n - i]) return puts("-1"), 0;
	int now = 1;
	For(i, 2, n) if(S[i] == '1') {
		For(k, now, i - 1) cout << k << ' ' << i << endl; now = i;
	}
	For(i, now, n - 1) cout << now << ' ' << n << endl;
	return flush(), 0;
}

猜你喜欢

转载自blog.csdn.net/lunch__/article/details/82912768
tr