#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int n, m;
int num[N] = { 0 }, id[N] = { 0 }, rid[N];
bool vis[N] = { false };
void dfs(int x, int cnt) {
vis[x] = true;
if (cnt & 1) num[x] = 2, --cnt;
if (cnt) {
dfs(x << 1, cnt >> 1);
dfs(x << 1 | 1, cnt >> 1);
}
}
void dfs1(int x, int& tot) {
if (num[x] == 0) num[x] = 1, ++tot;
if (vis[x << 1]) dfs1(x << 1, tot);
if (vis[x << 1 | 1]) dfs1(x << 1 | 1, tot);
}
void dfs2(int x, int tot) {
vis[x] = true;
if (!num[x] && tot) num[x] = 1, --tot;
// printf("%d %d\n", x, num[x]);
if (tot > 0) {
int tmp = tot >> 1;
dfs2(x << 1, tmp);
dfs2(x << 1 | 1, tot - tmp);
}
}
void dfs3(int x, int& tot) {
if (num[x]) {
id[x] = ++tot;
rid[tot] = x;
}
if (vis[x << 1]) dfs3(x << 1, tot);
if (vis[x << 1 | 1]) dfs3(x << 1 | 1, tot);
}
int main() {
int x, y;
scanf("%d %d", &x, &y);
dfs(1, y);
int tot = 0;
dfs1(1, tot);
if (tot > x) puts("-1");
else {
x -= tot;
dfs2(1, x);
int tmp = 0;
dfs3(1, tmp);
for (int i = 1; i <= tmp; ++i) {
int u = rid[i];
printf("%d %d %d\n", num[u], id[u << 1], id[u << 1 | 1]);
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<typename T>
inline void rd(T& x)
{
int tmp = 1; char c = getchar(); x = 0;
while (c > '9' || c < '0') { if (c == '-')tmp = -1; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
x *= tmp;
}
const int N = 3e5 + 10;
const int M = 1e7 + 10;
const int mod = 1e9 + 7, inf = 0x3f3f3f3f;
ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y);}
int head[N], cntE = 0;
struct edge {
int next, to, w;
}e[M];
void add(int u, int v, int w = 0) {
e[cntE].to = v;
e[cntE].next = head[u];
e[cntE].w = w;
head[u] = cntE++;
}
int n, m;
vector<int> dfs(int x) {
vector<int>ans;
for (int i = head[x]; ~i; i = e[i].next) {
int u = e[i].to;
vector<int>tmp = ans, now = dfs(u);
int op = 1;
for (auto v : now) {
op ^= 1;
tmp.push_back(v);
if (op) tmp.insert(tmp.end(), ans.begin(), ans.end());
else tmp.insert(tmp.end(), ans.rbegin(), ans.rend());
}
ans = tmp;
}
ans.insert(ans.begin(), x);
return ans;
}
int main() {
#ifdef _DEBUG
FILE* _INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT = freopen("output.txt", "w", stdout);
#endif
rd(n);
memset(head, -1, sizeof(int) * (n + 10)); cntE = 0;
for (int i = 2; i <= n; ++i) {
int x; rd(x);
add(x, i);
}
vector<int>ans = dfs(1);
printf("%d\n", ans.size() - 1);
for (int i = 1; i < ans.size(); ++i) {
printf("%d ", ans[i]);
}
puts("");
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
int main() {
int T = 1;
scanf("%d", &T);
while (T--) {
ll b, d;scanf("%lld %lld", &b, &d);
if (b % d == 0) {
printf("%d\n", b - 1);
continue;
}
ll tmp = gcd(b, d);
d /= tmp;
ll ans = b / d;
if (b % d == 0) --ans;
printf("%lld\n", ans);
}
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e2 + 10;
const int INF = 0x3f3f3f3f;
int main() {
int n;
scanf("%d", &n);
int minn = INF, ans = -1;
for (int i = 1; i <= n; ++i) {
int x;
char s[N];
scanf("%d %s", &x, s);
int len = strlen(s);
int sum0 = 0, sum2 = 0, sum1 = 0;
for (int j = 0; j < len; ++j) {
if (s[j] == '0')
++sum0;
else if (s[j] == '1')
++sum1;
else if (s[j] == '2')
++sum2;
}
if (sum0 >= 1 && sum1 >= 1 && sum2 >= 2) {
if (x < minn) {
minn = x;
ans = i;
}
}
}
if (ans == -1)
puts("0");
else
printf("%d\n", ans);
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<typename T>
inline void rd(T& x)
{
int tmp = 1; char c = getchar(); x = 0;
while (c > '9' || c < '0') { if (c == '-')tmp = -1; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
x *= tmp;
}
const int N = 3e5 + 10;
const int M = 1e7 + 10;
const int mod = 1e9 + 7, inf = 0x3f3f3f3f;
ll gcd(ll x, ll y) { if (y == 0) return x; return gcd(y, x % y);}
int head[N], cntE = 0;
struct edge {
int next, to, w;
}e[M];
void add(int u, int v, int w = 0) {
e[cntE].to = v;
e[cntE].next = head[u];
e[cntE].w = w;
head[u] = cntE++;
}
int n, m;
struct node {
int w, id;
bool friend operator<(const node& a, const node& b) {
return a.w < b.w;
}
node(int w = 0, int id = 0) :w(w), id(id) {}
}a[N];
int fa[N][25] = { 0 }, h[N], lg[N];
void dfs(int x, int fx, int dep) {
fa[x][0] = fx;
h[x] = dep;
for (int i = head[x]; ~i; i = e[i].next) {
int v = e[i].to;
dfs(v, x, dep + 1);
}
}
void init() {
dfs(1, 0, 1);
for (int j = 1; j <= 20; ++j) {
for (int i = 1; i <= n; ++i) {
fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
}
lg[0] = 0;
for (int i = 1; i <= N - 10; ++i) {
lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = 0, tmp = h[u] - h[v]; i <= 19; ++i) {
if ((1 << i) & tmp) u = fa[u][i];
}
if (u == v) return u;
for (int i = lg[h[u]] - 1; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int main() {
#ifdef _DEBUG
FILE* _INPUT = freopen("input.txt", "r", stdin);
// FILE* _OUTPUT = freopen("output.txt", "w", stdout);
#endif
rd(n);
memset(head, -1, sizeof(int) * (n + 10)); cntE = 0;
for (int i = 1; i <= n; ++i) {
int x, y; rd(x), rd(y), rd(a[i].w);
a[i].id = i;
if (x) add(i, x);
if (y) add(i, y);
}
init();
sort(a + 1, a + 1 + n);
rd(m);
while (m--) {
int l, r; rd(l), rd(r);
if (r<a[1].w || l>a[n].w) {
puts("1");
continue;
}
int tl, tr;
if (l <= a[1].w) tl = 0;
else {
int tmp = lower_bound(a + 1, a + 1 + n, node(l, 0)) - a;
tl = lca(a[tmp - 1].id, a[tmp].id);
}
if (r >= a[n].w) tr = 0;
else {
int tmp = upper_bound(a + 1, a + 1 + n, node(r, 0)) - a;
tr = lca(a[tmp - 1].id, a[tmp].id);
}
int fx = lca(tl, tr);
int ans = (h[tl] + h[tr] - h[fx]) * 2 + 1;
printf("%d\n", ans);
}
return 0;
}
签到题
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 10;
vector<int>vec[N];
int num[N] = { 0 };
int main() {
int n;scanf("%d", &n);
for (int i = 1;i <= n;++i) {
int k;scanf("%d", &k);
while (k--) {
int x;scanf("%d", &x);
vec[i].push_back(x);
}
}
int ans = 0;
int maxn = 0;
for (int i = n;i > 0;--i) {
maxn = 0;
for (auto v : vec[i]) {
maxn = max(maxn, num[v]);
}
++maxn;
for (auto v : vec[i]) {
num[v] = maxn;
}
}
printf("%d\n", maxn);
}