Cod sursa(job #2733553)

Utilizator Cristian1997Vintur Cristian Cristian1997 Data 30 martie 2021 16:42:52
Problema Pioni Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.98 kb
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<endl
#define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<endl;}
#define all(v) v.begin(), v.end()
#define fi first
#define se second
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

template<typename T1, typename T2>
ostream& operator <<(ostream &out, const pair<T1, T2> &item) {
	out << '(' << item.first << ", " << item.second << ')';
	return out;
}

template <typename T>
ostream& operator <<(ostream &out, const vector<T>& v) {
	for(const auto &item : v) out << item << ' ';
	return out;
}

const int N = 20010;

int mex[N], r[N];
vector<int> adj[N];

int getMex(vector<int> &vals) {
	sort(all(vals));
	vals.erase(unique(all(vals)), vals.end());
	
	for(int i = 0; ; ++i)
		if(i >= vals.size() || vals[i] != i) return i;
}

void calc(int v) {
	if(mex[v] != -1) return;

	vector<int> vals;
	for(auto u : adj[v]) {
		calc(u);
		vals.push_back(mex[u]);
		if(mex[u] == 0) r[v] = u;
	}

	mex[v] = getMex(vals);
}

int main()
{
	ios_base::sync_with_stdio(false);
	freopen("pioni.in", "r", stdin);
	freopen("pioni.out", "w", stdout);
	
	int t, m, n, x, y, v, nr;

	cin >> t >> n >> m;
	for(int i = 0; i < m; ++i) {
		cin >> x >> y;
		adj[x].push_back(y);
	}

	memset(mex, -1, sizeof mex);
	for(int i = 1; i <= n; ++i) calc(i);

	while(t--) {
		vector<int> nodes;

		for(cin >> nr; nr; --nr) {
			cin >> v;
			if(mex[v] > 0) nodes.push_back(v);
		}

		if(!nodes.empty()) {
			cout << "Nargy" << '\n';
			cout << nodes.size();
			for(auto v : nodes) cout << ' ' << v << ' ' << r[v];
			cout << '\n';
		} else {
			cout << "Fumeanu" << '\n';
		}
	}

	return 0;
}