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;
}

AIM Tech Round 3 (Div. 1) (构造,树形dp,费用流,概率dp)的更多相关文章

  1. codeforce AIM tech Round 4 div 2 B rectangles

    2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...

  2. AIM Tech Round 4 (Div. 1) C - Upgrading Tree 构造 + 树的重心

    C - Upgrading Tree 我发现我构造题好弱啊啊啊. 很明显能想到先找到重心, 然后我们的目标就是把所有点接到重心的儿子上,让重心的儿子子树变成菊花图, 这个先把重心到儿子的边连到 i , ...

  3. AIM Tech Round 3 (Div. 1) B. Recover the String 构造

    B. Recover the String 题目连接: http://www.codeforces.com/contest/708/problem/B Description For each str ...

  4. AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)

    rating又掉下去了.好不容易蓝了.... A..没读懂题,wa了好几次,明天问队友补上... B. Checkpoints 题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n ...

  5. AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)

    D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  6. AIM Tech Round 3 (Div. 2)

    #include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...

  7. AIM Tech Round 3 (Div. 2) A B C D

    虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...

  8. AIM Tech Round 3 (Div. 2) B

    Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...

  9. AIM Tech Round 3 (Div. 2) A

    Description Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Ko ...

随机推荐

  1. python3.4学习笔记(十六) windows下面安装easy_install和pip教程

    python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...

  2. EXKMP

    (我和lesphere,reverse研究了这个东西一上午)QAQ kmp是求字符串S的任意前缀与字符串T的最长的相同的前缀和后缀 exkmp第求字符串S的任意后缀与字符串T的最长公共前缀 与kmp相 ...

  3. mysql的数据类型和字符集

    MySQL的数据类型 MySQL数据库支持的数据类型主要有以下几种: 整型 浮点型 字符 BLOB型 枚举和集合类型 JSON类型(MySQL5.7新增加的支持) 整型 整数类型是数据库中最基本的数据 ...

  4. Python Web学习笔记之Python多线程基础

    多线程理解 多线程是多个任务同时运行的一种方式.比如一个循环中,每个循环看做一个任务,我们希望第一次循环运行还没结束时,就可以开始第二次循环,用这种方式来节省时间. python中这种同时运行的目的是 ...

  5. 企业应用开发中最常用c++库

    log4cpp,http://log4cpp.sourceforge.net/,跟log4j一样,不说最重要,绝对是最常用的. zk 客户端,https://blog.csdn.net/yangzhe ...

  6. CP2102

    1概述 CP2102其集成度高,内置USB2.0全速功能控制器.USB收发器.晶体振荡器.EEPROM及异步串行数据总线(UART),支持调制解调器全功能信号,无需任何外部的USB器件.CP2102与 ...

  7. 小工具:使用Python自动生成MD风格链接

    很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护.最近发现自己刷了不少LC的题目了,想搬运到这个仓库上. 玩Github最重要的当然是写README了,MD的逼格决定了项目牛 ...

  8. HTTP If-Modified-Since引发的浏览器缓存汇总

    在看Spring中HttpServlet的Service方法时,对于GET请求,代码逻辑如下: if (method.equals(METHOD_GET)) { long lastModified = ...

  9. Python3基础 str translate 将指定字符转换成另一种特定字符

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. PHP中namespace和use使用详解

    来源于:http://www.jb51.net/article/36389.htm 命名空间一个最明确的目的就是解决重名问题,PHP中不允许两个函数或者类出现相同的名字,否则会产生一个致命的错误.这种 ...