Submission #1106839

# Submission time Handle Problem Language Result Execution time Memory
1106839 2024-10-31T07:43:37 Z luvna Railway (BOI17_railway) C++14
100 / 100
75 ms 56000 KB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e6 + 15;
const int lg = 17;

int n, m, K;
vector<pii> g[N];
int par[N][lg+1];
int tin[N], tout[N], timerDFS;
int h[N];
int idx[N];

void dfs(int u){
    tin[u] = ++timerDFS;
    for(auto [v, id] : g[u]) if(v != par[u][0]){
        par[v][0] = u;
        h[v] = h[u] + 1;
        idx[v] = id;
        for(int j = 1; j <= lg; j++) par[v][j] = par[par[v][j-1]][j-1];
        dfs(v);
    }
    tout[u] = timerDFS;
}

bool inside(int u, int v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int LCA(int u, int v){
    if(inside(u,v)) return u;
    if(inside(v,u)) return v;
    for(int j = lg; j >= 0; j--){
        if(h[u] >= (1 << j) && !inside(par[u][j], v)) u = par[u][j];
    }
    return par[u][0];
}

bool cmp(int a, int b){
    return tin[a] < tin[b];
}

stack<int> st;
int cnt[N];

void solve_query(){
    int k; cin >> k;
    vector<int> vertices;
    for(int i = 1; i <= k; i++){
        int x; cin >> x;
        vertices.pub(x);
    }
    sort(all(vertices), cmp);
    for(int i = 0; i < k-1; i++) vertices.pub(LCA(vertices[i], vertices[i+1]));
    sort(all(vertices), cmp);
    compact(vertices);
    st.push(vertices[0]);
    for(int i = 1; i < sz(vertices); i++){
        while(!st.empty() && !inside(st.top(), vertices[i])) st.pop();
        cnt[vertices[i]]++;
        cnt[st.top()]--;
        st.push(vertices[i]);
    }

    while(!st.empty()) st.pop();
}

vector<int> res;

void calc(int u){
    for(auto [v, id] : g[u]) if(v != par[u][0]){
        calc(v);
        cnt[u] += cnt[v];
    }
    if(cnt[u] >= K) res.pub(idx[u]);
}

void solve(){
    cin >> n >> m >> K;

    for(int i = 1; i < n; i++){
        int u, v; cin >> u >> v;
        g[u].pub(pii(v,i));
        g[v].pub(pii(u,i));
    }

    dfs(1);

    while(m--) solve_query();

    calc(1);

    cout << sz(res) << endl;

    sort(all(res));

    for(int x : res) cout << x << " ";
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    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; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message

railway.cpp: In function 'void dfs(int)':
railway.cpp:36:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   36 |     for(auto [v, id] : g[u]) if(v != par[u][0]){
      |              ^
railway.cpp: In function 'void calc(int)':
railway.cpp:91:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |     for(auto [v, id] : g[u]) if(v != par[u][0]){
      |              ^
railway.cpp: In function 'int main()':
railway.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
railway.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29520 KB Output is correct
2 Correct 8 ms 30684 KB Output is correct
3 Correct 8 ms 30940 KB Output is correct
4 Correct 5 ms 29532 KB Output is correct
5 Correct 5 ms 29692 KB Output is correct
6 Correct 9 ms 31312 KB Output is correct
7 Correct 9 ms 31228 KB Output is correct
8 Correct 7 ms 30800 KB Output is correct
9 Correct 9 ms 30876 KB Output is correct
10 Correct 6 ms 29520 KB Output is correct
11 Correct 5 ms 29668 KB Output is correct
12 Correct 5 ms 29536 KB Output is correct
13 Correct 5 ms 29696 KB Output is correct
14 Correct 7 ms 29672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29520 KB Output is correct
2 Correct 8 ms 30684 KB Output is correct
3 Correct 8 ms 30940 KB Output is correct
4 Correct 5 ms 29532 KB Output is correct
5 Correct 5 ms 29692 KB Output is correct
6 Correct 9 ms 31312 KB Output is correct
7 Correct 9 ms 31228 KB Output is correct
8 Correct 7 ms 30800 KB Output is correct
9 Correct 9 ms 30876 KB Output is correct
10 Correct 6 ms 29520 KB Output is correct
11 Correct 5 ms 29668 KB Output is correct
12 Correct 5 ms 29536 KB Output is correct
13 Correct 5 ms 29696 KB Output is correct
14 Correct 7 ms 29672 KB Output is correct
15 Correct 27 ms 31260 KB Output is correct
16 Correct 26 ms 31480 KB Output is correct
17 Correct 27 ms 31312 KB Output is correct
18 Correct 9 ms 31312 KB Output is correct
19 Correct 9 ms 31056 KB Output is correct
20 Correct 20 ms 31312 KB Output is correct
21 Correct 23 ms 31312 KB Output is correct
22 Correct 5 ms 29520 KB Output is correct
23 Correct 8 ms 30800 KB Output is correct
24 Correct 9 ms 30800 KB Output is correct
25 Correct 5 ms 29520 KB Output is correct
26 Correct 6 ms 29660 KB Output is correct
27 Correct 9 ms 31436 KB Output is correct
28 Correct 12 ms 31056 KB Output is correct
29 Correct 8 ms 30800 KB Output is correct
30 Correct 10 ms 30800 KB Output is correct
31 Correct 5 ms 29520 KB Output is correct
32 Correct 5 ms 29644 KB Output is correct
33 Correct 6 ms 29776 KB Output is correct
34 Correct 5 ms 29520 KB Output is correct
35 Correct 6 ms 29520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 53696 KB Output is correct
2 Correct 6 ms 29520 KB Output is correct
3 Correct 57 ms 53340 KB Output is correct
4 Correct 49 ms 52736 KB Output is correct
5 Correct 63 ms 53696 KB Output is correct
6 Correct 69 ms 53952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 49752 KB Output is correct
2 Correct 55 ms 47944 KB Output is correct
3 Correct 51 ms 47432 KB Output is correct
4 Correct 47 ms 47512 KB Output is correct
5 Correct 52 ms 47360 KB Output is correct
6 Correct 50 ms 51528 KB Output is correct
7 Correct 67 ms 51280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 49752 KB Output is correct
2 Correct 55 ms 47944 KB Output is correct
3 Correct 51 ms 47432 KB Output is correct
4 Correct 47 ms 47512 KB Output is correct
5 Correct 52 ms 47360 KB Output is correct
6 Correct 50 ms 51528 KB Output is correct
7 Correct 67 ms 51280 KB Output is correct
8 Correct 60 ms 51272 KB Output is correct
9 Correct 60 ms 51472 KB Output is correct
10 Correct 60 ms 55644 KB Output is correct
11 Correct 59 ms 55508 KB Output is correct
12 Correct 45 ms 47216 KB Output is correct
13 Correct 44 ms 47228 KB Output is correct
14 Correct 56 ms 47432 KB Output is correct
15 Correct 49 ms 47440 KB Output is correct
16 Correct 47 ms 47532 KB Output is correct
17 Correct 51 ms 47432 KB Output is correct
18 Correct 51 ms 47332 KB Output is correct
19 Correct 52 ms 47844 KB Output is correct
20 Correct 55 ms 51528 KB Output is correct
21 Correct 62 ms 51632 KB Output is correct
22 Correct 53 ms 51524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 29520 KB Output is correct
2 Correct 8 ms 30684 KB Output is correct
3 Correct 8 ms 30940 KB Output is correct
4 Correct 5 ms 29532 KB Output is correct
5 Correct 5 ms 29692 KB Output is correct
6 Correct 9 ms 31312 KB Output is correct
7 Correct 9 ms 31228 KB Output is correct
8 Correct 7 ms 30800 KB Output is correct
9 Correct 9 ms 30876 KB Output is correct
10 Correct 6 ms 29520 KB Output is correct
11 Correct 5 ms 29668 KB Output is correct
12 Correct 5 ms 29536 KB Output is correct
13 Correct 5 ms 29696 KB Output is correct
14 Correct 7 ms 29672 KB Output is correct
15 Correct 27 ms 31260 KB Output is correct
16 Correct 26 ms 31480 KB Output is correct
17 Correct 27 ms 31312 KB Output is correct
18 Correct 9 ms 31312 KB Output is correct
19 Correct 9 ms 31056 KB Output is correct
20 Correct 20 ms 31312 KB Output is correct
21 Correct 23 ms 31312 KB Output is correct
22 Correct 5 ms 29520 KB Output is correct
23 Correct 8 ms 30800 KB Output is correct
24 Correct 9 ms 30800 KB Output is correct
25 Correct 5 ms 29520 KB Output is correct
26 Correct 6 ms 29660 KB Output is correct
27 Correct 9 ms 31436 KB Output is correct
28 Correct 12 ms 31056 KB Output is correct
29 Correct 8 ms 30800 KB Output is correct
30 Correct 10 ms 30800 KB Output is correct
31 Correct 5 ms 29520 KB Output is correct
32 Correct 5 ms 29644 KB Output is correct
33 Correct 6 ms 29776 KB Output is correct
34 Correct 5 ms 29520 KB Output is correct
35 Correct 6 ms 29520 KB Output is correct
36 Correct 60 ms 53696 KB Output is correct
37 Correct 6 ms 29520 KB Output is correct
38 Correct 57 ms 53340 KB Output is correct
39 Correct 49 ms 52736 KB Output is correct
40 Correct 63 ms 53696 KB Output is correct
41 Correct 69 ms 53952 KB Output is correct
42 Correct 55 ms 49752 KB Output is correct
43 Correct 55 ms 47944 KB Output is correct
44 Correct 51 ms 47432 KB Output is correct
45 Correct 47 ms 47512 KB Output is correct
46 Correct 52 ms 47360 KB Output is correct
47 Correct 50 ms 51528 KB Output is correct
48 Correct 67 ms 51280 KB Output is correct
49 Correct 60 ms 51272 KB Output is correct
50 Correct 60 ms 51472 KB Output is correct
51 Correct 60 ms 55644 KB Output is correct
52 Correct 59 ms 55508 KB Output is correct
53 Correct 45 ms 47216 KB Output is correct
54 Correct 44 ms 47228 KB Output is correct
55 Correct 56 ms 47432 KB Output is correct
56 Correct 49 ms 47440 KB Output is correct
57 Correct 47 ms 47532 KB Output is correct
58 Correct 51 ms 47432 KB Output is correct
59 Correct 51 ms 47332 KB Output is correct
60 Correct 52 ms 47844 KB Output is correct
61 Correct 55 ms 51528 KB Output is correct
62 Correct 62 ms 51632 KB Output is correct
63 Correct 53 ms 51524 KB Output is correct
64 Correct 52 ms 51608 KB Output is correct
65 Correct 51 ms 47544 KB Output is correct
66 Correct 55 ms 47432 KB Output is correct
67 Correct 52 ms 47320 KB Output is correct
68 Correct 50 ms 47068 KB Output is correct
69 Correct 55 ms 47116 KB Output is correct
70 Correct 59 ms 51712 KB Output is correct
71 Correct 62 ms 51280 KB Output is correct
72 Correct 5 ms 29520 KB Output is correct
73 Correct 8 ms 30960 KB Output is correct
74 Correct 8 ms 30800 KB Output is correct
75 Correct 5 ms 29520 KB Output is correct
76 Correct 5 ms 29520 KB Output is correct
77 Correct 8 ms 31312 KB Output is correct
78 Correct 8 ms 31056 KB Output is correct
79 Correct 7 ms 30800 KB Output is correct
80 Correct 9 ms 30952 KB Output is correct
81 Correct 6 ms 29520 KB Output is correct
82 Correct 5 ms 29520 KB Output is correct
83 Correct 5 ms 29520 KB Output is correct
84 Correct 6 ms 29520 KB Output is correct
85 Correct 5 ms 29520 KB Output is correct
86 Correct 26 ms 31144 KB Output is correct
87 Correct 26 ms 31312 KB Output is correct
88 Correct 26 ms 31312 KB Output is correct
89 Correct 10 ms 31312 KB Output is correct
90 Correct 8 ms 31056 KB Output is correct
91 Correct 19 ms 31468 KB Output is correct
92 Correct 24 ms 31312 KB Output is correct
93 Correct 5 ms 29520 KB Output is correct
94 Correct 56 ms 55656 KB Output is correct
95 Correct 58 ms 55312 KB Output is correct
96 Correct 51 ms 54600 KB Output is correct
97 Correct 58 ms 55540 KB Output is correct
98 Correct 69 ms 56000 KB Output is correct
99 Correct 57 ms 47440 KB Output is correct
100 Correct 70 ms 47560 KB Output is correct
101 Correct 55 ms 47432 KB Output is correct
102 Correct 54 ms 47740 KB Output is correct
103 Correct 51 ms 51228 KB Output is correct
104 Correct 50 ms 51652 KB Output is correct
105 Correct 56 ms 51156 KB Output is correct
106 Correct 75 ms 51736 KB Output is correct
107 Correct 73 ms 51428 KB Output is correct
108 Correct 57 ms 55644 KB Output is correct
109 Correct 63 ms 55512 KB Output is correct
110 Correct 44 ms 47224 KB Output is correct
111 Correct 47 ms 47176 KB Output is correct
112 Correct 53 ms 47364 KB Output is correct
113 Correct 49 ms 47432 KB Output is correct