Poj 3256 cow Picnic (DFS)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.