답안 #515578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
515578 2022-01-19T09:19:07 Z akhan42 Railway (BOI17_railway) C++17
100 / 100
365 ms 39384 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define mp make_pair

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
//typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }


const int MX = 100 * 1000 + 10;
const int mxpw = 20;
vii tobe_cleaned;

struct Seg {
	int n;
	vi t;

	Seg(int n): n(n) {
		t.assign(2 * n, 0);
	}

	void update(int l, int r, bool clean = false, int val = 1) {
		if(clean)
			tobe_cleaned.eb(l, r);
		for(l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
			if(l & 1) t[l++] += val;
			if(r & 1) t[--r] += val;
		}
	}

	int query(int p) {
		int res = 0;
		for(p += n; p > 0; p >>= 1)
			res += t[p];
		return res;
	}
};

vi gr[MX];
int timer = 0;
int tin[MX], tout[MX];
int par[mxpw][MX];
int sub_sz[MX];
int depth[MX];
int root[MX];
int node2edge[MX];
map<ii, int> edges;


void dfs1(int node = 1, int pr = 0, int dep = 0) {
	depth[node] = dep;
	par[0][node] = pr;
	sub_sz[node] = 1;
	if(pr != 0)
		node2edge[node] = edges[mp(min(node, pr), max(node, pr))];

	for(int &nb: gr[node]) {
		gr[nb].erase(find(all(gr[nb]), node));

		dfs1(nb, node, dep + 1);
		sub_sz[node] += sub_sz[nb];
		if(sub_sz[nb] > sub_sz[gr[node][0]])
			swap(nb, gr[node][0]);
	}
}

void dfs2(int node = 1) {
	tin[node] = ++timer;
	for(int nb: gr[node]) {
		root[nb] = (gr[node][0] == nb ? root[node] : nb);
		dfs2(nb);
	}
	tout[node] = timer;
}

Seg* seg_gl;
Seg* seg_cur;

void process_path(int u, int v, bool upd_gl = true) {
	for(; root[u] != root[v]; v = par[0][root[v]]) {
		if(depth[root[u]] > depth[root[v]])
			swap(u, v);
		seg_cur->update(tin[root[v]], tin[v], true);
		if(upd_gl) seg_gl->update(tin[root[v]], tin[v]);
	}
	if(depth[u] > depth[v]) swap(u, v);
	seg_cur->update(tin[u], tin[v], true);
	if(upd_gl) seg_gl->update(tin[u], tin[v]);
}

void clean() {
	for(ii lr: tobe_cleaned) {
		int l = lr.F, r = lr.S;
		seg_cur->update(l, r, false, -1);
	}
	tobe_cleaned.clear();
}

int lca_node(int u, int v) {
	for(; root[u] != root[v]; v = par[0][root[v]])
		if(depth[root[u]] > depth[root[v]])
			swap(u, v);
	return depth[u] > depth[v] ? v : u;
}


void solve() {
//	tout[0] = MX;
	int n, m, k;
	cin >> n >> m >> k;
	forbn(i, 1, n) {
		int a, b;
		cin >> a >> b;
		gr[a].pb(b);
		gr[b].pb(a);
		edges[mp(min(a, b), max(a, b))] = i;
	}

	seg_gl = new Seg(n + 1);
	seg_cur = new Seg(n + 1);
	dfs1();
//	root[1] = 1;
	dfs2();

	forbn(d, 1, mxpw) {
		forn(i, n + 1) {
			int to = par[d - 1][i];
			par[d][i] = par[d - 1][to];
		}
	}

	forn(i, m) {
		int s;
		cin >> s;
		vi nodes(s);
		int lca = -1;
		forn(j, s) {
			cin >> nodes[j];
			if(j == 0)
				lca = nodes[j];
			else
				lca = lca_node(lca, nodes[j]);
		}
		process_path(0, lca, false);
		forn(j, s) {
			int u = nodes[j];
			if(seg_cur->query(tin[u]))
				continue;
			for(int pw = mxpw - 1; pw >= 0; pw--)
				if(seg_cur->query(tin[par[pw][u]]) == 0)
					u = par[pw][u];

			process_path(nodes[j], u);
		}
		clean();
	}

	vi ans;
	forbn(u, 2, n + 1) {
//		cerr << u << ' ' << seg_gl->query(tin[u]) << '\n';
		if(seg_gl->query(tin[u]) >= k) {
			ans.pb(node2edge[u]);
		}
	}
	cout << sz(ans) << '\n';
	sort(all(ans));
	for(int a: ans) {
		cout << a << ' ';
	}
}


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

//	freopen("grassplant.in", "r", stdin);
//	freopen("grassplant.out", "w", stdout);

	int t = 1;
//	cin >> t;
	while(t--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2764 KB Output is correct
2 Correct 9 ms 4984 KB Output is correct
3 Correct 8 ms 5004 KB Output is correct
4 Correct 1 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 9 ms 5560 KB Output is correct
7 Correct 9 ms 5080 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 8 ms 4980 KB Output is correct
10 Correct 1 ms 2764 KB Output is correct
11 Correct 1 ms 2672 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2764 KB Output is correct
2 Correct 9 ms 4984 KB Output is correct
3 Correct 8 ms 5004 KB Output is correct
4 Correct 1 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 9 ms 5560 KB Output is correct
7 Correct 9 ms 5080 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 8 ms 4980 KB Output is correct
10 Correct 1 ms 2764 KB Output is correct
11 Correct 1 ms 2672 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2760 KB Output is correct
15 Correct 68 ms 5352 KB Output is correct
16 Correct 56 ms 5364 KB Output is correct
17 Correct 57 ms 5360 KB Output is correct
18 Correct 10 ms 5572 KB Output is correct
19 Correct 10 ms 5060 KB Output is correct
20 Correct 76 ms 5548 KB Output is correct
21 Correct 67 ms 5444 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 12 ms 4904 KB Output is correct
24 Correct 10 ms 4932 KB Output is correct
25 Correct 2 ms 2676 KB Output is correct
26 Correct 2 ms 2792 KB Output is correct
27 Correct 10 ms 5708 KB Output is correct
28 Correct 12 ms 5112 KB Output is correct
29 Correct 9 ms 4980 KB Output is correct
30 Correct 10 ms 4992 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 2 ms 2672 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2672 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 238 ms 38524 KB Output is correct
2 Correct 1 ms 2764 KB Output is correct
3 Correct 262 ms 38084 KB Output is correct
4 Correct 204 ms 37584 KB Output is correct
5 Correct 321 ms 38704 KB Output is correct
6 Correct 257 ms 38628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 31304 KB Output is correct
2 Correct 189 ms 25880 KB Output is correct
3 Correct 246 ms 25108 KB Output is correct
4 Correct 216 ms 25196 KB Output is correct
5 Correct 229 ms 25252 KB Output is correct
6 Correct 160 ms 31488 KB Output is correct
7 Correct 175 ms 31336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 167 ms 31304 KB Output is correct
2 Correct 189 ms 25880 KB Output is correct
3 Correct 246 ms 25108 KB Output is correct
4 Correct 216 ms 25196 KB Output is correct
5 Correct 229 ms 25252 KB Output is correct
6 Correct 160 ms 31488 KB Output is correct
7 Correct 175 ms 31336 KB Output is correct
8 Correct 268 ms 31464 KB Output is correct
9 Correct 212 ms 31468 KB Output is correct
10 Correct 296 ms 38752 KB Output is correct
11 Correct 242 ms 38576 KB Output is correct
12 Correct 224 ms 25564 KB Output is correct
13 Correct 243 ms 25636 KB Output is correct
14 Correct 206 ms 25616 KB Output is correct
15 Correct 213 ms 25560 KB Output is correct
16 Correct 224 ms 25704 KB Output is correct
17 Correct 229 ms 25684 KB Output is correct
18 Correct 237 ms 25752 KB Output is correct
19 Correct 187 ms 26404 KB Output is correct
20 Correct 163 ms 32224 KB Output is correct
21 Correct 162 ms 32264 KB Output is correct
22 Correct 153 ms 32184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2764 KB Output is correct
2 Correct 9 ms 4984 KB Output is correct
3 Correct 8 ms 5004 KB Output is correct
4 Correct 1 ms 2764 KB Output is correct
5 Correct 2 ms 2764 KB Output is correct
6 Correct 9 ms 5560 KB Output is correct
7 Correct 9 ms 5080 KB Output is correct
8 Correct 8 ms 4992 KB Output is correct
9 Correct 8 ms 4980 KB Output is correct
10 Correct 1 ms 2764 KB Output is correct
11 Correct 1 ms 2672 KB Output is correct
12 Correct 2 ms 2668 KB Output is correct
13 Correct 2 ms 2764 KB Output is correct
14 Correct 2 ms 2760 KB Output is correct
15 Correct 68 ms 5352 KB Output is correct
16 Correct 56 ms 5364 KB Output is correct
17 Correct 57 ms 5360 KB Output is correct
18 Correct 10 ms 5572 KB Output is correct
19 Correct 10 ms 5060 KB Output is correct
20 Correct 76 ms 5548 KB Output is correct
21 Correct 67 ms 5444 KB Output is correct
22 Correct 2 ms 2764 KB Output is correct
23 Correct 12 ms 4904 KB Output is correct
24 Correct 10 ms 4932 KB Output is correct
25 Correct 2 ms 2676 KB Output is correct
26 Correct 2 ms 2792 KB Output is correct
27 Correct 10 ms 5708 KB Output is correct
28 Correct 12 ms 5112 KB Output is correct
29 Correct 9 ms 4980 KB Output is correct
30 Correct 10 ms 4992 KB Output is correct
31 Correct 2 ms 2764 KB Output is correct
32 Correct 2 ms 2672 KB Output is correct
33 Correct 2 ms 2764 KB Output is correct
34 Correct 2 ms 2672 KB Output is correct
35 Correct 2 ms 2764 KB Output is correct
36 Correct 238 ms 38524 KB Output is correct
37 Correct 1 ms 2764 KB Output is correct
38 Correct 262 ms 38084 KB Output is correct
39 Correct 204 ms 37584 KB Output is correct
40 Correct 321 ms 38704 KB Output is correct
41 Correct 257 ms 38628 KB Output is correct
42 Correct 167 ms 31304 KB Output is correct
43 Correct 189 ms 25880 KB Output is correct
44 Correct 246 ms 25108 KB Output is correct
45 Correct 216 ms 25196 KB Output is correct
46 Correct 229 ms 25252 KB Output is correct
47 Correct 160 ms 31488 KB Output is correct
48 Correct 175 ms 31336 KB Output is correct
49 Correct 268 ms 31464 KB Output is correct
50 Correct 212 ms 31468 KB Output is correct
51 Correct 296 ms 38752 KB Output is correct
52 Correct 242 ms 38576 KB Output is correct
53 Correct 224 ms 25564 KB Output is correct
54 Correct 243 ms 25636 KB Output is correct
55 Correct 206 ms 25616 KB Output is correct
56 Correct 213 ms 25560 KB Output is correct
57 Correct 224 ms 25704 KB Output is correct
58 Correct 229 ms 25684 KB Output is correct
59 Correct 237 ms 25752 KB Output is correct
60 Correct 187 ms 26404 KB Output is correct
61 Correct 163 ms 32224 KB Output is correct
62 Correct 162 ms 32264 KB Output is correct
63 Correct 153 ms 32184 KB Output is correct
64 Correct 181 ms 32132 KB Output is correct
65 Correct 300 ms 25792 KB Output is correct
66 Correct 323 ms 25636 KB Output is correct
67 Correct 256 ms 25592 KB Output is correct
68 Correct 162 ms 25512 KB Output is correct
69 Correct 185 ms 25400 KB Output is correct
70 Correct 206 ms 32408 KB Output is correct
71 Correct 225 ms 31860 KB Output is correct
72 Correct 2 ms 2672 KB Output is correct
73 Correct 11 ms 4992 KB Output is correct
74 Correct 10 ms 4940 KB Output is correct
75 Correct 2 ms 2676 KB Output is correct
76 Correct 1 ms 2764 KB Output is correct
77 Correct 9 ms 5696 KB Output is correct
78 Correct 8 ms 5160 KB Output is correct
79 Correct 8 ms 4968 KB Output is correct
80 Correct 12 ms 5032 KB Output is correct
81 Correct 1 ms 2764 KB Output is correct
82 Correct 1 ms 2764 KB Output is correct
83 Correct 1 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 61 ms 5272 KB Output is correct
87 Correct 59 ms 5340 KB Output is correct
88 Correct 61 ms 5400 KB Output is correct
89 Correct 13 ms 5632 KB Output is correct
90 Correct 10 ms 5120 KB Output is correct
91 Correct 66 ms 5552 KB Output is correct
92 Correct 67 ms 5388 KB Output is correct
93 Correct 2 ms 2676 KB Output is correct
94 Correct 218 ms 38968 KB Output is correct
95 Correct 283 ms 38604 KB Output is correct
96 Correct 221 ms 38108 KB Output is correct
97 Correct 365 ms 38984 KB Output is correct
98 Correct 258 ms 39384 KB Output is correct
99 Correct 299 ms 25676 KB Output is correct
100 Correct 214 ms 25832 KB Output is correct
101 Correct 265 ms 25692 KB Output is correct
102 Correct 184 ms 26308 KB Output is correct
103 Correct 148 ms 31988 KB Output is correct
104 Correct 158 ms 32348 KB Output is correct
105 Correct 166 ms 31852 KB Output is correct
106 Correct 232 ms 32156 KB Output is correct
107 Correct 228 ms 31984 KB Output is correct
108 Correct 244 ms 39036 KB Output is correct
109 Correct 275 ms 39040 KB Output is correct
110 Correct 202 ms 25476 KB Output is correct
111 Correct 240 ms 25592 KB Output is correct
112 Correct 216 ms 25664 KB Output is correct
113 Correct 198 ms 25536 KB Output is correct