Submission #1092130

# Submission time Handle Problem Language Result Execution time Memory
1092130 2024-09-23T09:26:38 Z underwaterkillerwhale Split the sequence (APIO14_sequence) C++17
0 / 100
4 ms 2908 KB
#include <bits/stdc++.h>
#define ll              long long
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define rep(i,m,n)      for(int i=(m); i<=(n); i++)
#define reb(i,m,n)      for(int i=(m); i>=(n); i--)
#define iter(id, v)     for(auto id : v)
#define fs              first
#define se              second
#define MP              make_pair
#define pb              push_back
#define bit(msk, i)     ((msk >> i) & 1)
#define SZ(v)           (int)v.size()
#define ALL(v)          v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 1e5 + 7;
const int Mod = 1e9 + 7; ///loonf mod sai
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 350;

struct Disjoin_Set {
    int m;
    int lab[N], sz[N];

    void init (int n) {
        m = n;
        rep (i, 1, n) lab[i] = i, sz[i] = 1;
    }

    int Find (int u) {
        return u == lab[u] ? u : lab[u] = Find(lab[u]);
    }

    void Join (int u, int v) {
        u = Find(u);
        v = Find(v);
        if (u == v) return;
        if (sz[u] < sz[v]) swap(u, v);
        lab[v] = u;
        sz[u] += sz[v];
    }

}DSU;

struct Query {
    int typ, u, v;
};

int n, Q;
vector<pii> ke[N];
int par[N][25], mn[N][25], high[N];

int dd[N];
Query qr[N];

void dfs (int u, int p, int w) {
    mn[u][0] = w;
    par[u][0] = p;
    rep (i, 1, 20) {
        par[u][i] = par[par[u][i - 1]][i - 1];
        mn[u][i] = min(mn[u][i - 1], mn[par[u][i - 1]][i - 1]);
    }
    high[u] = high[p] + 1;
    iter (&id, ke[u]) {
        int v, w;
        tie(v, w) = id;
        if (v != p) {
            dfs(v, u, w);
        }
    }
}

int Min (int u, int v) {
    if (u == v) return 0;
    if (high[u] > high[v]) swap(u, v);
    int Mn = INF;
    reb (i, 20, 0) if (bit(high[v] - high[u], i)) Mn = min(Mn, mn[v][i]), v = par[v][i];
    if (v == u) return Mn;
    reb (i, 20, 0) if (par[v][i] != par[u][i]) {
        Mn = min({Mn, mn[u][i], mn[v][i]});
        v = par[v][i];
        u = par[u][i];
    }
    Mn = min({Mn, mn[u][0], mn[v][0]});
    return Mn;
}

void solution() {
    cin >> n >> Q;
    rep (i, 1, n - 1) {
        int u, v, w;
        cin >> u >> v >> w;
        ke[u].pb({v, w});
        ke[v].pb({u, w});
    }
    dfs(1, 0, 0);
    rep (i, 1, Q) {
        cin >> qr[i].typ >> qr[i].u;
        if (qr[i].typ == 1) cin >> qr[i].v;
        else dd[qr[i].u]++;
    }
    DSU.init(n);
    rep (u, 1, n)
    iter (&id, ke[u]) {
        int v = id.fs;
        if (!dd[u] && !dd[v]) {
            DSU.Join(u, v);
        }
    }
//    cout << DSU.Find(1)<<" "<<DSU.Find(2) <<"\n";
    vector<int> vec;
    reb (i, Q, 1){
        int u = qr[i].u, v = qr[i].v;
        if (qr[i].typ == 2) {
            if (dd[u] == 1) {
                iter (&id, ke[u]) {
                    int v1 = id.fs;
                    if (!dd[v1]) DSU.Join(u, v1);
                }
            }
            --dd[u];
        }
        else {
            if (u == v) {
                if (!dd[u]) vec.pb(0);
                else vec.pb(-1);
            }
            else if (DSU.Find(u) == DSU.Find(v)) {
                vec.pb(Min(u, v));
//                cout << Min(u, v) <<" asd\n";
            }
            else {
//                cout << -1 <<"\n";
                vec.pb(-1);
            }
        }
    }
    reverse(ALL(vec));
    iter (&id, vec) cout << id <<"\n";
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
    file("NETWORK");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug +8


6 2
2 1 2 2 1 1
1 3





*/

Compilation message

sequence.cpp: In function 'int Min(int, int)':
sequence.cpp:83:36: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   83 |     reb (i, 20, 0) if (bit(high[v] - high[u], i)) Mn = min(Mn, mn[v][i]), v = par[v][i];
      |                            ~~~~~~~~^~~~~~~~~
sequence.cpp:12:27: note: in definition of macro 'bit'
   12 | #define bit(msk, i)     ((msk >> i) & 1)
      |                           ^~~
sequence.cpp: In function 'int main()':
sequence.cpp:148:27: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  148 | #define file(name) freopen(name".inp","r",stdin); \
      |                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:151:5: note: in expansion of macro 'file'
  151 |     file("NETWORK");
      |     ^~~~
sequence.cpp:149:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  149 | freopen(name".out","w",stdout);
      | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:151:5: note: in expansion of macro 'file'
  151 |     file("NETWORK");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2904 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2652 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2904 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2652 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2908 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 2652 KB Unexpected end of file - int64 expected
2 Halted 0 ms 0 KB -