Http://poj.org/problem? Id = 3256
Store the paths of N farms in an adjacent table, record the initial number of cattle in each farm, and query the number and number of cattle in each farm along the path DFS.
Code:
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Int num [1001];
Int sum [1001];
Int head [1001];
Bool vis [1001];
Int X;
Struct past {
Int V, NEX;
} Edge [2, 10001];
Void addedge (int u, int v ){
Edge [X]. V = V;
Edge [X]. NEX = head [u];
Head [u] = X;
X ++;
}
Void DFS (int A, int B ){
For (INT I = head [a]; I; I = edge [I]. NEX ){
If (vis [edge [I]. V]) continue;
Vis [edge [I]. V] = true;
Sum [edge [I]. V] + = B;
DFS (edge [I]. V, B );
}
}
Int main (){
Int n, m, K, I, U, V, ans;
While (~ Scanf ("% d", & K, & N, & M )){
Memset (Num, 0, sizeof (Num ));
Memset (sum, 0, sizeof (SUM ));
For (I = 0; I <K; I ++ ){
Scanf ("% d", & U );
Num [u] ++;
}
For (I = 0; I <m; I ++ ){
Scanf ("% d", & U, & V );
Addedge (u, v );
}
For (I = 1; I <= N; I ++ ){
Memset (VIS, false, sizeof (VIS ));
Sum [I] + = num [I];
Vis [I] = true;
DFS (I, num [I]);
}
Ans = 0;
For (I = 1; I <= N; I ++)
If (sum [I] = k) ans ++;
Printf ("% d \ n", ANS );
}
Return 0 ;}