답안 #1041573

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1041573 2024-08-02T05:55:04 Z ilovveyyou Rigged Roads (NOI19_riggedroads) C++14
0 / 100
2000 ms 48072 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define clz __builtin_clzll
#define ctz __builtin_ctzll
#define popcount __builtin_popcount
#define lg(x) (63 - clz(x))

template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }
template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }
template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a)); a.resize(unique(all(a)) - a.begin());
    }

const ll oo = (ll) 1e18, inf = (ll) 1e9, mod = (ll) 1e9 + 7;
const int mxn = (int) 2e5 + 5, S = (int) 450, S2 = (int) 450, lg = (int) 17;

void add(ll &x, ll y) {
    x += y;
    if (x >= mod) x -= mod;
}

void sub(ll &x, ll y) {
    x -= y;
    if (x < 0) x += mod;
}

struct Edge {
    int u, v;
    void input() {
        cin >> u >> v;
    }
};
int n, e;
bool pos[mxn];
Edge edges[mxn];


namespace HLD {

    vector<int> g[mxn];

    struct Segtree {
        int n;
        vector<int> st, lz;
        Segtree(int n) : n(n) {
            int m = MASK(lg(n));
            st.resize(m << 2);
            lz.resize(m << 2, -1);
        }

        void upd(int i, int l, int r, int u, int v, int x) {
            if (l > v || r < u) return;
            if (l >= u && r <= v) {

            }
        }
    };

    int h[mxn];
    int par[mxn][lg + 1];

    void dfs(int u = 1, int p = 0) {

        for (int i = 1; i <= lg; ++i)
            par[u][i] = par[par[u][i-1]][i-1];

        for (int v : g[u]) if (v != p) {
            h[v] = h[u] + 1;
            par[v][0] = u;
            dfs(v, u);
        }

        return;
    }
    int find_ancestor(int u, int k) {
        for (; k; k -= k & -k)
            u = par[u][ctz(k)];
        return u;
    }
    int lca(int u, int v) {
        if (h[u] < h[v])
            swap(u, v);
        u = find_ancestor(u, h[u] - h[v]);
        if (u == v) return u;
        for (int i = lg(h[u]); i >= 0; --i) if (par[u][i] != par[v][i]) {
            u = par[u][i]; v = par[v][i];
        }
        return par[u][0];
    }

    int value[mxn];


    int getPath(int u, int v) {
        int p = lca(u, v); 
        int ans = 0;
        while (u != p) {
            ans += value[u];
            u = par[u][0];
        }
        while (v != p) {
            ans += value[v];
            v = par[v][0];
        }
        return ans;
    }

    void updPath(int u, int v, int x) {
        int p = lca(u, v);
        // cout << "UPD " << u << ' ' << v << ' ' << p << ' ' << x << '\n';
        value[p] = 0;
        while (u != p) {
            value[u] = x;
            u = par[u][0];
        }
        while (v != p) {
            value[v] = x;
            v = par[v][0];
        }
    }   
    int getEmpt(int u, int v) {
        // cout << u << ' ' << v << ' ' << h[u] + h[v] - 2 * h[lca(u, v)] << ' ' << getPath(u, v) << '\n';
        return h[u] + h[v] - 2 * h[lca(u, v)] - getPath(u, v);
    }

    void build() {

        for (int i = 1; i <= e; ++i) if (pos[i]) {
            int u = edges[i].u, v = edges[i].v;
            g[u].push_back(v); g[v].push_back(u);
        }

        dfs();
        for (int i = 1; i <= e; ++i) if (pos[i]) {
            int u = edges[i].u, v = edges[i].v;
            if (par[u][0] != v) swap(u, v);
        }

        return;
    }
}

void solve() {
    cin >> n >> e;
    for (int i = 1; i <= e; ++i) 
        edges[i].input();
    for (int i = 1; i < n; ++i) {
        int p; cin >> p;
        pos[p] = true;
    }

    HLD::build(); 

    set<int> s;
    for (int i = 1; i <= e; ++i)
        s.insert(i);
    int cnt = 0;
    vector<int> ans(n+1);
 
    for (int i = 1; i <= e; ++i) {
        int u = edges[i].u, v = edges[i].v;
        // for (int i = 1; i <= e; ++i) if (pos[i])
        //     cout << HLD::value[edges[i].v] << ' ';
        // cout << '\n';
        // cout << u << ' ' << v << '\n';
        if (pos[i]) {
            // cout << i << ' ' << HLD::getPath(v, v) << '\n';
            if (!HLD::getPath(u, v)) {
                // cout << i << ' ' << '\n';
                ans[i] = ++cnt;
                s.erase(cnt);
            }
            else {
                ans[i] = *s.begin();
                s.erase(s.begin());
            }
            HLD::updPath(u, v, 1);
        }
        else {
            int sum = HLD::getEmpt(u, v);
            // cout << i << ' ' << sum << "\n";
            ans[i] = ++cnt + sum;
            s.erase(ans[i]);
            cnt = ans[i];
            HLD::updPath(u, v, 1);
        }
    }

    for (int i = 1; i <= e; ++i)
        cout << ans[i] << ' ';

    return;
}


int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define TASK "task"
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Compilation message

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:222:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  222 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:223:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  223 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 61 ms 48072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2052 ms 32596 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 25 ms 15964 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 100 ms 41428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Incorrect 1 ms 6492 KB Output isn't correct
4 Halted 0 ms 0 KB -