Submission #1095406

# Submission time Handle Problem Language Result Execution time Memory
1095406 2024-10-02T04:15:26 Z quanlt206 Railway (BOI17_railway) C++17
100 / 100
169 ms 45140 KB
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;

template<class A, class B>
    bool maximize(A& x, B y) {
        if (x < y) return x = y, true; else return false;
    }
template<class A, class B>
    bool minimize(A& x, B y) {
        if (x > y) return x = y, true; else return false;
    }

/* END OF TEMPLATE */

const int N = 1e5 + 7;


int P[N][20], high[N];

int n, m, k;

int last_edge[N];

vector<pii> v[N];

map<int, int> mp[N];

void dfs(int x, int par) {
    P[x][0] = par;
    for (int i = 1; i < 18; i++) P[x][i] = P[P[x][i - 1]][i - 1];
    for (auto y : v[x])
        if (y.X != par) {
            high[y.X] = high[x] + 1;
            last_edge[y.X] = y.Y;
            dfs(y.X, x);
        }
}

int LCA(int u, int v) {
    if (high[u] > high[v]) swap(u, v);
    int h = high[v] - high[u];
    for (int i = 0; i < 18; i++)
        if (h & MASK(i)) v = P[v][i];
    if (u == v) return u;
    for (int i = 17; i >= 0; i--)
        if (P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
    return P[u][0];
}

void update(int x, int y, int w) {
    mp[x][w]++;
    mp[y][w]++;
    mp[LCA(x, y)][w]-=2;
}

vector<int> res;

void DFS(int x, int par) {
    for (auto y : v[x])
        if (y.X != par) {
            DFS(y.X, x);
            if ((int)mp[x].size() < (int)mp[y.X].size()) swap(mp[x], mp[y.X]);
            for (auto tmp : mp[y.X]) {
                mp[x][tmp.X]+=tmp.Y;
                if (mp[x][tmp.X] == 0) mp[x].erase(mp[x].find(tmp.X));
            }
        }
    if (mp[x].size() >= k) res.push_back(last_edge[x]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m>>k;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin>>x>>y;
        v[x].push_back({y, i});
        v[y].push_back({x, i});
    }
    dfs(1, 1);
    for (int t = 1; t <= m; t++) {
        int s;
        cin>>s;
        set<int> tmp;
        while (s--) {
            int x;
            cin>>x;
            tmp.insert(x);
        }
        vector<int> a(tmp.begin(), tmp.end());
        for (int i = 1; i < a.size(); i++) update(a[i - 1], a[i], t);
    }
    DFS(1, 1);
    sort(all(res));
    cout<<res.size()<<"\n";
    for (auto x : res) cout<<x<<" ";
    return 0;
}

Compilation message

railway.cpp: In function 'void DFS(int, int)':
railway.cpp:96:22: warning: comparison of integer expressions of different signedness: 'std::map<int, int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   96 |     if (mp[x].size() >= k) res.push_back(last_edge[x]);
      |         ~~~~~~~~~~~~~^~~~
railway.cpp: In function 'int main()':
railway.cpp:120:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         for (int i = 1; i < a.size(); i++) update(a[i - 1], a[i], t);
      |                         ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7520 KB Output is correct
2 Correct 6 ms 8796 KB Output is correct
3 Correct 7 ms 8796 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 7 ms 9564 KB Output is correct
7 Correct 7 ms 9052 KB Output is correct
8 Correct 6 ms 9068 KB Output is correct
9 Correct 7 ms 9052 KB Output is correct
10 Correct 4 ms 7536 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 4 ms 7516 KB Output is correct
14 Correct 4 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7520 KB Output is correct
2 Correct 6 ms 8796 KB Output is correct
3 Correct 7 ms 8796 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 7 ms 9564 KB Output is correct
7 Correct 7 ms 9052 KB Output is correct
8 Correct 6 ms 9068 KB Output is correct
9 Correct 7 ms 9052 KB Output is correct
10 Correct 4 ms 7536 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 4 ms 7516 KB Output is correct
14 Correct 4 ms 7516 KB Output is correct
15 Correct 48 ms 15000 KB Output is correct
16 Correct 43 ms 15048 KB Output is correct
17 Correct 50 ms 14928 KB Output is correct
18 Correct 8 ms 9916 KB Output is correct
19 Correct 7 ms 9052 KB Output is correct
20 Correct 42 ms 14068 KB Output is correct
21 Correct 43 ms 14164 KB Output is correct
22 Correct 3 ms 7512 KB Output is correct
23 Correct 7 ms 8796 KB Output is correct
24 Correct 6 ms 8796 KB Output is correct
25 Correct 3 ms 7516 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 7 ms 9560 KB Output is correct
28 Correct 6 ms 9052 KB Output is correct
29 Correct 6 ms 9048 KB Output is correct
30 Correct 6 ms 9052 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7516 KB Output is correct
33 Correct 3 ms 7516 KB Output is correct
34 Correct 3 ms 7512 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 41672 KB Output is correct
2 Correct 3 ms 7516 KB Output is correct
3 Correct 105 ms 41464 KB Output is correct
4 Correct 105 ms 40764 KB Output is correct
5 Correct 89 ms 40904 KB Output is correct
6 Correct 100 ms 43472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 36704 KB Output is correct
2 Correct 125 ms 32772 KB Output is correct
3 Correct 139 ms 38860 KB Output is correct
4 Correct 133 ms 39248 KB Output is correct
5 Correct 145 ms 38736 KB Output is correct
6 Correct 104 ms 36704 KB Output is correct
7 Correct 105 ms 36692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 36704 KB Output is correct
2 Correct 125 ms 32772 KB Output is correct
3 Correct 139 ms 38860 KB Output is correct
4 Correct 133 ms 39248 KB Output is correct
5 Correct 145 ms 38736 KB Output is correct
6 Correct 104 ms 36704 KB Output is correct
7 Correct 105 ms 36692 KB Output is correct
8 Correct 109 ms 36176 KB Output is correct
9 Correct 98 ms 35920 KB Output is correct
10 Correct 81 ms 40652 KB Output is correct
11 Correct 84 ms 40648 KB Output is correct
12 Correct 68 ms 26448 KB Output is correct
13 Correct 92 ms 26120 KB Output is correct
14 Correct 118 ms 33108 KB Output is correct
15 Correct 116 ms 32944 KB Output is correct
16 Correct 145 ms 40396 KB Output is correct
17 Correct 158 ms 40652 KB Output is correct
18 Correct 132 ms 38480 KB Output is correct
19 Correct 126 ms 33620 KB Output is correct
20 Correct 103 ms 36944 KB Output is correct
21 Correct 113 ms 37064 KB Output is correct
22 Correct 111 ms 37204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7520 KB Output is correct
2 Correct 6 ms 8796 KB Output is correct
3 Correct 7 ms 8796 KB Output is correct
4 Correct 3 ms 7512 KB Output is correct
5 Correct 3 ms 7516 KB Output is correct
6 Correct 7 ms 9564 KB Output is correct
7 Correct 7 ms 9052 KB Output is correct
8 Correct 6 ms 9068 KB Output is correct
9 Correct 7 ms 9052 KB Output is correct
10 Correct 4 ms 7536 KB Output is correct
11 Correct 3 ms 7516 KB Output is correct
12 Correct 3 ms 7516 KB Output is correct
13 Correct 4 ms 7516 KB Output is correct
14 Correct 4 ms 7516 KB Output is correct
15 Correct 48 ms 15000 KB Output is correct
16 Correct 43 ms 15048 KB Output is correct
17 Correct 50 ms 14928 KB Output is correct
18 Correct 8 ms 9916 KB Output is correct
19 Correct 7 ms 9052 KB Output is correct
20 Correct 42 ms 14068 KB Output is correct
21 Correct 43 ms 14164 KB Output is correct
22 Correct 3 ms 7512 KB Output is correct
23 Correct 7 ms 8796 KB Output is correct
24 Correct 6 ms 8796 KB Output is correct
25 Correct 3 ms 7516 KB Output is correct
26 Correct 3 ms 7516 KB Output is correct
27 Correct 7 ms 9560 KB Output is correct
28 Correct 6 ms 9052 KB Output is correct
29 Correct 6 ms 9048 KB Output is correct
30 Correct 6 ms 9052 KB Output is correct
31 Correct 3 ms 7516 KB Output is correct
32 Correct 3 ms 7516 KB Output is correct
33 Correct 3 ms 7516 KB Output is correct
34 Correct 3 ms 7512 KB Output is correct
35 Correct 3 ms 7516 KB Output is correct
36 Correct 108 ms 41672 KB Output is correct
37 Correct 3 ms 7516 KB Output is correct
38 Correct 105 ms 41464 KB Output is correct
39 Correct 105 ms 40764 KB Output is correct
40 Correct 89 ms 40904 KB Output is correct
41 Correct 100 ms 43472 KB Output is correct
42 Correct 101 ms 36704 KB Output is correct
43 Correct 125 ms 32772 KB Output is correct
44 Correct 139 ms 38860 KB Output is correct
45 Correct 133 ms 39248 KB Output is correct
46 Correct 145 ms 38736 KB Output is correct
47 Correct 104 ms 36704 KB Output is correct
48 Correct 105 ms 36692 KB Output is correct
49 Correct 109 ms 36176 KB Output is correct
50 Correct 98 ms 35920 KB Output is correct
51 Correct 81 ms 40652 KB Output is correct
52 Correct 84 ms 40648 KB Output is correct
53 Correct 68 ms 26448 KB Output is correct
54 Correct 92 ms 26120 KB Output is correct
55 Correct 118 ms 33108 KB Output is correct
56 Correct 116 ms 32944 KB Output is correct
57 Correct 145 ms 40396 KB Output is correct
58 Correct 158 ms 40652 KB Output is correct
59 Correct 132 ms 38480 KB Output is correct
60 Correct 126 ms 33620 KB Output is correct
61 Correct 103 ms 36944 KB Output is correct
62 Correct 113 ms 37064 KB Output is correct
63 Correct 111 ms 37204 KB Output is correct
64 Correct 104 ms 35928 KB Output is correct
65 Correct 169 ms 45140 KB Output is correct
66 Correct 157 ms 40248 KB Output is correct
67 Correct 122 ms 36436 KB Output is correct
68 Correct 56 ms 24656 KB Output is correct
69 Correct 63 ms 24656 KB Output is correct
70 Correct 100 ms 36976 KB Output is correct
71 Correct 95 ms 35800 KB Output is correct
72 Correct 3 ms 7516 KB Output is correct
73 Correct 6 ms 8796 KB Output is correct
74 Correct 6 ms 8796 KB Output is correct
75 Correct 3 ms 7516 KB Output is correct
76 Correct 4 ms 7512 KB Output is correct
77 Correct 6 ms 9564 KB Output is correct
78 Correct 7 ms 8888 KB Output is correct
79 Correct 6 ms 8816 KB Output is correct
80 Correct 6 ms 9048 KB Output is correct
81 Correct 3 ms 7516 KB Output is correct
82 Correct 3 ms 7536 KB Output is correct
83 Correct 3 ms 7516 KB Output is correct
84 Correct 3 ms 7516 KB Output is correct
85 Correct 3 ms 7516 KB Output is correct
86 Correct 43 ms 14820 KB Output is correct
87 Correct 45 ms 14928 KB Output is correct
88 Correct 44 ms 14932 KB Output is correct
89 Correct 8 ms 9820 KB Output is correct
90 Correct 6 ms 9052 KB Output is correct
91 Correct 42 ms 14144 KB Output is correct
92 Correct 44 ms 14416 KB Output is correct
93 Correct 3 ms 7516 KB Output is correct
94 Correct 106 ms 41704 KB Output is correct
95 Correct 105 ms 41420 KB Output is correct
96 Correct 100 ms 40784 KB Output is correct
97 Correct 106 ms 40908 KB Output is correct
98 Correct 102 ms 43468 KB Output is correct
99 Correct 153 ms 42920 KB Output is correct
100 Correct 148 ms 40016 KB Output is correct
101 Correct 146 ms 40528 KB Output is correct
102 Correct 161 ms 33616 KB Output is correct
103 Correct 110 ms 36692 KB Output is correct
104 Correct 107 ms 37100 KB Output is correct
105 Correct 115 ms 36616 KB Output is correct
106 Correct 102 ms 35940 KB Output is correct
107 Correct 99 ms 35924 KB Output is correct
108 Correct 85 ms 40652 KB Output is correct
109 Correct 77 ms 40648 KB Output is correct
110 Correct 65 ms 26196 KB Output is correct
111 Correct 63 ms 26216 KB Output is correct
112 Correct 121 ms 32848 KB Output is correct
113 Correct 116 ms 33108 KB Output is correct