/* Bismillahir Rahmanir Rahim
Solution-Using BFS
*/
#include<bits/stdc++.h>
#define fi(n, m) for(int i=n; i<=m; i++)
using namespace std;
vector<int>vt[1009];
int ar[100], cnt[1009], vis[1009], n, m, k, ans;
void bfs(int st){
fi( 0, n) vis[i]=0;
vis[st]=1;
queue<int>q;
q.push(st);
cnt[st]++; //counting-how many people reach here(this node)
if(cnt[st]==k) ans++; //if(counter[this node]==total people)
while(!q.empty()){
int u=q.front();
q.pop();
for(int j=0; j<vt[u].size(); j++){
int v=vt[u][j];
if(!vis[v]){
q.push(v);
vis[v]=1;
cnt[v]++; //same as previous
if(cnt[v]==k) ans++; //same as previous
}
}
}
}
int main(){
int t, cs=1, u, v;
cin>>t;
while(t--){
ans=0;
cin>>k>>n>>m;
fi(0, k-1) cin>>ar[i];
fi(1, m){
cin>>u>>v;
vt[u].push_back(v);
}
fi(0, n) cnt[i]=0;
for(int x=0; x<k; x++){
bfs(ar[x]); //BFS from all people location (node)
}
cout<<"Case "<<cs++<<": "<<ans<<endl;
fi(0, n) vt[i].clear();
}
return 0;
}
|
Saturday, March 11, 2017
Solution of Light OJ 1111 - Best Picnic Ever
Subscribe to:
Post Comments (Atom)
-
#include<bits/stdc++.h> #define ll long long using namespace std ; ll n , k , t_case ; ll bigmod ( ll b , ll p , ll m...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3...
Thank you Bhai
ReplyDeleteGreat and I have a tremendous give: How Much Is A Complete House Renovation whole home renovation cost
ReplyDeleteThank You and that i have a super proposal: What Renovations Can You Claim On Tax house renovation business
ReplyDelete