#include <bits/stdc++.h>
using namespace std;
// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
// constants...
const double PI = acos(-1);
const ll mod = 1000000007; // 998244353
const ll INF = 1e9+5;
const ll INFLL = 1e18+5;
// defines...
#define PB push_back
#define fi first
#define se second
#define boost_ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
// functions...
ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;}
ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);}
ll modpow(ll a,ll p,ll mod) {ll ans=1;while(p){if(p%2)ans=(ans*a)%mod;a=(a*a)%mod;p/=2;} return ans;}
ll inverse_mod(ll n,ll mod) {return modpow(n,mod-2,mod);}
const int N= 1e5+5,LOG= 18;
int depth[N],up[N][LOG], sz[N];
vector<pii> v[N];
vector<int> col[N], minister[N];
void dfs(int pos,int pre) {
sz[pos] = 1;
for(auto it:v[pos]) {
if(it.fi==pre) continue;
depth[it.fi]=depth[pos]+1;
up[it.fi][0] = pos;
for(int j = 1; j < LOG; j++)
{
up[it.fi][j] = up[up[it.fi][j-1]][j-1];
}
dfs(it.fi,pos);
sz[pos] += sz[it.fi];
}
return ;
}
int kthancestor(int pos,int k) {
for(int i=LOG-1;i>=0;i--) {
if(k&(1<<i))
pos=up[pos][i];
}
return pos;
}
int get_lca(int a, int b) {
if(depth[a] < depth[b]) {
swap(a, b);
}
// 1) Get same depth.
int k = depth[a] - depth[b];
a=kthancestor(a,k);
// 2) if b was ancestor of a then now a==b
if(a == b) {
return a;
}
// 3) move both a and b with powers of two
for(int j = LOG - 1; j >= 0; j--) {
if(up[a][j] != up[b][j]) {
a = up[a][j];
b = up[b][j];
}
}
return up[a][0];
}
int n, m, k;
set<int> subtree[N], ans;
void dfs_seg(int pos, int pre, int ind) {
int big = -1, big_ind = -1;
for(auto it : v[pos]) {
if(it.fi == pre) continue;
if(big == -1 || sz[big] < sz[it.fi])
big = it.fi; big_ind = it.se;
}
if(big != -1) {
dfs_seg(big, pos, big_ind);
swap(subtree[big], subtree[pos]);
}
for(auto it : v[pos]) {
if(it.fi == pre || it.fi == big) continue;
dfs_seg(it.fi, pos, it.se);
for(auto itt : subtree[it.fi]) {
subtree[pos].insert(itt);
}
}
for(auto it : col[pos]) subtree[pos].insert(it);
for(auto it : minister[pos])
subtree[pos].erase(it);
if(subtree[pos].size() >= k) ans.insert(ind);
}
void solve() {
cin >> n >> m >> k;
for(int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
v[x].PB({y, i}); v[y].PB({x, i});
}
dfs(1, 1);
for(int i = 1; i <= m; i++) {
int x, y, lc;
cin >> x;
vector<int> temp(x);
for(int j = 0; j < x; j++) cin >> temp[j];
for(int j = 0; j < x; j++) {
if(j == 0) lc = temp[j];
else lc = get_lca(lc, temp[j]);
// cout << i << lc << endl;
}
minister[lc].PB(i);
for(int j = 0; j < x; j++) {
col[temp[j]].PB(i);
}
}
dfs_seg(1, 1, 0);
ans.erase(0);
cout << ans.size() << endl;
for(auto it : ans) cout << it << " ";
cout << endl;
}
int main()
{
boost_;
int t = 1;
// cin >> t;
for(int i=1;i<=t;i++)
{
// cout<<"Case "<<i<<":\n";
solve();
}
}
Compilation message
railway.cpp: In function 'void dfs_seg(int, int, int)':
railway.cpp:78:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
78 | if(big == -1 || sz[big] < sz[it.fi])
| ^~
railway.cpp:79:22: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
79 | big = it.fi; big_ind = it.se;
| ^~~~~~~
railway.cpp:97:28: warning: comparison of integer expressions of different signedness: 'std::set<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
97 | if(subtree[pos].size() >= k) ans.insert(ind);
| ~~~~~~~~~~~~~~~~~~~~^~~~
railway.cpp: In function 'void solve()':
railway.cpp:111:16: warning: unused variable 'y' [-Wunused-variable]
111 | int x, y, lc;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
41596 KB |
Output is correct |
2 |
Correct |
3 ms |
12892 KB |
Output is correct |
3 |
Correct |
108 ms |
40364 KB |
Output is correct |
4 |
Correct |
104 ms |
38768 KB |
Output is correct |
5 |
Correct |
92 ms |
40728 KB |
Output is correct |
6 |
Correct |
90 ms |
40788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
79 ms |
33104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
79 ms |
33104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |