Submission #922860

# Submission time Handle Problem Language Result Execution time Memory
922860 2024-02-06T08:18:30 Z asdasdqwer Railway (BOI17_railway) C++14
100 / 100
245 ms 47304 KB
#include <bits/stdc++.h>
using namespace std;

#define int int64_t

vector<int> idx;

signed main() {
    int n,m,k;cin>>n>>m>>k;
    vector<vector<int>> g(n);
    int hh=0;
    map<array<int,2>,int> mp;
    for (int i=1;i<n;i++) {
        int a,b;cin>>a>>b;a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
        mp[{a,b}] = hh++;
    }

    idx.assign(n, -1);

    int cnt=0;
    vector<vector<int>> lft(n, vector<int>(20, -1));
    vector<int> d(n);

    function<void(int,int)> dfs=[&](int node,int pv) {
        idx[node]=cnt++;
        for (int x:g[node]) {
            if (x==pv)continue;
            d[x]=d[node]+1;
            lft[x][0]=node;
            dfs(x,node);
        }
    };

    dfs(0, -1);

    for (int j=1;j<20;j++) {
        for (int i=0;i<n;i++) {
            lft[i][j]=lft[i][j-1];
            if (lft[i][j] != -1) {
                lft[i][j] = lft[lft[i][j]][j-1];
            }
        }
    }

    function<int(int,int)> jmp=[&](int a, int steps) -> int {
        int c=0;
        while (steps) {
            if ((steps&1)==1) {
                a=lft[a][c];
            }
            c++;steps>>=1;
        }
        return a;
    };

    function<int(int,int)> lca=[&](int a,int b) -> int {
        if (d[a]<d[b])swap(a,b);
        a=jmp(a,d[a]-d[b]);
        if (a==b)return a;

        for (int i=19;i>=0;i--) {
            if (lft[a][i] != lft[b][i]) {
                a=lft[a][i];
                b=lft[b][i];
            }
        }
        return lft[a][0];
    };

    vector<int> sm(n, 0);

    for (int i=0;i<m;i++) {
        int s;cin>>s;
        vector<int> inp(s);
        for (auto &x:inp){cin>>x;x--;}

        sort(inp.begin(),inp.end(), [](int a, int b) { return idx[a] < idx[b]; });

        for (int x:inp)sm[x]++;

        for (int j=0;j<s;j++) {
            int n1=inp[j];
            int n2=inp[(j+1)%s];
            sm[lca(n1,n2)]--;
        }
    }

    function<void(int,int)> dfs2=[&](int node,int pv) {
        for (int x:g[node]) {
            if (x == pv) continue;
            dfs2(x,node);
            sm[node] += sm[x];
        }
    };

    dfs2(0, -1);

    vector<int> out;
    for (int i=0;i<n;i++) {
        if (sm[i] >= k) {
            int code=-1;
            if (mp.find({i, lft[i][0]}) != mp.end()) {
                code = mp[{i, lft[i][0]}];
            }

            else if (mp.find({lft[i][0], i}) != mp.end()) {
                code = mp[{lft[i][0], i}];
            }

            if (code != -1) {
                out.push_back(code);
            }

            else {
                assert(0 == -1);
            }
        }
    }

    sort(out.begin(), out.end());
    if (out.size() == 0) {
        cout<<0<<"\n";
        return 0;
    }

    vector<int> dis={out[0]};
    for (int x:out) {
        if (dis.back() != x)dis.push_back(x);
    }

    cout<<dis.size()<<"\n";
    for (int x:dis) {
        cout<<x+1<<" ";
    }
    cout<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 9 ms 3676 KB Output is correct
3 Correct 9 ms 3676 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 11 ms 4336 KB Output is correct
7 Correct 9 ms 3952 KB Output is correct
8 Correct 8 ms 3676 KB Output is correct
9 Correct 9 ms 3676 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 9 ms 3676 KB Output is correct
3 Correct 9 ms 3676 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 11 ms 4336 KB Output is correct
7 Correct 9 ms 3952 KB Output is correct
8 Correct 8 ms 3676 KB Output is correct
9 Correct 9 ms 3676 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 37 ms 3860 KB Output is correct
16 Correct 34 ms 3924 KB Output is correct
17 Correct 36 ms 3932 KB Output is correct
18 Correct 14 ms 4444 KB Output is correct
19 Correct 11 ms 3988 KB Output is correct
20 Correct 31 ms 3900 KB Output is correct
21 Correct 39 ms 3872 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 9 ms 3880 KB Output is correct
24 Correct 9 ms 3692 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 18 ms 4444 KB Output is correct
28 Correct 10 ms 3960 KB Output is correct
29 Correct 8 ms 3728 KB Output is correct
30 Correct 9 ms 3676 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 500 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 47100 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 241 ms 46120 KB Output is correct
4 Correct 202 ms 45068 KB Output is correct
5 Correct 236 ms 47048 KB Output is correct
6 Correct 241 ms 47212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 39952 KB Output is correct
2 Correct 170 ms 35148 KB Output is correct
3 Correct 160 ms 34656 KB Output is correct
4 Correct 181 ms 34696 KB Output is correct
5 Correct 176 ms 34704 KB Output is correct
6 Correct 156 ms 40272 KB Output is correct
7 Correct 149 ms 40028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 39952 KB Output is correct
2 Correct 170 ms 35148 KB Output is correct
3 Correct 160 ms 34656 KB Output is correct
4 Correct 181 ms 34696 KB Output is correct
5 Correct 176 ms 34704 KB Output is correct
6 Correct 156 ms 40272 KB Output is correct
7 Correct 149 ms 40028 KB Output is correct
8 Correct 175 ms 40424 KB Output is correct
9 Correct 170 ms 40404 KB Output is correct
10 Correct 191 ms 47304 KB Output is correct
11 Correct 187 ms 47288 KB Output is correct
12 Correct 129 ms 34732 KB Output is correct
13 Correct 136 ms 34804 KB Output is correct
14 Correct 176 ms 34660 KB Output is correct
15 Correct 152 ms 34676 KB Output is correct
16 Correct 155 ms 34700 KB Output is correct
17 Correct 187 ms 34640 KB Output is correct
18 Correct 164 ms 34644 KB Output is correct
19 Correct 152 ms 35156 KB Output is correct
20 Correct 164 ms 40692 KB Output is correct
21 Correct 172 ms 41116 KB Output is correct
22 Correct 162 ms 40564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 9 ms 3676 KB Output is correct
3 Correct 9 ms 3676 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 11 ms 4336 KB Output is correct
7 Correct 9 ms 3952 KB Output is correct
8 Correct 8 ms 3676 KB Output is correct
9 Correct 9 ms 3676 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 37 ms 3860 KB Output is correct
16 Correct 34 ms 3924 KB Output is correct
17 Correct 36 ms 3932 KB Output is correct
18 Correct 14 ms 4444 KB Output is correct
19 Correct 11 ms 3988 KB Output is correct
20 Correct 31 ms 3900 KB Output is correct
21 Correct 39 ms 3872 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 9 ms 3880 KB Output is correct
24 Correct 9 ms 3692 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 18 ms 4444 KB Output is correct
28 Correct 10 ms 3960 KB Output is correct
29 Correct 8 ms 3728 KB Output is correct
30 Correct 9 ms 3676 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 500 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 217 ms 47100 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 241 ms 46120 KB Output is correct
39 Correct 202 ms 45068 KB Output is correct
40 Correct 236 ms 47048 KB Output is correct
41 Correct 241 ms 47212 KB Output is correct
42 Correct 154 ms 39952 KB Output is correct
43 Correct 170 ms 35148 KB Output is correct
44 Correct 160 ms 34656 KB Output is correct
45 Correct 181 ms 34696 KB Output is correct
46 Correct 176 ms 34704 KB Output is correct
47 Correct 156 ms 40272 KB Output is correct
48 Correct 149 ms 40028 KB Output is correct
49 Correct 175 ms 40424 KB Output is correct
50 Correct 170 ms 40404 KB Output is correct
51 Correct 191 ms 47304 KB Output is correct
52 Correct 187 ms 47288 KB Output is correct
53 Correct 129 ms 34732 KB Output is correct
54 Correct 136 ms 34804 KB Output is correct
55 Correct 176 ms 34660 KB Output is correct
56 Correct 152 ms 34676 KB Output is correct
57 Correct 155 ms 34700 KB Output is correct
58 Correct 187 ms 34640 KB Output is correct
59 Correct 164 ms 34644 KB Output is correct
60 Correct 152 ms 35156 KB Output is correct
61 Correct 164 ms 40692 KB Output is correct
62 Correct 172 ms 41116 KB Output is correct
63 Correct 162 ms 40564 KB Output is correct
64 Correct 163 ms 40560 KB Output is correct
65 Correct 184 ms 34788 KB Output is correct
66 Correct 168 ms 34700 KB Output is correct
67 Correct 157 ms 34692 KB Output is correct
68 Correct 126 ms 34972 KB Output is correct
69 Correct 128 ms 34956 KB Output is correct
70 Correct 171 ms 41144 KB Output is correct
71 Correct 162 ms 40044 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 10 ms 3676 KB Output is correct
74 Correct 12 ms 3672 KB Output is correct
75 Correct 0 ms 348 KB Output is correct
76 Correct 0 ms 348 KB Output is correct
77 Correct 10 ms 4428 KB Output is correct
78 Correct 9 ms 3928 KB Output is correct
79 Correct 8 ms 3676 KB Output is correct
80 Correct 8 ms 3864 KB Output is correct
81 Correct 1 ms 348 KB Output is correct
82 Correct 0 ms 348 KB Output is correct
83 Correct 0 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 29 ms 3864 KB Output is correct
87 Correct 30 ms 3868 KB Output is correct
88 Correct 31 ms 3932 KB Output is correct
89 Correct 11 ms 4444 KB Output is correct
90 Correct 9 ms 3932 KB Output is correct
91 Correct 31 ms 3896 KB Output is correct
92 Correct 32 ms 3900 KB Output is correct
93 Correct 0 ms 344 KB Output is correct
94 Correct 221 ms 47064 KB Output is correct
95 Correct 191 ms 46028 KB Output is correct
96 Correct 167 ms 44932 KB Output is correct
97 Correct 238 ms 47192 KB Output is correct
98 Correct 194 ms 47304 KB Output is correct
99 Correct 154 ms 34656 KB Output is correct
100 Correct 159 ms 34712 KB Output is correct
101 Correct 170 ms 34696 KB Output is correct
102 Correct 155 ms 35228 KB Output is correct
103 Correct 181 ms 40016 KB Output is correct
104 Correct 157 ms 40892 KB Output is correct
105 Correct 149 ms 39896 KB Output is correct
106 Correct 172 ms 40524 KB Output is correct
107 Correct 185 ms 40304 KB Output is correct
108 Correct 194 ms 47248 KB Output is correct
109 Correct 245 ms 47212 KB Output is correct
110 Correct 139 ms 34896 KB Output is correct
111 Correct 149 ms 34716 KB Output is correct
112 Correct 155 ms 34640 KB Output is correct
113 Correct 152 ms 34636 KB Output is correct