# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
339418 | tengiz05 | 특수한 그래프 (IZhO13_specialg) | C++17 | 1099 ms | 5992 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define PI acos(-1)
#define ld long double
template<class T> bool ckmin(T& a, const T& b) {return a>b? a=b, true:false;}
template<class T> bool ckmax(T& a, const T& b) {return a<b? a=b, true:false;}
const int mod = 1e9+7, N = 1e5+5;
int msb(int val){return sizeof(int)*8-__builtin_clzll(val)-1;}
int n, m, k;
int nex[N];
int ans[N];
bool black[N];
int dist;
int looking_for;
bool found;
vector<bool> used;
void dfs(int u, int d=0){
if(u == looking_for){
dist = d;
found = true;
return;
}
used[u] = true;
if(!used[nex[u]])dfs(nex[u],d+1);
}
void solve(int test_case){
int i, j, numq;
cin >> n;
for(i=1;i<=n;i++){
cin >> nex[i];
}
vector<tuple<int,int,int>> Q;
cin >> numq;
for(i=0;i<numq;i++){
int typ;
cin >> typ;
if(typ == 1){
int t;
cin >> t;
Q.pb(make_tuple(typ,t,0));
black[t] = true;
}else {
int l, r;
cin >> l >> r;
Q.pb(make_tuple(typ,l,r));
}
}
// reverse(all(Q));
for(i=0;i<numq;i++){
found = false;
auto [typ, l, r] = Q[i];
if(typ == 2){
used.assign(n+1, 0);
looking_for = r;
dfs(l);
if(found)ans[i] = dist;
else ans[i] = -1;
}else {
nex[l] = 0;
}
}
for(i=0;i<numq;i++){
if(get<0>(Q[i]) == 2)cout << ans[i] << '\n';
}
return;
}
signed main(){
FASTIO;
#define MULTITEST 0
#if MULTITEST
int _T;
cin >> _T;
for(int T_CASE = 1; T_CASE <= _T; T_CASE++)
solve(T_CASE);
#else
solve(1);
#endif
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |