# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
992048 | yeediot | Special graph (IZhO13_specialg) | C++17 | 2 ms | 4440 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#ifdef local
void CHECK();
void setio(){
freopen("/Users/iantsai/cpp/input.txt","r",stdin);
freopen("/Users/iantsai/cpp/output.txt","w",stdout);
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
#define TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio();
const int mxn = 1e5+5;
int n, p[mxn], dis[mxn], in[mxn], out[mxn], timer, vis[mxn], t[mxn];
vector<int>adj[mxn];
struct BIT{
int bit[mxn];
void init(){
for(int i=0;i<=n;i++){
bit[i] = 0;
}
}
void modify(int p, int v){
for(;p<=n;p+=p&-p){
bit[p] += v;
}
}
int query(int p){
int res=0;
for(;p;p-=p&-p){
res += bit[p];
}
return res;
}
}bt;
void dfs(int v, int top, int d){
if(vis[v] == 1){
return ;
}
vis[v] = 1;
in[v] = ++timer;
t[v] = top;
dis[v] = d;
for(auto u : adj[v]){
dfs(u, top, d+1);
}
out[v] = timer;
}
signed main(){
TOI_is_so_de;
cin >> n;
bt.init();
for(int i=1;i<=n;i++){
cin >> p[i];
if(p[i] != 0){
adj[p[i]].pb(i);
}
}
for(int i=1;i<=n;i++){
if(vis[i]) continue;
int cur = i;
while(p[cur] != -1 and vis[cur] == 0){
vis[cur] = -1;
cur = p[cur];
}
dfs(cur, cur, 0);
debug(cur);
}
for(int i=1;i<=n;i++){
debug(in[i], out[i]);
}
int q;
cin >> q;
while(q--){
int ty;
cin >> ty;
if(ty == 1){
int v;
cin >> v;
p[v] = -1;
bt.modify(in[v], 1);
bt.modify(out[v]+1, -1);
}
else{
int v, u, ans = 0;
cin >> v >> u;
if(in[u] <= in[v] and in[v]-1 <= out[u]){
ans = bt.query(in[v])-bt.query(in[u]) == 0 ? dis[v]-dis[u] : -1;
}
else{
if(bt.query(in[v]) > 0){
ans = -1;
}
else{
ans = dis[v];
v = p[t[v]];
debug(v);
if(v == -1 or in[v] < in[u] or in[v] > out[u] or bt.query(in[v])-bt.query(in[u]) != 0){
ans = -1;
}
else{
ans += dis[v]-dis[u]+1;
}
}
}
cout << ans << '\n';
}
}
#ifdef local
CHECK();
#endif
}
/*
input:
[in[i], out[i]] = [2, 3]
[in[i], out[i]] = [3, 4]
[in[i], out[i]] = [1, 4]
[in[i], out[i]] = [0, 6]
[in[i], out[i]] = [5, 6]
[in[i], out[i]] = [4, 6
*/
#ifdef local
void CHECK(){
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
function<bool(string,string)> compareFiles = [](string p1, string p2)->bool {
std::ifstream file1(p1);
std::ifstream file2(p2);
if (!file1.is_open() || !file2.is_open())return false;
std::string line1, line2;
while (getline(file1, line1) && getline(file2, line2)) {
if (line1 != line2)return false;
}
int cnta=0,cntb=0;
while(getline(file1,line1))cnta++;
while(getline(file2,line2))cntb++;
return cntb-cnta<=1;
};
bool check = compareFiles("output.txt","expected.txt");
if (check) cerr<<"ACCEPTED\n";
else cerr<<"WRONG ANSWER!\n";
}
#else
#endif
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |