답안 #1083940

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1083940 2024-09-04T15:20:54 Z EkinOnal Railway (BOI17_railway) C++17
100 / 100
94 ms 30568 KB
//#pragma GCC optimize("O3,unroll-loops,Ofast")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
 
#define MAX 100007
#define pb push_back
#define mp make_pair 
//#define int long long
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>> 
const int mod = 1e9+7;
//const int INF = 1e18;
// myMap.begin()->first :  key
// myMap.begin()->second : value
 
int epow(int a,int b){int ans=1;while(b){if(b&1) ans*=a;a*=a;b>>=1;ans%=mod;a%=mod;}return (ans+mod)%mod;}
int gcd(int a,int b) {if(a<b)swap(a,b);while(b){int tmp=b;b=a%b;a=tmp;}return a;}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
int sum(int a,int b){return ((a%mod)+(b%mod))%mod;}

typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

vi dep(MAX),tin(MAX),tout(MAX),pare(MAX);
vpii adj[MAX];
vector<vi> p(MAX,vi(22));
int timer=0;
void tour(int node,int par){
	tin[node]=timer++;
	dep[node]=dep[par]+1;
	p[node][0]=par;
	for(auto u : adj[node]){
		if(u.f==par) continue;
		pare[u.f]=u.s;
		tour(u.f,node);
	}
	tout[node]=timer-1;
}

int find_lca(int a,int b){
	if(dep[a]<dep[b]) swap(a,b);
	int fark=dep[a]-dep[b];
	for(int i=20;i>=0;i--){
		if((1LL<<i)<=fark){
			fark-=(1LL<<i);
			a=p[a][i];
		}
	}
	if(a==b) return a;
	for(int i=20;i>=0;i--){
		if(p[a][i]!=p[b][i]){
			a=p[a][i];
			b=p[b][i];
		}
	}
	return p[a][0];
}

vi val(MAX),ans;
int n,m,k;
void dfs(int node,int par){
	for(auto u : adj[node]){
		if(u.f==par) continue;
		dfs(u.f,node);
		val[node]+=val[u.f];
	}
	if(val[node]>=2*k) ans.pb(pare[node]);
}

void solve(){
	cin>>n>>m>>k;
	for(int i=1;i<n;i++){
		int a,b; cin>>a>>b;
		adj[a].pb({b,i}); adj[b].pb({a,i});
	}

	tour(1,1);
	for(int i=1;i<=20;i++){
		for(int node=1;node<=n;node++) p[node][i]=p[p[node][i-1]][i-1];
	}
	
	while(m--){
		int s; cin>>s;
		vi v(s); for(int i=0;i<s;i++) cin>>v[i];
		sort(v.begin(),v.end(),[&](int a,int b)-> bool {
			return tin[a]<tin[b];
		});

		v.pb(v[0]);
		for(int i=0;i<s;i++){
			val[v[i]]++;
			val[v[i+1]]++;
			val[find_lca(v[i],v[i+1])]-=2;
		}
	}
	dfs(1,1);
	sort(ans.begin(),ans.end());
	cout<<ans.size()<<endl;
	for(auto u : ans) cout<<u<<" ";

	return;
}

int32_t main() {
//	freopen("in.in", "r", stdin);
//	freopen("out.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(0);   
    int t=1;
//    cin >> t;
    while (t--) solve();
 
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16472 KB Output is correct
2 Correct 12 ms 16988 KB Output is correct
3 Correct 14 ms 16988 KB Output is correct
4 Correct 8 ms 16476 KB Output is correct
5 Correct 9 ms 16408 KB Output is correct
6 Correct 15 ms 17304 KB Output is correct
7 Correct 12 ms 16988 KB Output is correct
8 Correct 14 ms 16988 KB Output is correct
9 Correct 13 ms 16988 KB Output is correct
10 Correct 9 ms 16476 KB Output is correct
11 Correct 9 ms 16476 KB Output is correct
12 Correct 10 ms 16476 KB Output is correct
13 Correct 10 ms 16324 KB Output is correct
14 Correct 10 ms 16500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16472 KB Output is correct
2 Correct 12 ms 16988 KB Output is correct
3 Correct 14 ms 16988 KB Output is correct
4 Correct 8 ms 16476 KB Output is correct
5 Correct 9 ms 16408 KB Output is correct
6 Correct 15 ms 17304 KB Output is correct
7 Correct 12 ms 16988 KB Output is correct
8 Correct 14 ms 16988 KB Output is correct
9 Correct 13 ms 16988 KB Output is correct
10 Correct 9 ms 16476 KB Output is correct
11 Correct 9 ms 16476 KB Output is correct
12 Correct 10 ms 16476 KB Output is correct
13 Correct 10 ms 16324 KB Output is correct
14 Correct 10 ms 16500 KB Output is correct
15 Correct 27 ms 17352 KB Output is correct
16 Correct 24 ms 17244 KB Output is correct
17 Correct 25 ms 17244 KB Output is correct
18 Correct 19 ms 17244 KB Output is correct
19 Correct 12 ms 16984 KB Output is correct
20 Correct 21 ms 17448 KB Output is correct
21 Correct 33 ms 17244 KB Output is correct
22 Correct 9 ms 16476 KB Output is correct
23 Correct 16 ms 16984 KB Output is correct
24 Correct 12 ms 16988 KB Output is correct
25 Correct 9 ms 16476 KB Output is correct
26 Correct 8 ms 16472 KB Output is correct
27 Correct 14 ms 17240 KB Output is correct
28 Correct 16 ms 16988 KB Output is correct
29 Correct 14 ms 16984 KB Output is correct
30 Correct 12 ms 16988 KB Output is correct
31 Correct 10 ms 16476 KB Output is correct
32 Correct 10 ms 16524 KB Output is correct
33 Correct 9 ms 16476 KB Output is correct
34 Correct 9 ms 16472 KB Output is correct
35 Correct 9 ms 16472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 29964 KB Output is correct
2 Correct 8 ms 16476 KB Output is correct
3 Correct 80 ms 29588 KB Output is correct
4 Correct 69 ms 29264 KB Output is correct
5 Correct 67 ms 30036 KB Output is correct
6 Correct 67 ms 30448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 25684 KB Output is correct
2 Correct 62 ms 22356 KB Output is correct
3 Correct 66 ms 22024 KB Output is correct
4 Correct 70 ms 21844 KB Output is correct
5 Correct 63 ms 21844 KB Output is correct
6 Correct 60 ms 25940 KB Output is correct
7 Correct 58 ms 25684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 55 ms 25684 KB Output is correct
2 Correct 62 ms 22356 KB Output is correct
3 Correct 66 ms 22024 KB Output is correct
4 Correct 70 ms 21844 KB Output is correct
5 Correct 63 ms 21844 KB Output is correct
6 Correct 60 ms 25940 KB Output is correct
7 Correct 58 ms 25684 KB Output is correct
8 Correct 76 ms 25912 KB Output is correct
9 Correct 66 ms 25936 KB Output is correct
10 Correct 70 ms 30408 KB Output is correct
11 Correct 63 ms 30208 KB Output is correct
12 Correct 48 ms 21588 KB Output is correct
13 Correct 50 ms 21584 KB Output is correct
14 Correct 59 ms 21844 KB Output is correct
15 Correct 72 ms 22096 KB Output is correct
16 Correct 60 ms 21792 KB Output is correct
17 Correct 61 ms 21840 KB Output is correct
18 Correct 59 ms 21844 KB Output is correct
19 Correct 61 ms 22332 KB Output is correct
20 Correct 61 ms 26176 KB Output is correct
21 Correct 59 ms 26192 KB Output is correct
22 Correct 56 ms 26172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16472 KB Output is correct
2 Correct 12 ms 16988 KB Output is correct
3 Correct 14 ms 16988 KB Output is correct
4 Correct 8 ms 16476 KB Output is correct
5 Correct 9 ms 16408 KB Output is correct
6 Correct 15 ms 17304 KB Output is correct
7 Correct 12 ms 16988 KB Output is correct
8 Correct 14 ms 16988 KB Output is correct
9 Correct 13 ms 16988 KB Output is correct
10 Correct 9 ms 16476 KB Output is correct
11 Correct 9 ms 16476 KB Output is correct
12 Correct 10 ms 16476 KB Output is correct
13 Correct 10 ms 16324 KB Output is correct
14 Correct 10 ms 16500 KB Output is correct
15 Correct 27 ms 17352 KB Output is correct
16 Correct 24 ms 17244 KB Output is correct
17 Correct 25 ms 17244 KB Output is correct
18 Correct 19 ms 17244 KB Output is correct
19 Correct 12 ms 16984 KB Output is correct
20 Correct 21 ms 17448 KB Output is correct
21 Correct 33 ms 17244 KB Output is correct
22 Correct 9 ms 16476 KB Output is correct
23 Correct 16 ms 16984 KB Output is correct
24 Correct 12 ms 16988 KB Output is correct
25 Correct 9 ms 16476 KB Output is correct
26 Correct 8 ms 16472 KB Output is correct
27 Correct 14 ms 17240 KB Output is correct
28 Correct 16 ms 16988 KB Output is correct
29 Correct 14 ms 16984 KB Output is correct
30 Correct 12 ms 16988 KB Output is correct
31 Correct 10 ms 16476 KB Output is correct
32 Correct 10 ms 16524 KB Output is correct
33 Correct 9 ms 16476 KB Output is correct
34 Correct 9 ms 16472 KB Output is correct
35 Correct 9 ms 16472 KB Output is correct
36 Correct 94 ms 29964 KB Output is correct
37 Correct 8 ms 16476 KB Output is correct
38 Correct 80 ms 29588 KB Output is correct
39 Correct 69 ms 29264 KB Output is correct
40 Correct 67 ms 30036 KB Output is correct
41 Correct 67 ms 30448 KB Output is correct
42 Correct 55 ms 25684 KB Output is correct
43 Correct 62 ms 22356 KB Output is correct
44 Correct 66 ms 22024 KB Output is correct
45 Correct 70 ms 21844 KB Output is correct
46 Correct 63 ms 21844 KB Output is correct
47 Correct 60 ms 25940 KB Output is correct
48 Correct 58 ms 25684 KB Output is correct
49 Correct 76 ms 25912 KB Output is correct
50 Correct 66 ms 25936 KB Output is correct
51 Correct 70 ms 30408 KB Output is correct
52 Correct 63 ms 30208 KB Output is correct
53 Correct 48 ms 21588 KB Output is correct
54 Correct 50 ms 21584 KB Output is correct
55 Correct 59 ms 21844 KB Output is correct
56 Correct 72 ms 22096 KB Output is correct
57 Correct 60 ms 21792 KB Output is correct
58 Correct 61 ms 21840 KB Output is correct
59 Correct 59 ms 21844 KB Output is correct
60 Correct 61 ms 22332 KB Output is correct
61 Correct 61 ms 26176 KB Output is correct
62 Correct 59 ms 26192 KB Output is correct
63 Correct 56 ms 26172 KB Output is correct
64 Correct 66 ms 25912 KB Output is correct
65 Correct 65 ms 21844 KB Output is correct
66 Correct 72 ms 21960 KB Output is correct
67 Correct 77 ms 21964 KB Output is correct
68 Correct 48 ms 21596 KB Output is correct
69 Correct 45 ms 21664 KB Output is correct
70 Correct 65 ms 26316 KB Output is correct
71 Correct 71 ms 25636 KB Output is correct
72 Correct 8 ms 16472 KB Output is correct
73 Correct 14 ms 16988 KB Output is correct
74 Correct 12 ms 16988 KB Output is correct
75 Correct 9 ms 16492 KB Output is correct
76 Correct 9 ms 16476 KB Output is correct
77 Correct 14 ms 17244 KB Output is correct
78 Correct 13 ms 16988 KB Output is correct
79 Correct 14 ms 16988 KB Output is correct
80 Correct 15 ms 16988 KB Output is correct
81 Correct 10 ms 16728 KB Output is correct
82 Correct 9 ms 16504 KB Output is correct
83 Correct 9 ms 16476 KB Output is correct
84 Correct 8 ms 16352 KB Output is correct
85 Correct 9 ms 16336 KB Output is correct
86 Correct 26 ms 17548 KB Output is correct
87 Correct 24 ms 17240 KB Output is correct
88 Correct 24 ms 17240 KB Output is correct
89 Correct 13 ms 17240 KB Output is correct
90 Correct 12 ms 17052 KB Output is correct
91 Correct 24 ms 17500 KB Output is correct
92 Correct 25 ms 17296 KB Output is correct
93 Correct 9 ms 16472 KB Output is correct
94 Correct 88 ms 30060 KB Output is correct
95 Correct 75 ms 29644 KB Output is correct
96 Correct 67 ms 29060 KB Output is correct
97 Correct 73 ms 29936 KB Output is correct
98 Correct 66 ms 30568 KB Output is correct
99 Correct 63 ms 21844 KB Output is correct
100 Correct 77 ms 22040 KB Output is correct
101 Correct 67 ms 21892 KB Output is correct
102 Correct 69 ms 22360 KB Output is correct
103 Correct 73 ms 25936 KB Output is correct
104 Correct 56 ms 26196 KB Output is correct
105 Correct 57 ms 25680 KB Output is correct
106 Correct 63 ms 25940 KB Output is correct
107 Correct 66 ms 25936 KB Output is correct
108 Correct 64 ms 30156 KB Output is correct
109 Correct 61 ms 30220 KB Output is correct
110 Correct 48 ms 21596 KB Output is correct
111 Correct 47 ms 21584 KB Output is correct
112 Correct 58 ms 21844 KB Output is correct
113 Correct 60 ms 21844 KB Output is correct