#1174
#include<string.h>
#include<queue>
#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;
#define FOR(i,b,e) for(int i=(b);i<=(e);i++)
#define FORE(i,b,e) for(int i=(b);i>=(e);i--)
#define maxnum 100003
int inside[maxnum];
struct p{
p* next;
int val;
p* newest;
}*rel[maxnum];
int main()
{
int T,N,M,A,B;
#ifdef DEBUG_
ifstream fin("G:/1.txt");
#define cin fin
#endif
//cin>>T;
scanf("%d",&T);
FOR(i,1,T){
memset(inside,0,sizeof(inside));
memset(rel,0,sizeof(rel));
queue<int> q;
//cin>>N>>M;
scanf("%d%d",&N,&M);
FOR(j,1,M){
//cin>>A>>B;
scanf("%d%d",&A,&B);
inside[B]+=1;
if(rel[A]==NULL){
rel[A]=new p;
rel[A]->newest=rel[A];
}else{
rel[A]->newest->next=new p;
rel[A]->newest=rel[A]->newest->next;
}
rel[A]->newest->val=B;
rel[A]->newest->next=NULL;
}
FOR(i,1,N){
if(inside[i]==0)
q.push(i);
}
int num=N;
p* pi;
while(!q.empty()){
num-=1;
pi = rel[q.front()];
q.pop();
while(pi!=NULL){
inside[pi->val]-=1;
if(!inside[pi->val])
q.push(pi->val);
pi=pi->next;
}
}
if(num==0)
//cout<<"Correct"<<endl;
printf("Correct\n");
else
//cout<<"Wrong"<<endl;
printf("Wrong\n");
}
return 0;
}
#1175
#include<string.h>
#include<queue>
#include<vector>
#include<iostream>
#include<fstream>
using namespace std;
#define FOR(i,b,e) for(int i=(b);i<=(e);i++)
#define FORE(i,b,e) for(int i=(b);i>=(e);i--)
#define maxnum 100003
#define MOD 142857
int outside[maxnum];
int infect[maxnum];
struct p{
p* next;
int val;
p* newest;
}*rel[maxnum];
vector<int> virus;
queue<int> q;
int main()
{
int N,M,K,A,B;
#ifdef DEBUG_
ifstream fin("G:/1.txt");
#define cin fin
#endif
//pre
cin>>N>>M>>K;
FOR(i,1,N)
infect[i]=1;
memset(outside,0,sizeof(outside));
memset(rel,0,sizeof(rel));
FOR(i,1,K){
cin>>A;
virus.push_back(A);
}
FOR(i,1,M){
cin>>A>>B;
outside[A]+=1;
if(rel[B]==NULL){
rel[B]=new p;
rel[B]->newest=rel[B];
}else{
rel[B]->newest->next=new p;
rel[B]->newest=rel[B]->newest->next;
}
rel[B]->newest->val=A;
rel[B]->newest->next=NULL;
}
FOR(i,1,N)
if(outside[i]==0)
q.push(i);
while(!q.empty()){
A=q.front();
p* po=rel[A];
q.pop();
while(po!=NULL){
infect[po->val]=(infect[po->val]+infect[A])%MOD;
if(--outside[po->val]==0)
q.push(po->val);
po=po->next;
}
}
int ans=0;
FOR(i,0,K-1)
ans=(ans+infect[virus[i]])%MOD;
cout<<ans<<endl;
return 0;
}