UVA 11518 - Dominos 2 Solution



Solution in C++:

///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<bits/stdc++.h>
using namespace std;
vector<int>graph[10009];
bool  visited[100009];
void dfs(int source)
{
    visited[source]=1;
    for(int i=0; i<graph[source].size(); i++)
    {
        int next=graph[source][i];
        if(visited[next]==0)
            dfs(next);
    }
}
int main()
{
    int nodes, edges,fall,t,a,c,i;
    cin>>t;
    while(t--)
    {
        cin >> nodes >> edges>>fall;
        for(i = 0; i <= nodes; i++)
            graph[i].clear(), visited[i] = 0;
        for (int i = 0; i < edges; i++)
        {
            int u, v;
            cin >> u >> v;
            graph[u].push_back(v);
        }
        while(fall--)
        {
            cin>>a;
            dfs(a);
        }
        c=0;
        for(int i=0; i<=nodes; i++)
        {
            c+=visited[i];
        }
        cout<<c<<endl;
    }

}



No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.