Submission #877179

# Submission time Handle Problem Language Result Execution time Memory
877179 2023-11-23T02:52:02 Z logn Rigged Roads (NOI19_riggedroads) C++14
31 / 100
2000 ms 59008 KB
#include <bits/stdc++.h>                                                                                                                                                                                      //Logm
using namespace std;
#define int long long
#define II pair<int,int>
#define fi first
#define se second

const int N = 3e5+5;
int n, m;

II e[N];
int idx[N];
bool spec[N];

int par[N], rak[N];
void init(int n) {
    for (int i = 1; i <= n; ++i) {
        par[i] = i;
        rak[i] = 0;
    }
}
int find(int u) {
    if (par[u] != u) par[u] = find(par[u]);
    return par[u];
}
bool join(int u, int v) {
    u = find(u);
    v = find(v);
    if (u == v) return 0;
    if (rak[u] == rak[v]) ++rak[u];
    if (rak[u] < rak[v]) par[u] = v;
    else par[v] = u;
    return 1;
}

namespace sub1 {
    void solve() {
        int p[m];
        for (int i = 0; i < m; ++i) p[i] = i + 1;
        
        vector<int> ans;
        do {
            init(n);
            bool flag = 0;
            for (int i = 0; i < m; ++i) {
                if (spec[p[i]]) {
                    if (!join(e[p[i]].fi, e[p[i]].se)) {
                        flag = 1;
                        break;
                    }
                } else {
                    if (join(e[p[i]].fi, e[p[i]].se)) {
                        flag = 1;
                        break;
                    }
                }
            }
            if (flag) continue;

            vector<int> res(m);
            for (int i = 0; i < m; ++i)
                res[p[i] - 1] = i + 1;
            
            if (ans.empty()) ans = res;
            else ans = min(ans, res);
        } while (next_permutation(p, p + m));
        
        for (int i: ans) cout << i << ' ';
    }
}

namespace sub2 {
    vector<II> adj[N];
    int ans[N];

    int index;
    II par[N];
    int dep[N];
     
    void build(int u) {
        for (II ed: adj[u]) {
            int v = ed.fi, id = ed.se;
            if (v != par[u].fi) {
                par[v] = II(u, id);
                dep[v] = dep[u] + 1;
                build(v);
            }
        }
    }
    void getpath(int u, int v, vector<int> &path) {
        if (dep[u] < dep[v]) swap(u, v);

        while (dep[u] > dep[v]) {
            path.push_back(par[u].se);
            u = par[u].fi;
        }
        if (u == v) return;

        while (u != v) {
            path.push_back(par[u].se);
            u = par[u].fi;
            path.push_back(par[v].se);
            v = par[v].fi;
        }
    }

    void solve() {
        for (int i = 1; i < n; ++i) {
            int u = e[idx[i]].fi, v = e[idx[i]].se;
            adj[u].push_back(II(v, idx[i]));
            adj[v].push_back(II(u, idx[i]));
        }

        build(1);

        for (int i = 1; i <= m; ++i) {
            if (spec[i]) {
                if (ans[i] == 0) ans[i] = ++index;
            } else {
                int u = e[i].fi, v = e[i].se;
                // đường đi từ u đến v trên cây khung phải được đánh số nhỏ hơn
                vector<int> path;
                getpath(u, v, path);
                sort(path.begin(), path.end());
                for (int x: path) {
                    if (ans[x] == 0) ans[x] = ++index;
                }
                ans[i] = ++index;
            }
        }

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

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("ZONING.inp", "r")) {
        freopen("ZONING.inp", "r", stdin);
        freopen("ZONING.out", "w", stdout);
    }

    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int u, v; cin >> u >> v;
        e[i] = II(u, v);
    }

    for (int i = 1; i < n; ++i) {
        cin >> idx[i];
        spec[idx[i]] = 1;
    }

    if (m <= 9) sub1::solve();

    sub2::solve();

    return 0;
}

Compilation message

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  140 |         freopen("ZONING.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  141 |         freopen("ZONING.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 14684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 14684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 33 ms 25540 KB Output is correct
2 Correct 64 ms 30916 KB Output is correct
3 Correct 69 ms 22220 KB Output is correct
4 Correct 91 ms 41272 KB Output is correct
5 Correct 97 ms 43720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2066 ms 34744 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 150 ms 48880 KB Output is correct
2 Correct 169 ms 55332 KB Output is correct
3 Correct 38 ms 27480 KB Output is correct
4 Correct 61 ms 32980 KB Output is correct
5 Correct 197 ms 59008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 41148 KB Output is correct
2 Correct 56 ms 32996 KB Output is correct
3 Correct 184 ms 51152 KB Output is correct
4 Correct 179 ms 47640 KB Output is correct
5 Correct 13 ms 17472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 14684 KB Output isn't correct
2 Halted 0 ms 0 KB -