int n; char val[maxn]; int num[maxn]; boolcheck(int x) { int cnt = 0; memset(num, 0, sizeof(num)); int end = -1; int st = -1; repp(i, 1, n) { if (val[i] == '1') { st = i; end = i + x - 1; break; } } if (end == -1) { returntrue; } repp(i, st, end) { int now = val[i] - '0'; int op = now ^ (cnt & 1); if (op) { if (i + x - 1>n) returnfalse; cnt++; } } returntrue; } boolcheck2(int x) { int cnt = 0; memset(num, 0, sizeof(num)); int end = -1; int st = -1; repp(i, 1, n) { if (val[i] == '0') { st = i; end = i + x - 1; break; } } if (end == -1) { returntrue; } repp(i, st, end) { int now = '1' - val[i]; int op = now ^ (cnt & 1); if (op) { if (i + x - 1>n) returnfalse; cnt++; } } returntrue; } voidsolve() { scanf("%s", val + 1); n = strlen(val + 1); int le = 1; int ri = n; while (le<ri) { int mid = (le + ri + 1) / 2; if (check(mid) || check2(mid)) { le = mid; } else { ri = mid - 1; } } cout << le; }
int n; vector<int>edge[maxn]; int dp[maxn]; booldfs(int x, int fa, int up) { vector<int>son; if (edge[x].size() & 1)son.push_back(0); for (int i = 0; i<edge[x].size(); i++) { int to = edge[x][i]; if (to == fa)continue; if (!dfs(to, x, up))returnfalse; if(dp[to] != -1)son.push_back(dp[to] + 1); } sort(son.begin(), son.end()); if (son.size() % 2 == 0) { int st = 0; int en = son.size() - 1; while (st < en) { if (son[st] + son[en] > up) { returnfalse; } st++; en--; } dp[x] = -1; } else { int le = 0; int ri = son.size() - 1; int flag = 1; while (le < ri) { int mid = (le + ri) / 2; int i = 0; int j = son.size() - 1; flag = 1; while (i < j) { if (i == mid)i++; if (j == mid)j--; if (i<j) { if (son[i] + son[j] > up) { flag = 0; break; } else { i++; j--; } } } if (flag) { ri = mid; } else { le = mid + 1; } } if (son[le] > up)returnfalse; int i = 0; int j = son.size() - 1; while (i < j) { if (i == le)i++; if (j == le)j--; if (i<j) { if (son[i] + son[j] > up) { returnfalse; } else { i++; j--; } } } dp[x] = son[le]; } returntrue; } voidsolve() { sa(n); repp(i, 1, n - 1) { int x, y; sa(x), sa(y); edge[x].push_back(y); edge[y].push_back(x); } int cnt = 0; int p = 1; repp(i, 1, n) { if (edge[i].size() & 1) { cnt++; } if (edge[i].size() == 1) { p = i; } } int ans = cnt / 2; int le = 1; int ri = n; while (le < ri) { int mid = (le + ri) / 2; memset(dp, 0, sizeof(dp)); if (dfs(p, -1, mid)) { ri = mid; } else { le = mid + 1; } } cout << ans << " " << le << endl; }