Problem | Verdict | Lang | Time | Best | Rank | Submit Time |
---|---|---|---|---|---|---|
| discuss11518 - | Accepted | C++11 | 0.030 | 0.000 | 1303 | 1 mins ago |
Suggest:
- Count vertex visited from DFS
#include<bits/stdc++.h>
using namespace std;
const int N= 1000111;
vector<int> a[N];
bool visited[N];
void dfs(int u){
visited[u]= true;
for(int i=0; i<a[u].size(); i++){
int v= a[u][i];
if (not visited[v]) dfs(v);
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int t; cin >> t;
while(t--){
int n, m, l; cin >> n >> m >> l;
for(int i=1; i<=n; i++)
a[i].clear(), visited[i]= false;
int u,v;
while(m--)
cin >> u >> v, a[u].push_back(v);
while(l--)
cin >> u, dfs(u);
int cnt= 0;
for(int i=1; i<=n; i++)
if (visited[i]) cnt++;
cout<<cnt<<"\n";
}
}
No comments:
Post a Comment