B. Recover the String
大意: 求构造01字符串使得子序列00,01,10,11的个数恰好为$a_{00},a_{01},a_{10},a_{11}$
挺简单的构造, 注意到可以通过$a_{00}$和$a_{11}$求出0和1的个数, 假设求出分别为$x,y$, 然后再调整a01与a10, 可以注意到a01的范围是在[0,xy], 并且最小值的状态为11...1100...00, 每次将右侧的1前移一位恰好增加1, 所以这样不断调整即可. 忘了判0, WA了4发..
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int x, y, a[4];
char s[N];
int chk(int x) {
int l=0, r=x+10, ans;
while (l<=r) {
if ((ll)mid*(mid-1)/2>=x) ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
} int main() {
REP(i,0,3) scanf("%d", a+i);
x = chk(a[0]), y = chk(a[3]);
if ((ll)x*(x-1)/2!=a[0]||(ll)y*(y-1)/2!=a[3]) return puts("Impossible"),0;
if (x==0&&y==0) {
if (a[1]+a[2]>1) return puts("Impossible"),0;
if (a[1]) puts("01");
else if (a[2]) puts("10");
else puts("0");
return 0;
}
if (!a[1]&&!a[2]) {
if (x==0) {REP(i,1,y) putchar('1');return hr,0;}
if (y==0) {REP(i,1,x) putchar('0');return hr,0;}
return puts("Impossible"),0;
}
x = max(x, 1), y = max(y, 1);
ll sum = 0;
REP(i,0,3) sum+=a[i];
if (sum!=(ll)(x+y)*(x+y-1)/2) return puts("Impossible"),0;
if (a[1]>(ll)x*y) return puts("Impossible"),0;
int k = a[1]/x, len = x+y;
REP(i,1,len) s[i]='0';
REP(i,1,y-k) s[i]='1';
REP(i,len-k+1,len) s[i]='1';
int res = a[1]-k*x;
s[y-k]='0', s[y-k+res]='1';
puts(s+1);
}
C. Centroids
大意: 给定树, 对于每个点判断移动一条边后是否能成为重心.
考虑每个点为根的情形, 只需要将最大子树选出尽量大的一块连到根上即可, 具体实现用树形dp, 维护最大转移与次大转移, 走最大子树时用次大, 其余用最大转移即可.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e6+10;
int n;
vector<int> g[N];
int mx[N], m1[N], m2[N], sz[N], ans[N];
void dfs(int x, int fa) {
sz[x] = 1;
int &t1=m1[x], &t2=m2[x];
for (int y:g[x]) if (y!=fa) {
dfs(y,x), sz[x]+=sz[y];
if (mx[y]>mx[t1]) t1=y;
}
for (int y:g[x]) if (y!=fa&&y!=t1) {
if (mx[y]>mx[t2]) t2=y;
}
mx[x] = mx[t1];
if (sz[x]<=n/2) mx[x] = sz[x];
}
void dfs2(int x, int fa, int pre) {
ans[x] = 1;
if (n-sz[x]-pre>n/2) ans[x]=0;
int &t1=m1[x], &t2=m2[x];
for (int y:g[x]) if (y!=fa) {
if (sz[y]-mx[y]>n/2) ans[x]=0;
if (n-sz[y]<=n/2) dfs2(y,x,n-sz[y]);
else dfs2(y,x,y==t1?max(pre,mx[t2]):max(pre,mx[t1]));
}
} int main() {
scanf("%d", &n);
REP(i,2,n) {
int u, v;
scanf("%d%d", &u, &v);
g[u].pb(v),g[v].pb(u);
}
dfs(1,0),dfs2(1,0,0);
REP(i,1,n) printf("%d ",ans[i]);hr;
}