UVA 10926 How Many Dependencies? Solution
To count the dependencies, do a clean DFSfor every node and count all visited nodes.
The input is small (N <= 100) and we do not need to worry about cycles in the digraph.
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
vector<int>g[1000];
bool v[1000];
void dfs(int s)
{
v[s]=1;
for(int i=0; i<g[s].size(); i++)
{
int x=g[s][i];
if(v[x]==0)
dfs(x);
}
}
int main()
{
int t,i,n,hn,a,j,r,c,mx;
while((cin>>n)&&n!=0)
{
for(i=1; i<=n; i++)
{
g[i].clear();
cin>>hn;
while(hn--)
{
cin>>a;
g[i].push_back(a);
}
}
mx=0,r;
for(i=1; i<=n; i++)
{
memset(v,false,sizeof(v));
dfs(i);
c=0;
for(j=1; j<=n; j++)
{
c+=v[j];
}
if(c>mx)
{
mx=c;
r=i;
}
}
cout<<r<<endl;
}
}
The input is small (N <= 100) and we do not need to worry about cycles in the digraph.
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<bits/stdc++.h>
using namespace std;
vector<int>g[1000];
bool v[1000];
void dfs(int s)
{
v[s]=1;
for(int i=0; i<g[s].size(); i++)
{
int x=g[s][i];
if(v[x]==0)
dfs(x);
}
}
int main()
{
int t,i,n,hn,a,j,r,c,mx;
while((cin>>n)&&n!=0)
{
for(i=1; i<=n; i++)
{
g[i].clear();
cin>>hn;
while(hn--)
{
cin>>a;
g[i].push_back(a);
}
}
mx=0,r;
for(i=1; i<=n; i++)
{
memset(v,false,sizeof(v));
dfs(i);
c=0;
for(j=1; j<=n; j++)
{
c+=v[j];
}
if(c>mx)
{
mx=c;
r=i;
}
}
cout<<r<<endl;
}
}
No comments