답안 #1040245

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040245 2024-07-31T19:44:55 Z soroostab Railway (BOI17_railway) C++14
100 / 100
67 ms 32200 KB
#include <bits/stdc++.h>
#define ll long long int
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pcc pair<char,char>
#define pbb pair<bool,bool>
#define PB push_back
#define PF push_front
#define MOD 1000000007
#define intxt freopen("input.txt","r",stdin)
#define outtxt freopen("output.txt","w",stdout)

using namespace std;

const int MAXN = 1e5 + 10;
ll n ,q ,k ,q1 ,q2 ,qq ,x = 1;
ll par[MAXN][20] ,h[MAXN] ,st[MAXN];
ll change[MAXN] ,ans[MAXN];
vector <pii> adj[MAXN];
vector <int> isvalid;

inline void dfs(int v , int p = 0) {
	st[v] = x;
	x++;
	for(auto u : adj[v]) {
		if(u.f == p) {
			continue;
		}
		h[u.f] = h[v] + 1;
		par[u.f][0] = v;
		dfs(u.f , v);
	}
}

inline ll bridge_cost(int v , int p = 0) {
	int cnt = change[v];
	for(auto u : adj[v]) {
		if(u.f == p) {
			continue;
		}
		ans[u.s] = bridge_cost(u.f ,v);
		cnt += ans[u.s];
		if(ans[u.s] >= (2 * k)) {
			isvalid.PB(u.s);
		}
	}
	return cnt;
}

ll LCA(int v , int u) {
	if(h[u] < h[v]) {
		swap(u ,v);
	}
	
	for(int i = 17 ; i >= 0 ; i--) {
		if(h[par[u][i]] >= h[v]) {
			u = par[u][i];
		}
	}
	if(u == v) {
		return v;
	}
	
	for(int i = 17 ; i >= 0 ; i--) {
		if(par[u][i] != par[v][i]) {
			u = par[u][i];
			v = par[v][i];
		}
	}
	return par[v][0];
}

bool cmp(const int u , const int v) {
	return st[v] < st[u];
}

int main() {

	ios_base::sync_with_stdio(0);
	
	cin >> n >> q >> k;
	
	for(int i = 1 ; i < n ; i++) {
		cin >> q1 >> q2;
		adj[q1].PB(make_pair(q2 , i));
		adj[q2].PB(make_pair(q1 , i));
	}
	
	dfs(1);
	
	for(int j = 1 ; j <= 17 ; j++) {
		for(int i = 1 ; i <= n ; i++) {
			par[i][j] = par[par[i][j - 1]][j - 1];
		}
	}
	
	while(q--) {
		
		cin >> qq;
		
		int a[qq + 10];
		for(int i = 1 ; i <= qq ; i++) {
			cin >> a[i];
		}
		sort(a + 1 ,a + qq + 1 ,cmp);
		
		for(int i = 1 ; i < qq ; i++) {
			change[LCA(a[i] ,a[i + 1])] -= 2;
			change[a[i]]++;
			change[a[i + 1]]++;
		}
		change[LCA(a[qq] ,a[1])] -= 2;
		change[a[qq]]++;
		change[a[1]]++;
		
	}
	
	bridge_cost(1);
	
	sort(isvalid.begin() ,isvalid.end());
	cout << isvalid.size() << endl;
	for(auto x : isvalid) {
		cout << x << " ";
	}
	
}
// Hello
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 5 ms 5480 KB Output is correct
7 Correct 4 ms 5128 KB Output is correct
8 Correct 4 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 5 ms 5480 KB Output is correct
7 Correct 4 ms 5128 KB Output is correct
8 Correct 4 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 17 ms 5468 KB Output is correct
16 Correct 17 ms 5420 KB Output is correct
17 Correct 17 ms 5464 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 3 ms 5060 KB Output is correct
20 Correct 18 ms 5548 KB Output is correct
21 Correct 23 ms 5724 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 4 ms 4956 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 4 ms 5324 KB Output is correct
28 Correct 4 ms 4996 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 4 ms 5212 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 62 ms 31888 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 60 ms 31572 KB Output is correct
4 Correct 59 ms 30924 KB Output is correct
5 Correct 67 ms 32012 KB Output is correct
6 Correct 63 ms 32200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 28748 KB Output is correct
2 Correct 47 ms 27216 KB Output is correct
3 Correct 52 ms 26960 KB Output is correct
4 Correct 51 ms 26960 KB Output is correct
5 Correct 59 ms 26964 KB Output is correct
6 Correct 49 ms 28896 KB Output is correct
7 Correct 51 ms 28752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 28748 KB Output is correct
2 Correct 47 ms 27216 KB Output is correct
3 Correct 52 ms 26960 KB Output is correct
4 Correct 51 ms 26960 KB Output is correct
5 Correct 59 ms 26964 KB Output is correct
6 Correct 49 ms 28896 KB Output is correct
7 Correct 51 ms 28752 KB Output is correct
8 Correct 53 ms 29268 KB Output is correct
9 Correct 54 ms 29184 KB Output is correct
10 Correct 54 ms 31944 KB Output is correct
11 Correct 58 ms 31888 KB Output is correct
12 Correct 47 ms 26964 KB Output is correct
13 Correct 48 ms 26708 KB Output is correct
14 Correct 48 ms 26964 KB Output is correct
15 Correct 52 ms 26872 KB Output is correct
16 Correct 48 ms 26960 KB Output is correct
17 Correct 50 ms 27084 KB Output is correct
18 Correct 49 ms 26960 KB Output is correct
19 Correct 49 ms 27228 KB Output is correct
20 Correct 46 ms 29164 KB Output is correct
21 Correct 47 ms 29136 KB Output is correct
22 Correct 47 ms 29176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 4 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
6 Correct 5 ms 5480 KB Output is correct
7 Correct 4 ms 5128 KB Output is correct
8 Correct 4 ms 5212 KB Output is correct
9 Correct 4 ms 5212 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 1 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 1 ms 2652 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 17 ms 5468 KB Output is correct
16 Correct 17 ms 5420 KB Output is correct
17 Correct 17 ms 5464 KB Output is correct
18 Correct 4 ms 5212 KB Output is correct
19 Correct 3 ms 5060 KB Output is correct
20 Correct 18 ms 5548 KB Output is correct
21 Correct 23 ms 5724 KB Output is correct
22 Correct 1 ms 2652 KB Output is correct
23 Correct 4 ms 4956 KB Output is correct
24 Correct 4 ms 4956 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 4 ms 5324 KB Output is correct
28 Correct 4 ms 4996 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 4 ms 5212 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 1 ms 2652 KB Output is correct
34 Correct 1 ms 2652 KB Output is correct
35 Correct 1 ms 2652 KB Output is correct
36 Correct 62 ms 31888 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 60 ms 31572 KB Output is correct
39 Correct 59 ms 30924 KB Output is correct
40 Correct 67 ms 32012 KB Output is correct
41 Correct 63 ms 32200 KB Output is correct
42 Correct 43 ms 28748 KB Output is correct
43 Correct 47 ms 27216 KB Output is correct
44 Correct 52 ms 26960 KB Output is correct
45 Correct 51 ms 26960 KB Output is correct
46 Correct 59 ms 26964 KB Output is correct
47 Correct 49 ms 28896 KB Output is correct
48 Correct 51 ms 28752 KB Output is correct
49 Correct 53 ms 29268 KB Output is correct
50 Correct 54 ms 29184 KB Output is correct
51 Correct 54 ms 31944 KB Output is correct
52 Correct 58 ms 31888 KB Output is correct
53 Correct 47 ms 26964 KB Output is correct
54 Correct 48 ms 26708 KB Output is correct
55 Correct 48 ms 26964 KB Output is correct
56 Correct 52 ms 26872 KB Output is correct
57 Correct 48 ms 26960 KB Output is correct
58 Correct 50 ms 27084 KB Output is correct
59 Correct 49 ms 26960 KB Output is correct
60 Correct 49 ms 27228 KB Output is correct
61 Correct 46 ms 29164 KB Output is correct
62 Correct 47 ms 29136 KB Output is correct
63 Correct 47 ms 29176 KB Output is correct
64 Correct 48 ms 29412 KB Output is correct
65 Correct 60 ms 26964 KB Output is correct
66 Correct 56 ms 26960 KB Output is correct
67 Correct 52 ms 26968 KB Output is correct
68 Correct 36 ms 26604 KB Output is correct
69 Correct 36 ms 26712 KB Output is correct
70 Correct 54 ms 29648 KB Output is correct
71 Correct 54 ms 29008 KB Output is correct
72 Correct 1 ms 2652 KB Output is correct
73 Correct 4 ms 4948 KB Output is correct
74 Correct 4 ms 4956 KB Output is correct
75 Correct 1 ms 2652 KB Output is correct
76 Correct 1 ms 2652 KB Output is correct
77 Correct 4 ms 5468 KB Output is correct
78 Correct 4 ms 5044 KB Output is correct
79 Correct 3 ms 4956 KB Output is correct
80 Correct 4 ms 5212 KB Output is correct
81 Correct 1 ms 2648 KB Output is correct
82 Correct 2 ms 2648 KB Output is correct
83 Correct 1 ms 2652 KB Output is correct
84 Correct 1 ms 2652 KB Output is correct
85 Correct 1 ms 2652 KB Output is correct
86 Correct 16 ms 5384 KB Output is correct
87 Correct 16 ms 5464 KB Output is correct
88 Correct 16 ms 5468 KB Output is correct
89 Correct 5 ms 5208 KB Output is correct
90 Correct 4 ms 5212 KB Output is correct
91 Correct 18 ms 5596 KB Output is correct
92 Correct 20 ms 5464 KB Output is correct
93 Correct 1 ms 2652 KB Output is correct
94 Correct 62 ms 31932 KB Output is correct
95 Correct 57 ms 31440 KB Output is correct
96 Correct 56 ms 30800 KB Output is correct
97 Correct 56 ms 31692 KB Output is correct
98 Correct 53 ms 32200 KB Output is correct
99 Correct 52 ms 26964 KB Output is correct
100 Correct 62 ms 26964 KB Output is correct
101 Correct 54 ms 26972 KB Output is correct
102 Correct 50 ms 27264 KB Output is correct
103 Correct 45 ms 28752 KB Output is correct
104 Correct 45 ms 29108 KB Output is correct
105 Correct 48 ms 28756 KB Output is correct
106 Correct 58 ms 29540 KB Output is correct
107 Correct 54 ms 29152 KB Output is correct
108 Correct 52 ms 31948 KB Output is correct
109 Correct 54 ms 31972 KB Output is correct
110 Correct 43 ms 26704 KB Output is correct
111 Correct 50 ms 26792 KB Output is correct
112 Correct 51 ms 26804 KB Output is correct
113 Correct 51 ms 26960 KB Output is correct