QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#416537#8720. BFS 序 0Afterlife#WA 627ms101588kbC++172.9kb2024-05-21 22:27:502024-05-21 22:27:51

Judging History

你现在查看的是最新测评结果

  • [2024-05-21 22:27:51]
  • 评测
  • 测评结果:WA
  • 用时:627ms
  • 内存:101588kb
  • [2024-05-21 22:27:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int N=1e6+1e3+7;

int n,st[N],ed[N],dep[N],dc;

vector<int> g[N];

int anc[N][21];

void dfs(int x,int f)
{
    st[x]=++dc;
    anc[x][0]=f;
    for(int j=1;j<=20;j++)
        anc[x][j]=anc[anc[x][j-1]][j-1];
    for(auto v:g[x])
    {
        if(v==f)
            continue;
        dep[v]=dep[x]+1;
        dfs(v,x);
    }
    ed[x]=dc;
}

int lca(int x,int y)
{
    if(dep[x]<dep[y])
        swap(x,y);
    for(int i=20;i>=0;i--)
        if(dep[x]-(1<<i)>=dep[y])
            x=anc[x][i];
    if(x==y)
        return x;
    for(int i=20;i>=0;i--)
        if(anc[x][i]!=anc[y][i])
            x=anc[x][i],y=anc[y][i];
    return anc[x][0];
}

namespace SOLVE {
  int m, i, a[N], q[N], t, tot;
  int pos[N];

  void dfs(int x)
  {
    for(auto v:g[x])
        dfs(v),pos[x]=min(pos[x],pos[v]);
  }
  bool vip[N], vis[N];
  bool cmp(int x, int y) { return st[x] < st[y]; }
  vector<int> g[N];
  void solve(vector<int> v,vector<int> seq) {
    int la=-1;
    bool ok=1;
    for(auto x:seq)
    {
        if(dep[x]<la)
            ok=0;
        la=dep[x];
    }
    if (!v.size())
      return;
    vis[1] = vip[1] = a[1] = 1;
    m = v.size();
    for (tot = ++m, i = 2; i <= m; i++)
      a[i] = v.back(), v.pop_back(), vis[a[i]] = vip[a[i]] = 1;
    sort(a + 1, a + m + 1, cmp);
    int x;
    for (i = 1; i < m; i++)
      if (!vis[x = lca(a[i], a[i + 1])])
        vis[a[++tot] = x] = 1;
    vector<int> use;
    m = tot, sort(a + 1, a + m + 1, cmp);
    for (q[t = 1] = 1, i = 2; i <= m; q[++t] = a[i++]) {
      while (st[a[i]] < st[q[t]] || ed[a[i]] > ed[q[t]]) // dfs order
        t--;
      if (q[t] != a[i])
        g[q[t]].push_back(a[i]);
      use.push_back(q[t]);
      use.push_back(a[i]);
    }
    for(auto x:use)
        pos[x]=1e9;
    // solve the problem
    dfs(1);
    for(int i=0;i<seq.size();i++)
        pos[seq[i]]=i;
    queue<int> q;
    q.push(1);
    auto it=seq.begin();
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        if(it!=seq.end()&&x==*it)
            it++;
        sort(g[x].begin(),g[x].end(),[&](const int &a,const int &b){return pos[a]<pos[b];});
        for(auto v:g[x])
            q.push(v);
    }
    ok&=it==seq.end();
    if(ok)
        cout<<"Yes\n";
    else
        cout<<"No\n";
    for (auto x : use)
      vis[x] = vip[x] = 0, g[x].clear();
    return;
  }
} // namespace SOLVE

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        int f;
        cin>>f;
        g[f].push_back(i);
    }
    dfs(1,0);
    int m;
    cin>>m;
    while(m--)
    {
        int k;
        cin>>k;
        vector<int> s;
        while(k--)
        {
            int x;
            cin>>x;
            s.push_back(x);
        }
        SOLVE::solve(s,s);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 64672kb

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: 627ms
memory: 101588kb

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
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
Yes
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
Yes
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
No
Yes
No
No
No
Yes
No
No
No
No
No
No
...

result:

wrong answer expected YES, found NO [2nd token]