QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#455467 | #8720. BFS 序 0 | Andyqian7 | WA | 371ms | 79120kb | C++14 | 2.9kb | 2024-06-26 14:34:48 | 2024-06-26 14:34:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5 + 10;
int n, T, m, dep[MAX], q[MAX], f[MAX][20], deg[MAX], vis[MAX];
vector<int> sons[MAX];
set<int> nxt[MAX];
int anc(int s, int dis)
{
int ans = s;
for (int i = 0; i < 20; i++)
{
if (dis >> i & 1)
ans = f[s][i];
}
return ans;
}
int lca(int x, int y)
{
if (dep[x] < dep[y])
swap(x, y);
x = anc(x, dep[x] - dep[y]);
for (int i = 19; ~i; i--)
{
if (f[x][i] != f[y][i])
{
x = f[x][i], y = f[y][i];
}
}
return f[x][0];
}
int main()
{
cin >> n;
for (int i = 2; i <= n; i++)
{
int fa;
cin >> fa;
sons[fa].push_back(i), dep[i] = dep[fa] + 1, f[i][0] = fa;
}
for (int i = 0; i < 19; i++)
{
for (int j = 1; j <= n; j++)
{
f[j][i + 1] = f[f[j][i]][i];
}
}
cin >> T;
while (T--)
{
cin >> m;
vector<int> c;
for (int i = 1; i <= m; i++)
{
cin >> q[i];
c.push_back(q[i]);
}
sort(c.begin(), c.end());
int sz = c.size();
if (unique(c.begin(), c.end()) - c.begin() != sz)
{
cout << "No" << endl;
continue;
}
set<int> todo;
bool sign = true;
for (int i = 1; i < m; i++)
{
if (dep[q[i]] > dep[q[i + 1]])
{
cout << "No" << endl;
sign = false;
break;
}
}
if (!sign)
continue;
for (int i = 1; i < m; i++)
{
if (dep[q[i]] < dep[q[i + 1]])
continue;
int z = lca(q[i], q[i + 1]);
int a = anc(q[i], dep[q[i]] - dep[z] - 1);
int b = anc(q[i + 1], dep[q[i + 1]] - dep[z] - 1);
nxt[a].insert(b);
todo.insert(a), todo.insert(b);
}
for (int s : todo)
{
for (int son : nxt[s])
{
deg[son]++;
}
}
queue<int> Q;
for (int s : todo)
{
if (!deg[s])
Q.push(s);
}
while (Q.size())
{
int t = Q.front();
vis[t] = 1;
Q.pop();
for (int son : nxt[t])
{
deg[son]--;
if (!deg[son])
Q.push(son);
}
}
for (int s : todo)
{
nxt[s].clear();
deg[s] = 0;
if (!vis[s])
{
cout << "No" << endl;
sign = false;
}
vis[s] = 0;
}
if (sign)
cout << "Yes" << endl;
}
}
详细
Test #1:
score: 100
Accepted
time: 8ms
memory: 45524kb
input:
6 1 1 3 2 4 10 4 3 6 2 5 1 4 3 2 4 5 5 2 5 4 6 3 3 1 4 2 3 5 6 3 5 4 5 2 6 1 4 4 3 2 5 4 4 6 2 3 3 3 2 6
output:
No Yes Yes No No No No No No Yes
result:
ok 10 token(s): yes count is 3, no count is 7
Test #2:
score: -100
Wrong Answer
time: 371ms
memory: 79120kb
input:
300000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...
output:
No Yes Yes Yes No No No No No No Yes Yes No Yes No Yes Yes Yes No No No No Yes No No Yes No No No No Yes Yes Yes No No Yes No No No No No No No Yes No Yes No Yes No No No No Yes Yes Yes No No Yes No No No No No Yes Yes No No No No No Yes Yes No Yes No No No Yes No No Yes No No Yes Yes Yes No No No N...
result:
wrong answer expected YES, found NO [90th token]