AcWing L2_040. PAT::哲哲打游戏
原题链接
简单
作者:
NEGU
,
2025-04-18 15:41:17
· 新疆
,
所有人可见
,
阅读 3
PAT::哲哲打游戏
- 用vector容器当做邻接表。因为这里不需要遍历邻接表中所有元素,只需要查询某个元素在或不在,这时候用vector当邻接表就比数组模拟邻接表要高效的多。
Solution:vector模拟邻接表
C++ 代码
#pragma GCC optimize(2)
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5+10, M = 110;
int save[M];
vector<int> g[N];
int n,m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i<=n; ++i){
int k;
cin >> k;
while(k--){
int x;
cin >> x;
g[i].push_back(x);
}
}
int now = 1;
for(int i = 0; i<m; ++i){
int a,b;
cin >>a >> b;
if(a == 1){
save[b] = now;
cout << now << endl;
}
else if(a == 0){
now = g[now][b-1];
}
else{
now = save[b];
}
}
cout << now;
return 0;
}