QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#423421#8720. BFS 序 0CipherxzcWA 195ms46484kbC++202.2kb2024-05-28 00:12:152024-05-28 00:12:16

Judging History

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

  • [2024-05-28 00:12:16]
  • 评测
  • 测评结果:WA
  • 用时:195ms
  • 内存:46484kb
  • [2024-05-28 00:12:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'

inline int lg(int x){
    return 31 - __builtin_clz(x); 
}

const int N = 3e5 + 5, M = 5e5 + 5;
int n, q, root = 1, dep[N], m, a[M];
vector<int> e[N], e2[N];
map<int, bool> vis;
bool flag;

inline void add(int u, int v){
    e[u].push_back(v);
}

inline void add2(int u, int v){
	//cout << u << ' ' << v << endl;
	e[u].push_back(v);
	vis[u] = vis[v] = false;
}

namespace LCA{
	int fa[20][N];

	void dfs(int p){
		for (int q : e[p]){
			if (q == fa[0][p]){
				continue;
			}
			dep[q] = dep[p] + 1;
			fa[0][q] = p;
			dfs(q);
		}
	}

	void init(){
		dfs(root);

		for (int j = 1; j <= lg(n - 1); j++){
			for (int i = 1; i <= n; i++){
				fa[j][i] = fa[j - 1][fa[j - 1][i]];
			}
		}
	}

	int lca(int u, int v){
		if (dep[u] < dep[v]){
			swap(u, v);
		}
		while (dep[u] - dep[v]){
			u = fa[lg(dep[u] - dep[v])][u];
		}
		if (u == v){
			return u;
		}
		for (int i = lg(dep[u]); i >= 0; i--){
			if (fa[i][u] != fa[i][v]){
				u = fa[i][u]; v = fa[i][v];
			}
		}
		return fa[0][u];
	}

	inline int get(int u, int v){
		while (dep[u] - dep[v] > 1){
			u = fa[lg(dep[u] - dep[v] - 1)][u];
		}
		return u;
	}
}

void dfs(int p){
	vis[p] = true;
	for (int q : e2[p]){
		if (vis[q]){
			flag = false;
			break;
		}else{
			dfs(q);
		}
	}
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;
	for (int i = 2, fa; i <= n; i++){
		cin >> fa;
		add(fa, i);
	}

	LCA::init();

	cin >> q;
	while (q--){
		for (auto [x, y] : vis){
			e2[x].clear();
		}
		vis.clear();

		cin >> m;
		for (int i = 1; i <= m; i++){
			cin >> a[i];
		}

		flag = true;
		for (int i = 2; i <= m && flag; i++){
			int u = a[i - 1], v = a[i];
			if (dep[u] > dep[v]){
				//cout << "! " << u << ' ' << v << endl;
				flag = false;
			}else if (dep[u] == dep[v]){
				int w = LCA::lca(u, v);
				int x = LCA::get(u, w), y = LCA::get(v, w);
				add2(x, y);
			}
		}

		if (flag){
			for (auto [x, y] : vis){
				if (!y){
					dfs(y);
				}
			}
		}

		if (flag){
			cout << "Yes" << endl;
		}else{
			cout << "No" << endl;
		}
	}

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9872kb

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: 195ms
memory: 46484kb

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:

Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
...

result:

wrong answer expected NO, found YES [1st token]