Submission #1038574

# Submission time Handle Problem Language Result Execution time Memory
1038574 2024-07-30T02:19:35 Z krakenn Railway (BOI17_railway) C++17
100 / 100
72 ms 25288 KB
#include "bits/stdc++.h"
using namespace std;

#define ll long long
#define ld long double
#define ii pair<int,int>
#define pll pair<ll,ll>
#define lll tuple<ll,ll,ll>
#define iii tuple<int,int,int>
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define dbg(v) \
cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;

#define m1(x) template<class T, class... U> void x(T&& a, U&&... b)
#define m2(x) (int[]){(x forward<U>(b),0)...}
m1(out) { cout << forward<T>(a);  m2(cout << " " <<); cout << "\n"; }
m1(in) { cin >> forward<T>(a); m2(cin >>); }

// nmax = 1e5

int tin[100004], tout[100004], timer = 0, p_edge[100004];
vector<ii> adj[100004];

void dfs2(int node = 1, int parent = 0) {
    tin[node] = ++timer;
    for (pair<int, int> &i : adj[node])
        if (i.first != parent) {
            p_edge[i.first] = i.second;
            dfs2(i.first, node);
        }
    tout[node] = timer;
}

template <typename T> class BinaryLift {
    public:
    int lgmax = 20;
    int nmax = 0;
    vector<vector<int>> far;
    vector<int> level;

    BinaryLift(int n): lgmax(__bit_width(n)+1), nmax(n), far(lgmax+1, vector<int>(nmax+3, 0)),
        level(nmax+3) {
        level[1] = 0;
        dfs(1, -1);

        for(int h = 1; h <= lgmax; h++) {
            for(int i = 1; i <= nmax; i++) {
                far[h][i] = far[h - 1][far[h - 1][i]];
            }
        }
    }

    int getLca(int x, int y) {
        if(level[x] < level[y]) swap(x, y);
        for(int h = lgmax; h >= 0; h--)
            if(level[y] + (1 << h) <= level[x]){
                x = far[h][x];
            }      
        if(x == y) return x;
        for(int h = lgmax; h >= 0; h--)
            if(far[h][x] != far[h][y]) {
                x = far[h][x];
                y = far[h][y];
            }
        return far[0][x];
    }

    void dfs(int now, int p) {
        for(auto &[v, cost]: adj[now]) {
            if(v != p) {
                // init
                far[0][v] = now;
                level[v] = level[now] + 1;
                dfs(v, now);
            }
        }
    }

    int kth(int node, int k){
        // beware k too high!
        int x = node;
        for(int h = lgmax; h >= 0; h--){
            if(k & (1 << h)){
                x = far[h][x];
            }
        }
        return x;
    }
};

class FenwickTree {
    private:
        vector<ll> f;
    
    public:
        int LSOne(int i){return (i&(-i));}
        FenwickTree(int n){
            f.assign(n+1, 0);
        }

        void update(int x, ll v){
            for(int i = x; i <= (int)f.size()-1; i += LSOne(i)){
                f[i] += v;
            }
        }

        void update(int l, int r, ll v){
            update(l, v);
            update(r+1, -v);
        }

        ll print(int k){
            ll sum = 0;
            for(int i = k; i >= 1; i -= LSOne(i)){
                sum += f[i];
            }

            return sum;
        }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n,m,k; in(n,m,k);

    forn(i,1,n-1){
        int a, b; in(a, b);
        adj[a].emplace_back(b, i);
        adj[b].emplace_back(a, i);
    }

    dfs2();
    BinaryLift<int> BL(n);
    FenwickTree FT(n);


    forn(i,1,m){
        int g; in(g);
        vector<int> q;
        forn(j,1,g){
            int temp; in(temp);
            q.emplace_back(temp);
        }
        sort(q.begin(), q.end(), [](int a, int b){return tin[a] < tin[b];});
        if(g==1) continue;

        q.emplace_back(q[0]);

        forn(j,1,sz(q)-1){
            int c=tin[q[j-1]], d = tin[q[j]];
            int lc = BL.getLca(q[j], q[j-1]);
            int e = tin[lc];
            FT.update(c, 1);
            FT.update(d, 1);
            FT.update(e, -2);
        }
    }

    vector<int> fin;
    const int trg = 2*k;

    forn(i,2,n){
        int res = FT.print(tout[i])-FT.print(tin[i]-1);
        if(res>=2*k) fin.emplace_back(p_edge[i]);
    }

    sort(fin.begin(), fin.end());

    out(sz(fin));

    forn(i,0,sz(fin)-1){
        cout << fin[i] << " \n"[i==sz(fin)-1];
    }
    
    return 0;
}

Compilation message

railway.cpp: In function 'int main()':
railway.cpp:164:15: warning: unused variable 'trg' [-Wunused-variable]
  164 |     const int trg = 2*k;
      |               ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4048 KB Output is correct
3 Correct 4 ms 3932 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 4 ms 4524 KB Output is correct
7 Correct 4 ms 4188 KB Output is correct
8 Correct 4 ms 3932 KB Output is correct
9 Correct 4 ms 4188 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4048 KB Output is correct
3 Correct 4 ms 3932 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 4 ms 4524 KB Output is correct
7 Correct 4 ms 4188 KB Output is correct
8 Correct 4 ms 3932 KB Output is correct
9 Correct 4 ms 4188 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 18 ms 4508 KB Output is correct
16 Correct 21 ms 4444 KB Output is correct
17 Correct 20 ms 4444 KB Output is correct
18 Correct 5 ms 4444 KB Output is correct
19 Correct 4 ms 4188 KB Output is correct
20 Correct 15 ms 4644 KB Output is correct
21 Correct 19 ms 4440 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 4 ms 3932 KB Output is correct
24 Correct 4 ms 4104 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 4 ms 4444 KB Output is correct
28 Correct 4 ms 4020 KB Output is correct
29 Correct 5 ms 4100 KB Output is correct
30 Correct 4 ms 3972 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 24924 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 64 ms 24344 KB Output is correct
4 Correct 62 ms 23668 KB Output is correct
5 Correct 66 ms 25032 KB Output is correct
6 Correct 57 ms 25288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 20948 KB Output is correct
2 Correct 61 ms 18240 KB Output is correct
3 Correct 72 ms 18124 KB Output is correct
4 Correct 68 ms 18116 KB Output is correct
5 Correct 57 ms 18020 KB Output is correct
6 Correct 54 ms 21196 KB Output is correct
7 Correct 50 ms 21196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 20948 KB Output is correct
2 Correct 61 ms 18240 KB Output is correct
3 Correct 72 ms 18124 KB Output is correct
4 Correct 68 ms 18116 KB Output is correct
5 Correct 57 ms 18020 KB Output is correct
6 Correct 54 ms 21196 KB Output is correct
7 Correct 50 ms 21196 KB Output is correct
8 Correct 58 ms 21196 KB Output is correct
9 Correct 66 ms 21160 KB Output is correct
10 Correct 57 ms 25032 KB Output is correct
11 Correct 56 ms 25032 KB Output is correct
12 Correct 51 ms 17868 KB Output is correct
13 Correct 59 ms 17864 KB Output is correct
14 Correct 56 ms 17876 KB Output is correct
15 Correct 57 ms 17864 KB Output is correct
16 Correct 59 ms 18120 KB Output is correct
17 Correct 60 ms 18120 KB Output is correct
18 Correct 59 ms 18124 KB Output is correct
19 Correct 63 ms 18376 KB Output is correct
20 Correct 52 ms 21528 KB Output is correct
21 Correct 56 ms 21704 KB Output is correct
22 Correct 66 ms 21328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4048 KB Output is correct
3 Correct 4 ms 3932 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 4 ms 4524 KB Output is correct
7 Correct 4 ms 4188 KB Output is correct
8 Correct 4 ms 3932 KB Output is correct
9 Correct 4 ms 4188 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 18 ms 4508 KB Output is correct
16 Correct 21 ms 4444 KB Output is correct
17 Correct 20 ms 4444 KB Output is correct
18 Correct 5 ms 4444 KB Output is correct
19 Correct 4 ms 4188 KB Output is correct
20 Correct 15 ms 4644 KB Output is correct
21 Correct 19 ms 4440 KB Output is correct
22 Correct 1 ms 2648 KB Output is correct
23 Correct 4 ms 3932 KB Output is correct
24 Correct 4 ms 4104 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 4 ms 4444 KB Output is correct
28 Correct 4 ms 4020 KB Output is correct
29 Correct 5 ms 4100 KB Output is correct
30 Correct 4 ms 3972 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 64 ms 24924 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 64 ms 24344 KB Output is correct
39 Correct 62 ms 23668 KB Output is correct
40 Correct 66 ms 25032 KB Output is correct
41 Correct 57 ms 25288 KB Output is correct
42 Correct 52 ms 20948 KB Output is correct
43 Correct 61 ms 18240 KB Output is correct
44 Correct 72 ms 18124 KB Output is correct
45 Correct 68 ms 18116 KB Output is correct
46 Correct 57 ms 18020 KB Output is correct
47 Correct 54 ms 21196 KB Output is correct
48 Correct 50 ms 21196 KB Output is correct
49 Correct 58 ms 21196 KB Output is correct
50 Correct 66 ms 21160 KB Output is correct
51 Correct 57 ms 25032 KB Output is correct
52 Correct 56 ms 25032 KB Output is correct
53 Correct 51 ms 17868 KB Output is correct
54 Correct 59 ms 17864 KB Output is correct
55 Correct 56 ms 17876 KB Output is correct
56 Correct 57 ms 17864 KB Output is correct
57 Correct 59 ms 18120 KB Output is correct
58 Correct 60 ms 18120 KB Output is correct
59 Correct 59 ms 18124 KB Output is correct
60 Correct 63 ms 18376 KB Output is correct
61 Correct 52 ms 21528 KB Output is correct
62 Correct 56 ms 21704 KB Output is correct
63 Correct 66 ms 21328 KB Output is correct
64 Correct 54 ms 21432 KB Output is correct
65 Correct 63 ms 18032 KB Output is correct
66 Correct 61 ms 17876 KB Output is correct
67 Correct 62 ms 17864 KB Output is correct
68 Correct 43 ms 17752 KB Output is correct
69 Correct 40 ms 17620 KB Output is correct
70 Correct 61 ms 21772 KB Output is correct
71 Correct 57 ms 21004 KB Output is correct
72 Correct 1 ms 2652 KB Output is correct
73 Correct 4 ms 3932 KB Output is correct
74 Correct 4 ms 3932 KB Output is correct
75 Correct 1 ms 2652 KB Output is correct
76 Correct 1 ms 2648 KB Output is correct
77 Correct 4 ms 4444 KB Output is correct
78 Correct 5 ms 4104 KB Output is correct
79 Correct 4 ms 4188 KB Output is correct
80 Correct 4 ms 4188 KB Output is correct
81 Correct 1 ms 2812 KB Output is correct
82 Correct 1 ms 2652 KB Output is correct
83 Correct 2 ms 2652 KB Output is correct
84 Correct 1 ms 2664 KB Output is correct
85 Correct 1 ms 2652 KB Output is correct
86 Correct 16 ms 4444 KB Output is correct
87 Correct 17 ms 4440 KB Output is correct
88 Correct 17 ms 4444 KB Output is correct
89 Correct 5 ms 4444 KB Output is correct
90 Correct 4 ms 4016 KB Output is correct
91 Correct 15 ms 4696 KB Output is correct
92 Correct 17 ms 4440 KB Output is correct
93 Correct 1 ms 2652 KB Output is correct
94 Correct 66 ms 25036 KB Output is correct
95 Correct 63 ms 24328 KB Output is correct
96 Correct 59 ms 23828 KB Output is correct
97 Correct 65 ms 25032 KB Output is correct
98 Correct 57 ms 25284 KB Output is correct
99 Correct 64 ms 18116 KB Output is correct
100 Correct 61 ms 18120 KB Output is correct
101 Correct 66 ms 18124 KB Output is correct
102 Correct 56 ms 18376 KB Output is correct
103 Correct 51 ms 21448 KB Output is correct
104 Correct 54 ms 21620 KB Output is correct
105 Correct 52 ms 21192 KB Output is correct
106 Correct 56 ms 21256 KB Output is correct
107 Correct 54 ms 21108 KB Output is correct
108 Correct 55 ms 25040 KB Output is correct
109 Correct 60 ms 25032 KB Output is correct
110 Correct 47 ms 17868 KB Output is correct
111 Correct 46 ms 17840 KB Output is correct
112 Correct 55 ms 18064 KB Output is correct
113 Correct 59 ms 17944 KB Output is correct