답안 #758104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
758104 2023-06-14T07:24:24 Z MetalPower Railway (BOI17_railway) C++14
100 / 100
303 ms 32552 KB
#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define fi first
#define se second

const int MX = 2e5 + 10;
const int LG = 19;

struct segtree{
	int st[MX << 1];

	void upd(int l, int r, int v){
		for(l += MX, r += MX + 1; l < r; l >>= 1, r >>= 1){
			if(l & 1) st[l++] += v;
			if(r & 1) st[--r] += v;
		}
	}

	int que(int p){
		int res = 0;
		for(p += MX; p > 0; p >>= 1) res += st[p];
		return res;
	}
} S;

struct segversion{
	int st[MX << 1];

	void upd(int l, int r, int v){
		for(l += MX, r += MX + 1; l < r; l >>= 1, r >>= 1){
			if(l & 1) st[l++] = v;
			if(r & 1) st[--r] = v;
		}
	}

	int que(int p){
		int res = 0;
		for(p += MX; p > 0; p >>= 1) res = max(res, st[p]);
		return res;
	}
} T;

vector<int> adj[MX];
int N, M, K, par[MX], head[MX], sp[MX][LG];
int sz[MX], dep[MX], in[MX], out[MX], tim = 0;

void dfs(int u, int v){
	if(v != 0) adj[u].erase(find(adj[u].begin(), adj[u].end(), v));

	sz[u] = 1; dep[u] = dep[v] + 1;
	sp[u][0] = par[u] = v;
	for(int i = 1; i < LG; i++) sp[u][i] = sp[sp[u][i - 1]][i - 1];

	for(int& nxt : adj[u]){
		dfs(nxt, u);
		sz[u] += sz[nxt];
		if(sz[nxt] > sz[adj[u][0]]) swap(nxt, adj[u][0]);
	}
}

void dfs2(int u){
	in[u] = ++tim;
	for(int nxt : adj[u]){
		if(nxt == adj[u][0]) head[nxt] = head[u];
		else head[nxt] = nxt;
		dfs2(nxt);
	}
	out[u] = tim;
}

void init(){
	dfs(1, 0);
	head[1] = 1;
	dfs2(1);
}

int lca(int u, int v){
	if(dep[u] > dep[v]) swap(u, v);

	int df = dep[v] - dep[u];
	for(int j = 0; j < LG; j++){
		if(df >> j & 1) v = sp[v][j];
	}

	if(u == v) return u;

	for(int j = LG - 1; j >= 0; j--){
		if(sp[u][j] != sp[v][j]){
			u = sp[u][j];
			v = sp[v][j];
		}
	}

	return sp[u][0];
}

void upd(int u, int v, int val){
	for(; head[u] != head[v]; v = par[head[v]]){
		if(dep[u] > dep[v]) swap(u, v);
		S.upd(in[head[v]], in[v], 1);
		T.upd(in[head[v]], in[v], val);
	}
	if(dep[u] > dep[v]) swap(u, v);
	S.upd(in[u], in[v], 1);
	T.upd(in[u], in[v], val);
}

vector<pair<pii, int>> edges;

int find_rep(int u, int root, int ver){ // find ancestor of u that is lower than root and off
	for(int i = LG - 1; i >= 0; i--){
		if(dep[sp[u][i]] > dep[root] && T.que(in[sp[u][i]]) != ver) u = sp[u][i];
	}
	return u;
}

int idx_edge[MX];

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

	cin >> N >> M >> K;
	for(int i = 1; i < N; i++){
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		edges.push_back({{u, v}, i});
	}

	init();
	for(auto edge : edges){
		if(dep[edge.fi.fi] > dep[edge.fi.se]) idx_edge[edge.fi.fi] = edge.se;
		else if(dep[edge.fi.fi] < dep[edge.fi.se]) idx_edge[edge.fi.se] = edge.se;
	}

	for(int i = 1; i <= M; i++){

		int s; cin >> s;
		vector<int> curr;

		int root = -1;
		for(int j = 1; j <= s; j++){
			int nd; cin >> nd;
			if(root == -1) root = nd;
			else root = lca(root, nd);
			curr.push_back(nd);
		}

		T.upd(in[root], in[root], i);
		for(int x : curr){
			if(x == root || T.que(in[x]) == i) continue;
			int nd = find_rep(x, root, i);
			upd(x, nd, i);
		}
	}

	vector<int> answ;

	for(int i = 1; i <= N; i++){
		if(S.que(in[i]) >= K) answ.push_back(idx_edge[i]);
	}

	sort(answ.begin(), answ.end());

	cout << answ.size() << '\n';
	for(int x : answ) cout << x << " ";
	cout << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 10 ms 6740 KB Output is correct
3 Correct 8 ms 6740 KB Output is correct
4 Correct 3 ms 4976 KB Output is correct
5 Correct 3 ms 5044 KB Output is correct
6 Correct 9 ms 7252 KB Output is correct
7 Correct 9 ms 6868 KB Output is correct
8 Correct 7 ms 6740 KB Output is correct
9 Correct 8 ms 6844 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 10 ms 6740 KB Output is correct
3 Correct 8 ms 6740 KB Output is correct
4 Correct 3 ms 4976 KB Output is correct
5 Correct 3 ms 5044 KB Output is correct
6 Correct 9 ms 7252 KB Output is correct
7 Correct 9 ms 6868 KB Output is correct
8 Correct 7 ms 6740 KB Output is correct
9 Correct 8 ms 6844 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 30 ms 7180 KB Output is correct
16 Correct 31 ms 7196 KB Output is correct
17 Correct 35 ms 7244 KB Output is correct
18 Correct 10 ms 7232 KB Output is correct
19 Correct 9 ms 6856 KB Output is correct
20 Correct 24 ms 7232 KB Output is correct
21 Correct 29 ms 7124 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 9 ms 6740 KB Output is correct
24 Correct 8 ms 6740 KB Output is correct
25 Correct 3 ms 5040 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 9 ms 7228 KB Output is correct
28 Correct 8 ms 6868 KB Output is correct
29 Correct 7 ms 6716 KB Output is correct
30 Correct 9 ms 6840 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5048 KB Output is correct
33 Correct 3 ms 4992 KB Output is correct
34 Correct 3 ms 5040 KB Output is correct
35 Correct 3 ms 5048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 168 ms 32172 KB Output is correct
2 Correct 3 ms 5040 KB Output is correct
3 Correct 170 ms 31400 KB Output is correct
4 Correct 155 ms 30712 KB Output is correct
5 Correct 303 ms 31996 KB Output is correct
6 Correct 187 ms 32552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 25468 KB Output is correct
2 Correct 109 ms 21912 KB Output is correct
3 Correct 106 ms 21780 KB Output is correct
4 Correct 117 ms 22228 KB Output is correct
5 Correct 122 ms 22424 KB Output is correct
6 Correct 112 ms 25800 KB Output is correct
7 Correct 104 ms 25600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 25468 KB Output is correct
2 Correct 109 ms 21912 KB Output is correct
3 Correct 106 ms 21780 KB Output is correct
4 Correct 117 ms 22228 KB Output is correct
5 Correct 122 ms 22424 KB Output is correct
6 Correct 112 ms 25800 KB Output is correct
7 Correct 104 ms 25600 KB Output is correct
8 Correct 141 ms 26888 KB Output is correct
9 Correct 151 ms 27020 KB Output is correct
10 Correct 197 ms 32124 KB Output is correct
11 Correct 195 ms 32100 KB Output is correct
12 Correct 98 ms 22116 KB Output is correct
13 Correct 74 ms 22128 KB Output is correct
14 Correct 103 ms 22372 KB Output is correct
15 Correct 123 ms 22480 KB Output is correct
16 Correct 114 ms 22584 KB Output is correct
17 Correct 104 ms 22032 KB Output is correct
18 Correct 120 ms 22092 KB Output is correct
19 Correct 121 ms 21972 KB Output is correct
20 Correct 115 ms 25900 KB Output is correct
21 Correct 117 ms 26088 KB Output is correct
22 Correct 112 ms 25900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 10 ms 6740 KB Output is correct
3 Correct 8 ms 6740 KB Output is correct
4 Correct 3 ms 4976 KB Output is correct
5 Correct 3 ms 5044 KB Output is correct
6 Correct 9 ms 7252 KB Output is correct
7 Correct 9 ms 6868 KB Output is correct
8 Correct 7 ms 6740 KB Output is correct
9 Correct 8 ms 6844 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5044 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 5076 KB Output is correct
15 Correct 30 ms 7180 KB Output is correct
16 Correct 31 ms 7196 KB Output is correct
17 Correct 35 ms 7244 KB Output is correct
18 Correct 10 ms 7232 KB Output is correct
19 Correct 9 ms 6856 KB Output is correct
20 Correct 24 ms 7232 KB Output is correct
21 Correct 29 ms 7124 KB Output is correct
22 Correct 4 ms 5076 KB Output is correct
23 Correct 9 ms 6740 KB Output is correct
24 Correct 8 ms 6740 KB Output is correct
25 Correct 3 ms 5040 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 9 ms 7228 KB Output is correct
28 Correct 8 ms 6868 KB Output is correct
29 Correct 7 ms 6716 KB Output is correct
30 Correct 9 ms 6840 KB Output is correct
31 Correct 3 ms 5076 KB Output is correct
32 Correct 3 ms 5048 KB Output is correct
33 Correct 3 ms 4992 KB Output is correct
34 Correct 3 ms 5040 KB Output is correct
35 Correct 3 ms 5048 KB Output is correct
36 Correct 168 ms 32172 KB Output is correct
37 Correct 3 ms 5040 KB Output is correct
38 Correct 170 ms 31400 KB Output is correct
39 Correct 155 ms 30712 KB Output is correct
40 Correct 303 ms 31996 KB Output is correct
41 Correct 187 ms 32552 KB Output is correct
42 Correct 114 ms 25468 KB Output is correct
43 Correct 109 ms 21912 KB Output is correct
44 Correct 106 ms 21780 KB Output is correct
45 Correct 117 ms 22228 KB Output is correct
46 Correct 122 ms 22424 KB Output is correct
47 Correct 112 ms 25800 KB Output is correct
48 Correct 104 ms 25600 KB Output is correct
49 Correct 141 ms 26888 KB Output is correct
50 Correct 151 ms 27020 KB Output is correct
51 Correct 197 ms 32124 KB Output is correct
52 Correct 195 ms 32100 KB Output is correct
53 Correct 98 ms 22116 KB Output is correct
54 Correct 74 ms 22128 KB Output is correct
55 Correct 103 ms 22372 KB Output is correct
56 Correct 123 ms 22480 KB Output is correct
57 Correct 114 ms 22584 KB Output is correct
58 Correct 104 ms 22032 KB Output is correct
59 Correct 120 ms 22092 KB Output is correct
60 Correct 121 ms 21972 KB Output is correct
61 Correct 115 ms 25900 KB Output is correct
62 Correct 117 ms 26088 KB Output is correct
63 Correct 112 ms 25900 KB Output is correct
64 Correct 114 ms 27060 KB Output is correct
65 Correct 139 ms 23032 KB Output is correct
66 Correct 144 ms 22836 KB Output is correct
67 Correct 149 ms 22800 KB Output is correct
68 Correct 78 ms 22144 KB Output is correct
69 Correct 78 ms 22152 KB Output is correct
70 Correct 128 ms 27516 KB Output is correct
71 Correct 167 ms 26832 KB Output is correct
72 Correct 4 ms 5076 KB Output is correct
73 Correct 9 ms 6772 KB Output is correct
74 Correct 8 ms 6856 KB Output is correct
75 Correct 3 ms 5076 KB Output is correct
76 Correct 3 ms 5076 KB Output is correct
77 Correct 9 ms 7252 KB Output is correct
78 Correct 8 ms 6868 KB Output is correct
79 Correct 9 ms 6716 KB Output is correct
80 Correct 10 ms 6804 KB Output is correct
81 Correct 3 ms 5076 KB Output is correct
82 Correct 3 ms 5044 KB Output is correct
83 Correct 3 ms 5036 KB Output is correct
84 Correct 3 ms 5076 KB Output is correct
85 Correct 3 ms 5076 KB Output is correct
86 Correct 30 ms 7124 KB Output is correct
87 Correct 30 ms 7196 KB Output is correct
88 Correct 30 ms 7244 KB Output is correct
89 Correct 9 ms 7252 KB Output is correct
90 Correct 8 ms 6856 KB Output is correct
91 Correct 28 ms 7252 KB Output is correct
92 Correct 25 ms 7232 KB Output is correct
93 Correct 3 ms 5072 KB Output is correct
94 Correct 187 ms 31984 KB Output is correct
95 Correct 169 ms 31396 KB Output is correct
96 Correct 173 ms 30736 KB Output is correct
97 Correct 260 ms 32004 KB Output is correct
98 Correct 206 ms 32432 KB Output is correct
99 Correct 129 ms 22204 KB Output is correct
100 Correct 106 ms 21972 KB Output is correct
101 Correct 119 ms 22404 KB Output is correct
102 Correct 104 ms 21940 KB Output is correct
103 Correct 108 ms 25544 KB Output is correct
104 Correct 106 ms 26028 KB Output is correct
105 Correct 106 ms 25552 KB Output is correct
106 Correct 143 ms 27112 KB Output is correct
107 Correct 168 ms 26920 KB Output is correct
108 Correct 192 ms 32240 KB Output is correct
109 Correct 199 ms 32156 KB Output is correct
110 Correct 84 ms 22048 KB Output is correct
111 Correct 74 ms 22168 KB Output is correct
112 Correct 106 ms 22388 KB Output is correct
113 Correct 133 ms 22424 KB Output is correct