답안 #404451

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
404451 2021-05-14T12:23:25 Z Aryan_Raina Railway (BOI17_railway) C++14
100 / 100
283 ms 38940 KB
#include <bits/stdc++.h>
using namespace std; 

#define int int64_t
#define ld long double
#define ar array

const int INF = 1e17;
const int MOD = 1e9+7;

template<class T> struct SegmentTree {
   int n; vector<T> values; 
   T IDENTITY = 0;
   SegmentTree(int n, int id = 0) : n(n), values(2*n, IDENTITY = id) {}
   T calc_op(T a, T b) { return a + b; }
   void modify(int i, T v) {
      for (values[i += n] = v; i >>= 1; ) 
         values[i] = calc_op(values[2*i], values[2*i + 1]);
   }
   T calc(int l, int r) {
      T rans = IDENTITY, lans = IDENTITY;
      for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
         if (l & 1) lans = calc_op(lans, values[l++]);
         if (r & 1) rans = calc_op(values[--r], rans);
      }
      return calc_op(lans, rans);
   }
};

const int MXN = 1e5+9;
vector<ar<int, 2>> g[MXN];
int N, M, K, up[20][MXN], depth[MXN], val[MXN];
int tin[MXN], tout[MXN], timer = 0;
vector<int> ans;

void dfs(int u, int pu) {
   up[0][u] = pu; tin[u] = timer++;
   for (int i = 1; i < 20; i++)
      up[i][u] = up[i-1][up[i-1][u]];

   for (auto v : g[u]) if (v[1] != pu) {
      depth[v[1]] = depth[u] + 1;
      dfs(v[1], u);
   }

   tout[u] = timer;
}

int lca(int u, int v) {
   if (depth[u] > depth[v]) swap(u, v);
   for (int i = 19; i >= 0; --i)
      if ((depth[v] - depth[u]) >> i & 1)
         v = up[i][v];

   if (u == v) return u;

   for (int i = 19; i >= 0; --i) 
      if (up[i][u] != up[i][v])
         u = up[i][u], v = up[i][v];

   return up[0][u];
}

void magic(int u, int pu) {
   for (auto v : g[u]) if (v[1] != pu) {
      magic(v[1], u);
      if (val[v[1]] >= K) ans.push_back(v[0]);
      val[u] += val[v[1]];
   }
}

int32_t main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0); cout.tie(0);

   cin >> N >> M >> K;
   for (int i = 0; i < N - 1; ++i) {
      int a, b; cin >> a >> b; --a, --b;
      g[a].push_back({i, b}); g[b].push_back({i, a});
   }

   dfs(0, 0);

   SegmentTree<int> seg(N, 0);

   while (M--) {
      int s; cin >> s;
      vector<int> shit(s);
      for (int &i : shit) cin >> i, --i;

      sort(shit.begin(), shit.end(), [&](int i, int j) { return depth[i] > depth[j]; });

      int baap = shit.back();
      for (int i : shit) baap = lca(baap, i);

      for (int i : shit) if (!seg.calc(tin[i], tout[i])) {
         int x = i;
         
         // get first top thingie with already +1 coz of this guy
         for (int j = 19; j >= 0; --j)
            if (depth[baap] < depth[up[j][x]] && !seg.calc(tin[up[j][x]], tout[up[j][x]]))
               x = up[j][x];
         x = up[0][x];

         val[i]++; val[x]--;
         seg.modify(tin[i], 1);
      }

      for (int i : shit) seg.modify(tin[i], 0);
   }   

   magic(0, 0);

   sort(ans.begin(), ans.end());
   
   cout << ans.size() << '\n';
   for (int i : ans) cout << i + 1 << ' ';
}  
 
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 9 ms 5452 KB Output is correct
3 Correct 8 ms 5324 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 10 ms 5924 KB Output is correct
7 Correct 9 ms 5452 KB Output is correct
8 Correct 7 ms 5324 KB Output is correct
9 Correct 8 ms 5324 KB Output is correct
10 Correct 2 ms 2804 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 9 ms 5452 KB Output is correct
3 Correct 8 ms 5324 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 10 ms 5924 KB Output is correct
7 Correct 9 ms 5452 KB Output is correct
8 Correct 7 ms 5324 KB Output is correct
9 Correct 8 ms 5324 KB Output is correct
10 Correct 2 ms 2804 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 49 ms 5788 KB Output is correct
16 Correct 42 ms 5828 KB Output is correct
17 Correct 43 ms 5828 KB Output is correct
18 Correct 11 ms 5880 KB Output is correct
19 Correct 10 ms 5500 KB Output is correct
20 Correct 38 ms 5972 KB Output is correct
21 Correct 42 ms 5812 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 9 ms 5416 KB Output is correct
24 Correct 8 ms 5324 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 10 ms 5836 KB Output is correct
28 Correct 9 ms 5444 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 8 ms 5324 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 2 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 259 ms 36736 KB Output is correct
2 Correct 2 ms 2764 KB Output is correct
3 Correct 238 ms 36232 KB Output is correct
4 Correct 230 ms 35492 KB Output is correct
5 Correct 141 ms 36716 KB Output is correct
6 Correct 123 ms 36956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 32344 KB Output is correct
2 Correct 158 ms 30300 KB Output is correct
3 Correct 181 ms 29928 KB Output is correct
4 Correct 178 ms 29892 KB Output is correct
5 Correct 198 ms 29892 KB Output is correct
6 Correct 148 ms 34044 KB Output is correct
7 Correct 148 ms 33860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 157 ms 32344 KB Output is correct
2 Correct 158 ms 30300 KB Output is correct
3 Correct 181 ms 29928 KB Output is correct
4 Correct 178 ms 29892 KB Output is correct
5 Correct 198 ms 29892 KB Output is correct
6 Correct 148 ms 34044 KB Output is correct
7 Correct 148 ms 33860 KB Output is correct
8 Correct 228 ms 34008 KB Output is correct
9 Correct 234 ms 34108 KB Output is correct
10 Correct 115 ms 38632 KB Output is correct
11 Correct 115 ms 38580 KB Output is correct
12 Correct 150 ms 29072 KB Output is correct
13 Correct 149 ms 29052 KB Output is correct
14 Correct 165 ms 29764 KB Output is correct
15 Correct 161 ms 29756 KB Output is correct
16 Correct 196 ms 29912 KB Output is correct
17 Correct 183 ms 29916 KB Output is correct
18 Correct 186 ms 29892 KB Output is correct
19 Correct 162 ms 30216 KB Output is correct
20 Correct 154 ms 34240 KB Output is correct
21 Correct 158 ms 34348 KB Output is correct
22 Correct 163 ms 34208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 9 ms 5452 KB Output is correct
3 Correct 8 ms 5324 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 10 ms 5924 KB Output is correct
7 Correct 9 ms 5452 KB Output is correct
8 Correct 7 ms 5324 KB Output is correct
9 Correct 8 ms 5324 KB Output is correct
10 Correct 2 ms 2804 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 2 ms 2764 KB Output is correct
13 Correct 3 ms 2764 KB Output is correct
14 Correct 2 ms 2764 KB Output is correct
15 Correct 49 ms 5788 KB Output is correct
16 Correct 42 ms 5828 KB Output is correct
17 Correct 43 ms 5828 KB Output is correct
18 Correct 11 ms 5880 KB Output is correct
19 Correct 10 ms 5500 KB Output is correct
20 Correct 38 ms 5972 KB Output is correct
21 Correct 42 ms 5812 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 9 ms 5416 KB Output is correct
24 Correct 8 ms 5324 KB Output is correct
25 Correct 2 ms 2764 KB Output is correct
26 Correct 2 ms 2764 KB Output is correct
27 Correct 10 ms 5836 KB Output is correct
28 Correct 9 ms 5444 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 8 ms 5324 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 2 ms 2764 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 259 ms 36736 KB Output is correct
37 Correct 2 ms 2764 KB Output is correct
38 Correct 238 ms 36232 KB Output is correct
39 Correct 230 ms 35492 KB Output is correct
40 Correct 141 ms 36716 KB Output is correct
41 Correct 123 ms 36956 KB Output is correct
42 Correct 157 ms 32344 KB Output is correct
43 Correct 158 ms 30300 KB Output is correct
44 Correct 181 ms 29928 KB Output is correct
45 Correct 178 ms 29892 KB Output is correct
46 Correct 198 ms 29892 KB Output is correct
47 Correct 148 ms 34044 KB Output is correct
48 Correct 148 ms 33860 KB Output is correct
49 Correct 228 ms 34008 KB Output is correct
50 Correct 234 ms 34108 KB Output is correct
51 Correct 115 ms 38632 KB Output is correct
52 Correct 115 ms 38580 KB Output is correct
53 Correct 150 ms 29072 KB Output is correct
54 Correct 149 ms 29052 KB Output is correct
55 Correct 165 ms 29764 KB Output is correct
56 Correct 161 ms 29756 KB Output is correct
57 Correct 196 ms 29912 KB Output is correct
58 Correct 183 ms 29916 KB Output is correct
59 Correct 186 ms 29892 KB Output is correct
60 Correct 162 ms 30216 KB Output is correct
61 Correct 154 ms 34240 KB Output is correct
62 Correct 158 ms 34348 KB Output is correct
63 Correct 163 ms 34208 KB Output is correct
64 Correct 200 ms 34212 KB Output is correct
65 Correct 283 ms 29928 KB Output is correct
66 Correct 217 ms 29812 KB Output is correct
67 Correct 198 ms 29844 KB Output is correct
68 Correct 131 ms 28996 KB Output is correct
69 Correct 127 ms 28996 KB Output is correct
70 Correct 198 ms 34540 KB Output is correct
71 Correct 235 ms 33816 KB Output is correct
72 Correct 2 ms 2796 KB Output is correct
73 Correct 9 ms 5452 KB Output is correct
74 Correct 8 ms 5364 KB Output is correct
75 Correct 2 ms 2796 KB Output is correct
76 Correct 2 ms 2764 KB Output is correct
77 Correct 9 ms 5948 KB Output is correct
78 Correct 9 ms 5440 KB Output is correct
79 Correct 7 ms 5324 KB Output is correct
80 Correct 8 ms 5324 KB Output is correct
81 Correct 2 ms 2764 KB Output is correct
82 Correct 2 ms 2764 KB Output is correct
83 Correct 2 ms 2764 KB Output is correct
84 Correct 2 ms 2764 KB Output is correct
85 Correct 2 ms 2764 KB Output is correct
86 Correct 41 ms 5708 KB Output is correct
87 Correct 42 ms 5868 KB Output is correct
88 Correct 45 ms 6004 KB Output is correct
89 Correct 10 ms 5812 KB Output is correct
90 Correct 10 ms 5500 KB Output is correct
91 Correct 38 ms 5928 KB Output is correct
92 Correct 40 ms 5804 KB Output is correct
93 Correct 2 ms 2764 KB Output is correct
94 Correct 261 ms 38636 KB Output is correct
95 Correct 249 ms 38072 KB Output is correct
96 Correct 243 ms 37340 KB Output is correct
97 Correct 142 ms 38552 KB Output is correct
98 Correct 125 ms 38940 KB Output is correct
99 Correct 227 ms 29904 KB Output is correct
100 Correct 199 ms 29924 KB Output is correct
101 Correct 200 ms 29920 KB Output is correct
102 Correct 178 ms 30320 KB Output is correct
103 Correct 162 ms 33980 KB Output is correct
104 Correct 159 ms 34336 KB Output is correct
105 Correct 156 ms 33860 KB Output is correct
106 Correct 237 ms 34136 KB Output is correct
107 Correct 240 ms 34116 KB Output is correct
108 Correct 119 ms 38696 KB Output is correct
109 Correct 117 ms 38584 KB Output is correct
110 Correct 155 ms 29088 KB Output is correct
111 Correct 152 ms 29068 KB Output is correct
112 Correct 175 ms 29832 KB Output is correct
113 Correct 176 ms 29748 KB Output is correct