Submission #972243

#TimeUsernameProblemLanguageResultExecution timeMemory
972243c2zi6Railway (BOI17_railway)C++14
36 / 100
117 ms28520 KiB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

VVPI gp;
VI val;
void increment(int u, int v) {
	val[u]--;
	val[v]++;
}

void getall(int u = 0, int p = -1) {
	for (auto[v, i] : gp[u]) if (v != p) {
		getall(v, u);
		val[u] += val[v];
	}
}

VI paredge;
VI tin, tout;
int tc  = 0;
VVI par; 
void dfs(int u = 0, int p = 0, int pi = -1) {
	tin[u] = tc++;
	paredge[u] = pi;
	par[u][0] = p;
	replr(i, 1, 19) par[u][i] = par[par[u][i-1]][i-1];
	for (auto[v, i] : gp[u]) if (v != p) {
		dfs(v, u, i);
	}
	tout[u] = tc++;
}
bool isanc(int u, int v) {
	return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int LCA(int u, int v) {
	if (isanc(u, v)) return u;
	reprl(i, 19, 0) if (!isanc(par[u][i], v)) u = par[u][i];
	return par[u][0];
}

void solve() {
	int n, m, k;
	cin >> n >> m >> k;
	gp = VVPI(n);
	rep(i, n-1) {
		int u, v;
		cin >> u >> v;
		u--, v--;
		gp[u].pb({v, i});
		gp[v].pb({u, i});
	}
	tin = tout = VI(n);
	par = VVI(n, VI(20));
	paredge = VI(n);
	dfs();

	val = VI(n);
	rep(i, m) {
		int s;
		cin >> s;
		VPI a(s);
		for (auto&[t, u] : a) {
			cin >> u;
			u--;
			t = tin[u];
		}
		sort(all(a));
		rep(i, s-1) {
			int lca = LCA(a[i].ss, a[i+1].ss);
			a.pb({tin[lca], lca});
		}
		sort(all(a));

		/* unique */
		VPI b{a[0]};
		for (int i = 1; i < a.size(); i++) if (a[i] != a[i-1]) b.pb(a[i]);
		a = b;

		/* for (auto&[t, u] : a) cout << u+1 << " "; cout << endl; */
		for (auto&[t, u] : a) {
			int cur = tin[u];
			while (true) {
				auto it = upper_bound(all(a), mkp(cur, (int)+2e9));
				if (it == a.end() || tin[it->ss] > tout[u]) break;
				/* cout << u+1 << " -> " << it->ss+1 << endl; */
				increment(u, it->ss);
				cur = tout[it->ss];
			}
		}
	}
	getall();
	VI ans;
	rep(u, n) if (val[u] >= k) ans.pb(paredge[u]+1);
	cout << ans.size() << endl;
	for (int x : ans) cout << x << " "; cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int t = 1;
    /* cin >> t; */
    while (t--) solve();
}

Compilation message (stderr)

railway.cpp: In function 'void getall(int, int)':
railway.cpp:41:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   41 |  for (auto[v, i] : gp[u]) if (v != p) {
      |           ^
railway.cpp: In function 'void dfs(int, int, int)':
railway.cpp:56:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |  for (auto[v, i] : gp[u]) if (v != p) {
      |           ^
railway.cpp: In function 'void solve()':
railway.cpp:91:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |   for (auto&[t, u] : a) {
      |             ^
railway.cpp:105:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |   for (int i = 1; i < a.size(); i++) if (a[i] != a[i-1]) b.pb(a[i]);
      |                   ~~^~~~~~~~~~
railway.cpp:109:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  109 |   for (auto&[t, u] : a) {
      |             ^
railway.cpp:124:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  124 |  for (int x : ans) cout << x << " "; cout << endl;
      |  ^~~
railway.cpp:124:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  124 |  for (int x : ans) cout << x << " "; cout << endl;
      |                                      ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...