QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#836359 | #9924. Reconstruction | ucup-team191# | RE | 4ms | 55288kb | C++23 | 2.6kb | 2024-12-28 18:57:07 | 2024-12-28 18:57:08 |
Judging History
answer
#include <bits/stdc++.h>
#define PB push_back
using namespace std;
const int N = 5e5 + 500;
const int LOG = 19;
vector < int > t1[N], t2[N];
int n;
int par[LOG][N], dep[N], siz[N];
int u_cnt[N], oduzmi[N], gore[N], dolje[N], tot_u;
int L[N], R[N], tme;
void dfs2(int x, int lst) {
L[x] = tme++;
for(int y : t2[x]) {
if(y == lst) continue;
dep[y] = dep[x] + 1;
par[0][y] = x;
dfs2(y, x);
}
R[x] = tme - 1;
}
int lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int j = LOG - 1;j >= 0;j--)
if(dep[x] - dep[y] >= (1 << j)) x = par[j][x];
if(x == y) return x;
for(int j = LOG - 1;j >= 0;j--)
if(par[j][x] != par[j][y])
x = par[j][x], y = par[j][y];
return par[0][x];
}
void ddfs2(int x, int lst) {
siz[x] = 1;
for(int y : t2[x]) if(y != lst) {
ddfs2(y, x);
siz[x] += siz[y];
u_cnt[x] += u_cnt[y];
oduzmi[x] += oduzmi[y];
}
if(x - 1) {
dolje[x] = siz[x] - 1 == (u_cnt[x] - oduzmi[x]) / 2;
gore[x] = (n - siz[x]) - 1 == (tot_u - u_cnt[x] - oduzmi[x]) / 2;
//printf("x(%d) : dolje (%d) | gore (%d)\n", x, dolje[x], gore[x]);
}
}
int digni(int x, int k) {
for(int j = 0;j < LOG;j++)
if(k & (1 << j)) x = par[j][x];
return x;
}
int subtree(int x, int y) {
if(L[x] <= L[y] && R[y] <= R[x])
return digni(y, dep[y] - dep[x] - 1);
return par[0][x];
}
int cur, delta[N], ans[N];
void asf(int x, int lst) {
ans[x] = cur == n - 1;
for(int y : t2[x]) {
if(y == lst) continue;
cur += gore[y] - dolje[y];
asf(y, x);
cur -= gore[y] - dolje[y];
}
}
int main() {
scanf("%d", &n);
for(int i = 1;i < n;i++) {
int x, y; scanf("%d%d", &x, &y);
t1[x].PB(y), t1[y].PB(x);
}
for(int i = 1;i < n;i++) {
int x, y; scanf("%d%d", &x, &y);
t2[x].PB(y), t2[y].PB(x);
}
dfs2(1, 1);
for(int j = 1;j < LOG;j++)
for(int i = 1;i <= n;i++)
par[j][i] = par[j - 1][par[j - 1][i]];
for(int x = 1;x <= n;x++) {
for(int y : t1[x]) if(x < y) {
u_cnt[x]++; u_cnt[y]++; tot_u += 2;
oduzmi[x]++; oduzmi[y]++;
oduzmi[lca(x, y)] -= 2;
}
}
ddfs2(1, 1);
for(int x = 1;x <= n;x++) {
map<int,int> kolko;
for(int y : t1[x]) {
int yy = subtree(x, y);
kolko[yy]++;
}
for(int y : t2[x]) {
bool mogu = (int)t1[x].size() - kolko[y] == (int)t2[x].size() - 1;
//printf("%d -> %d : %d\n", y, x, mogu);
if(y == par[x][0]) {
dolje[x] &= mogu;
} else {
gore[y] &= mogu;
}
}
}
for(int x = 1;x <= n;x++) cur += dolje[x];
asf(1, 1);
for(int x = 1;x <= n;x++)
printf("%d", ans[x] && (t1[x].size() == t2[x].size()));
printf("\n");
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 55288kb
input:
3 1 2 2 3 2 1 1 3
output:
001
result:
ok single line: '001'
Test #2:
score: 0
Accepted
time: 3ms
memory: 55016kb
input:
6 1 3 3 4 3 6 4 5 5 2 1 3 1 4 4 5 5 2 3 6
output:
010110
result:
ok single line: '010110'
Test #3:
score: 0
Accepted
time: 4ms
memory: 44732kb
input:
1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 0ms
memory: 54996kb
input:
2 1 2 1 2
output:
11
result:
ok single line: '11'
Test #5:
score: -100
Runtime Error
input:
500000 321614 78209 64619 204431 81539 336200 128603 201377 132521 391792 41587 137224 174146 381680 341915 451206 493371 256005 259794 168656 161914 462290 105839 333679 377214 267008 283062 457340 219692 196741 123276 321510 473789 410796 498171 203543 178249 456921 255509 449152 294196 457566 277...