Cod sursa(job #2335022)

Utilizator AlexPop28Pop Alex-Nicolae AlexPop28 Data 3 februarie 2019 14:54:20
Problema Pioni Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <bits/stdc++.h>
#define all(cont) cont.begin(), cont.end()
#define pb push_back
#define fi first
#define se second
#define DEBUG(x) cerr << (#x) << ": " << (x) << '\n'

using namespace std;

typedef pair <int, int> pii;
typedef vector <int> vi;
typedef long long ll;
typedef unsigned long long ull;

const int NMAX = 2e4 + 5;

vi adj[NMAX];
int to[NMAX];
bool vis[NMAX];

void dfs (int node) {
  vis[node] = true;
  to[node] = -1;
  for (int &x : adj[node]) {
    if (!vis[x]) {
      dfs (x);
    }

    if (to[x] == -1) {
      to[node] = x;
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL_DEFINE
  freopen (".in", "r", stdin);
#endif

  freopen ("pioni.in", "r", stdin);
  freopen ("pioni.out", "w", stdout);

  int t, n, m;
  scanf ("%d%d%d", &t, &n, &m);
  while (m--) {
    int u, v;
    scanf ("%d%d", &u, &v);
    adj[u].pb (v);
  }

  for (int node = 1; node <= n; ++node) {
    if (!vis[node]) {
      dfs (node);
    }
//    fprintf (stderr, "node = %d, to = %d\n", node, to[node]);
  }

  while (t--) {
    int k;
    scanf ("%d", &k);

    vi ans;
    while (k--) {
      int x;
      scanf ("%d", &x);

      if (to[x] != -1) {
        ans.pb (x);
      }
    }

    if (ans.empty()) {
      puts ("Fumeanu");
    } else {
      printf ("Nargy\n%d ", ans.size());
      for (int &node : ans) {
        printf ("%d %d ", node, to[node]);
      }
      printf ("\n");
    }
  }

  fclose (stdin);
  fclose (stdout);

#ifdef LOCAL_DEFINE
  fprintf (stderr, "Time elapsed: %lf s.\n", 1.0 * clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}