Submission #667221

#TimeUsernameProblemLanguageResultExecution timeMemory
667221AmirHBall Machine (BOI13_ballmachine)C++14
60.08 / 100
1100 ms40260 KiB
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
#include<bitset>
#include<queue>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<unordered_map>
#include<list>
#include<utility>
#include<stack>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
#define cl clear
#define F  first
#define S  second
#define pb push_back
#define Sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()
#define faster ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int MAX_N = 2e5 + 25;
const ll INF = 1e18;
const int inf = 1e9;
void free() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
}
vector<int> vtx[MAX_N];
set<pii> adj[MAX_N];
int mini[MAX_N];
vector<int> res;
int par[MAX_N];
set<int> notseen;
int ar[MAX_N];
int pr[MAX_N];

void dfs2(int v) {
    for(auto i : adj[v])
        dfs2(i.second);
    res.pb(v);
}
void dfs(int v, int s) {
    mini[v] = v;
    for(auto i : vtx[v]) {
        if(i != s) {
            dfs(i, v);
            mini[v] = min(mini[v], mini[i]);
            adj[v].insert({mini[i], i});
        }
    }
   // cout << v << " " << mini[v] << '\n';
}
int main() {
    faster; 
    //free();
    int n, q; cin >> n >> q;
    int st;
    for(int i = 1; i <= n; i++) {
        int x; cin >> x;
        par[i] = x;
        if(x == 0) {
            st = i;
        }
        else {
            vtx[x].pb(i);
            vtx[i].pb(x);
        }
    }
    dfs(st, 0);
    dfs2(st);
    for(int i = 0; i < res.size(); i++) {
        ar[res[i]] = i + 1;
        pr[i + 1] = res[i];
    }
    for(int i = 1; i <= n; i++)
        notseen.insert(i);
    while(q--) {
        int t, x; cin >> t >> x;
        if(t == 1) {
            int ft;
            while(x--) {
                ft = pr[*notseen.begin()];
                notseen.erase(notseen.begin());
            }
            cout << ft << '\n';
        }
        if(t == 2) {
            int sec = x;
            int d = -1;
            while(x != 0 && notseen.find(ar[x]) == notseen.end()) {
               // cout << x << '\n';
                sec = x;
                x = par[x];
                d++;
            }
            notseen.insert(ar[sec]);
            cout << d << '\n';
            // cout << "!! " << " ";
            // for(auto i : notseen)
            //     cout << i << " ";
            // cout << '\n';
        }
    }
}

Compilation message (stderr)

ballmachine.cpp: In function 'int main()':
ballmachine.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for(int i = 0; i < res.size(); i++) {
      |                    ~~^~~~~~~~~~~~
ballmachine.cpp: In function 'void free()':
ballmachine.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen("input.txt", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp:33:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |         freopen("output.txt", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
ballmachine.cpp: In function 'int main()':
ballmachine.cpp:93:27: warning: 'ft' may be used uninitialized in this function [-Wmaybe-uninitialized]
   93 |             cout << ft << '\n';
      |                           ^~~~
ballmachine.cpp:78:9: warning: 'st' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |     dfs2(st);
      |     ~~~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...