QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469096#8720. BFS 序 0JEdwardWA 256ms75876kbC++172.0kb2024-07-09 13:46:132024-07-09 13:46:14

Judging History

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

  • [2024-07-09 13:46:14]
  • 评测
  • 测评结果:WA
  • 用时:256ms
  • 内存:75876kb
  • [2024-07-09 13:46:13]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define ls(x) (x<<1)
#define rs(x) (x<<1|1) 
#define here system("pause")
using namespace std;

const int N = 3e5+5;
const int mod = 1e9+7;
const int INF = 1e15+7;
const double eps = 1e-6;
inline int pow(int a, int b, int p){ int res = 1%p; while(b){ if(b&1) res=res*a%p; a=a*a%p, b>>=1;} return res;}
inline int inv(int a, int p){ return pow(a,p-2,p)%p;}

int n, q;
vector<int> pd[N];
int f[N][20], dep[N], ask[N<<1], indeg[N];

inline void sol(){
	cin >> n;
	for(int i=2;i<=n;i++){
		cin >> f[i][0];
	}
	for(int i=1;i<=n;i++){
		dep[i]=dep[f[i][0]]+1;
	}
	for(int i=1;i<20;i++){
		for(int j=1;j<=n;j++){
			f[i][j]=f[i-1][f[i-1][j]];
		}
	}
		
	cin >> q;
	while(q--){
		int num;
		cin >> num;
		bool flag = 1;
		
		set<int> dots;
		for(int i=1;i<=num;i++){
			cin >> ask[i];
			if(ask[i] > n) flag = 0;
			dots.insert(ask[i]);
			if(i!=1 && dep[ask[i]] < dep[ask[i-1]]) flag = 0;
		}
		
		if(!flag || dots.size()!=num){
			cout << "No\n";
			continue;
		}
		
		set<int> st;
		for(int i=2;i<=num;i++){
			if(dep[ask[i-1]]==dep[ask[i]]){
				int a = ask[i-1], b = ask[i];
				for(int j=19;j>=0;j--){
					if(f[j][a] != f[j][b]) a = f[j][a], b = f[j][b];
				}
				
				st.insert(a), st.insert(b);
				pd[a].push_back(b);
				++indeg[b];
			}
		}
		
		queue<int> q;
		for(int k:st){
			if(!indeg[k]) q.push(k);
		}
		
		while(q.size()){
			int now = q.front();
			q.pop();
			for(int k:pd[now]){
				--indeg[k];
				if(!indeg[k]) q.push(k);
			}
		}
		
		for(int k:st){
			if(indeg[k]) flag = 0;
		}
		
		if(!flag) cout << "No\n";
		else cout << "Yes\n";
		
		for(int k:st){
			indeg[k] = 0;
			pd[k].clear();
		}
	}
}
/*

11
1 1 1 1 1 1 1 1 1 2
6
3 2 5 7
2 2 1
5 2 3 5 7 9
4 2 3 2 7
3 2 3 2
6 1 2 7 9 41 10
*/
signed main(){
	IOS;
	int tc = 1;
	while(tc--){
		sol();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 16004kb

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: 256ms
memory: 75876kb

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

result:

wrong answer expected NO, found YES [87th token]