Cod sursa(job #2440991)

Utilizator CharacterMeCharacter Me CharacterMe Data 19 iulie 2019 18:09:52
Problema Pioni Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <iostream>
#include <cstdio>
#include <vector>
using std::vector;
int t, n, m, i, j, k, list[30001], next[20001];
bool dp[20001], check[20001], head[20001];
vector<int> graph[20001];
void dfs(int node);
int main()
{
    freopen("pioni.in", "r", stdin);
    freopen("pioni.out", "w", stdout);
    scanf("%d%d%d", &t, &n, &m);
    for(i=1; i<=m; ++i){
        int x, y;
        scanf("%d%d", &x, &y);
        graph[x].push_back(y);
        head[y]=true;
    }
    for(i=1; i<=n; ++i) if(head[i]==false) dfs(i);
    while(t--){
        scanf("%d", &k);
        int win=0;
        for(i=1; i<=k; ++i) {
            scanf("%d", &list[i]);
            if(dp[list[i]]==true) ++win;
        }
        if(win){
            printf("Nargy\n%d ", win);
            for(i=1; i<=k; ++i) if(dp[list[i]]==true) printf("%d %d ", list[i], next[list[i]]);
            printf("\n");
        }
        else printf("Fumeanu\n");
    }
    return 0;
}
void dfs(int node){
    check[node]=true;
    for(auto j:graph[node]){
        if(check[j]==false) dfs(j);
        if(dp[j]==false && dp[node]==false) {dp[node]=true; next[node]=j;}
    }
    return;
}