Submission #798401

# Submission time Handle Problem Language Result Execution time Memory
798401 2023-07-30T16:41:56 Z PixelCat Simurgh (IOI17_simurgh) C++14
51 / 100
300 ms 4420 KB
#include "simurgh.h"

#ifdef NYAOWO
#include "grader.cpp"
#endif

#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define eb emplace_back
// #define int LL
using namespace std;
using i32 = int32_t;
using LL = long long;
using pii = pair<int, int>;

const int MAXN = 500;

vector<pii> adj[MAXN + 10];  // { node, edge id }

struct Tree {
    set<int> edge;
    set<int> suseid;
    set<int> egg;
    set<int> epote;
    int par[MAXN + 10];
    int dep[MAXN + 10];
    int peid[MAXN + 10];
    bool good[MAXN + 10];
    bool sussy[MAXN + 10];
    vector<int> get_edges() {
        vector<int> ids;
        for(auto &i:edge) ids.eb(i);
        return ids;
    }
    int query() {
        // cerr << "query: ";
        // out();
        return count_common_roads(get_edges());
    }
    void dfs(int x, int p, int d, int pe, bool tree_edge_only = true) {
        if(p < 0) {
            memset(dep, 0, sizeof(dep));
        }
        par[x] = p;
        dep[x] = d;
        peid[x] = pe;
        good[x] = !suseid.count(pe);
        sussy[x] = !good[x];
        for(auto &i:adj[x]) if(i.F != p && dep[i.F] == 0) {
            if(tree_edge_only && !edge.count(i.S)) continue;
            else if(!tree_edge_only) {
                edge.insert(i.S);
                suseid.insert(i.S);
            }
            dfs(i.F, x, d + 1, i.S, tree_edge_only);
            good[x] = good[x] && good[i.F];
            sussy[x] = sussy[x] && good[i.F];
        }
    }
    void collect(int x, int p, set<int> &subtree) {
        subtree.insert(x);
        for(auto &i:adj[x]) if(i.F != p && edge.count(i.S)) {
            collect(i.F, x, subtree);
        }
    }
    void build_tree(int N) {
        For(_, 1, N - 1) {
            dfs(0, -1, 1, -1);
            // For(i, 0, N - 1) cerr << par[i] << " \n"[i == N - 1];
            // For(i, 0, N - 1) cerr << peid[i] << " \n"[i == N - 1];

            int x = -1;
            For(i, 0, N - 1) if(sussy[i]) x = i;
            assert(x >= 0);
            suseid.erase(peid[x]);
            int cnt;

            set<int> subtree;
            collect(x, par[x], subtree);
            vector<int> eids;
            for(auto &a:subtree) for(auto &e:adj[a]) {
                if(subtree.count(e.F) || egg.count(e.S)) continue;
                if(epote.count(e.S)) {
                    edge.erase(peid[x]);
                    edge.insert(e.S);
                    goto FOUND;
                }
                eids.eb(e.S);
            }
            
            cnt = query();
            edge.erase(peid[x]);
            For(i, 0, sz(eids) - 1) {
                edge.insert(eids[i]);
                int cnt2 = query();
                if(cnt < cnt2) {
                    For(j, 0, i - 1) egg.insert(eids[j]);
                    egg.insert(peid[x]);
                    epote.insert(eids[i]);
                    goto FOUND;
                } else if(cnt > cnt2) {
                    For(j, 0, i - 1) epote.insert(eids[j]);
                    epote.insert(peid[x]);
                    egg.insert(eids[i]);
                    edge.erase(eids[i]);
                    edge.insert(peid[x]);
                    goto FOUND;
                }
                edge.erase(eids[i]);
            }
            edge.insert(peid[x]);
            for(auto &i:eids) epote.insert(i);
            FOUND:;
            out();
        }
    }
    void out() {
        #ifdef NYAOWO
        cerr << "tree: ";
        for(auto &i:get_edges()) {
            cerr << i << " ";
        }
        cerr << "\n";
        #endif
    }
} tr;

vector<int> find_roads(int N, vector<int> U, vector<int> V) {
    int M = sz(U);
    For(i, 0, M - 1) {
        adj[U[i]].eb(V[i], i);
        adj[V[i]].eb(U[i], i);
    }
    tr.dfs(0, -1, 1, -1, false);
    tr.build_tree(N);
    return tr.get_edges();

    // vector<int> r(n - 1);
    // for(int i = 0; i < n - 1; i++)
    //     r[i] = i;
    // int common = count_common_roads(r);
    // if(common == n - 1)
    //     return r;
    // r[0] = n - 1;
    // return r;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 316 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 1 ms 320 KB correct
13 Correct 1 ms 316 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 316 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 1 ms 320 KB correct
13 Correct 1 ms 316 KB correct
14 Correct 5 ms 340 KB correct
15 Correct 4 ms 340 KB correct
16 Correct 3 ms 324 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 312 KB correct
19 Correct 3 ms 340 KB correct
20 Correct 3 ms 340 KB correct
21 Correct 3 ms 340 KB correct
22 Correct 3 ms 312 KB correct
23 Correct 2 ms 356 KB correct
24 Correct 3 ms 336 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 2 ms 340 KB correct
33 Correct 2 ms 340 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 316 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 1 ms 320 KB correct
13 Correct 1 ms 316 KB correct
14 Correct 5 ms 340 KB correct
15 Correct 4 ms 340 KB correct
16 Correct 3 ms 324 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 312 KB correct
19 Correct 3 ms 340 KB correct
20 Correct 3 ms 340 KB correct
21 Correct 3 ms 340 KB correct
22 Correct 3 ms 312 KB correct
23 Correct 2 ms 356 KB correct
24 Correct 3 ms 336 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 2 ms 340 KB correct
33 Correct 2 ms 340 KB correct
34 Correct 290 ms 2724 KB correct
35 Correct 285 ms 2716 KB correct
36 Correct 190 ms 2060 KB correct
37 Correct 14 ms 468 KB correct
38 Correct 300 ms 2660 KB correct
39 Correct 225 ms 2372 KB correct
40 Correct 184 ms 1988 KB correct
41 Correct 252 ms 2600 KB correct
42 Correct 258 ms 2632 KB correct
43 Correct 108 ms 1512 KB correct
44 Correct 74 ms 1176 KB correct
45 Correct 94 ms 1304 KB correct
46 Correct 72 ms 1096 KB correct
47 Correct 30 ms 668 KB correct
48 Correct 7 ms 316 KB correct
49 Correct 10 ms 444 KB correct
50 Correct 29 ms 632 KB correct
51 Correct 76 ms 1108 KB correct
52 Correct 101 ms 1188 KB correct
53 Correct 67 ms 1024 KB correct
54 Correct 97 ms 1560 KB correct
55 Correct 86 ms 1340 KB correct
56 Correct 97 ms 1352 KB correct
57 Correct 89 ms 1336 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB correct
2 Correct 0 ms 212 KB correct
3 Incorrect 124 ms 4420 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB correct
2 Correct 1 ms 212 KB correct
3 Correct 1 ms 212 KB correct
4 Correct 1 ms 212 KB correct
5 Correct 0 ms 212 KB correct
6 Correct 0 ms 212 KB correct
7 Correct 0 ms 212 KB correct
8 Correct 0 ms 212 KB correct
9 Correct 1 ms 212 KB correct
10 Correct 0 ms 316 KB correct
11 Correct 1 ms 212 KB correct
12 Correct 1 ms 320 KB correct
13 Correct 1 ms 316 KB correct
14 Correct 5 ms 340 KB correct
15 Correct 4 ms 340 KB correct
16 Correct 3 ms 324 KB correct
17 Correct 3 ms 340 KB correct
18 Correct 2 ms 312 KB correct
19 Correct 3 ms 340 KB correct
20 Correct 3 ms 340 KB correct
21 Correct 3 ms 340 KB correct
22 Correct 3 ms 312 KB correct
23 Correct 2 ms 356 KB correct
24 Correct 3 ms 336 KB correct
25 Correct 1 ms 212 KB correct
26 Correct 2 ms 340 KB correct
27 Correct 2 ms 340 KB correct
28 Correct 1 ms 340 KB correct
29 Correct 2 ms 340 KB correct
30 Correct 2 ms 340 KB correct
31 Correct 2 ms 340 KB correct
32 Correct 2 ms 340 KB correct
33 Correct 2 ms 340 KB correct
34 Correct 290 ms 2724 KB correct
35 Correct 285 ms 2716 KB correct
36 Correct 190 ms 2060 KB correct
37 Correct 14 ms 468 KB correct
38 Correct 300 ms 2660 KB correct
39 Correct 225 ms 2372 KB correct
40 Correct 184 ms 1988 KB correct
41 Correct 252 ms 2600 KB correct
42 Correct 258 ms 2632 KB correct
43 Correct 108 ms 1512 KB correct
44 Correct 74 ms 1176 KB correct
45 Correct 94 ms 1304 KB correct
46 Correct 72 ms 1096 KB correct
47 Correct 30 ms 668 KB correct
48 Correct 7 ms 316 KB correct
49 Correct 10 ms 444 KB correct
50 Correct 29 ms 632 KB correct
51 Correct 76 ms 1108 KB correct
52 Correct 101 ms 1188 KB correct
53 Correct 67 ms 1024 KB correct
54 Correct 97 ms 1560 KB correct
55 Correct 86 ms 1340 KB correct
56 Correct 97 ms 1352 KB correct
57 Correct 89 ms 1336 KB correct
58 Correct 1 ms 212 KB correct
59 Correct 0 ms 212 KB correct
60 Incorrect 124 ms 4420 KB WA in grader: NO
61 Halted 0 ms 0 KB -