#include<bits/stdc++.h>
using namespace std;
#define mx 50000*3
long vis[mx];
vector<long>vec[mx];
void dfs(long u)
{
vis[u]=1;
for(long i1=0;i1<vec[u].size();i1++)
{
if(vis[vec[u][i1]]==0)
dfs(vec[u][i1]);
}
}
int main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
memset(vis,0,sizeof(vis));
long node,edge,i,a,b,cnt=0;
cin>>node>>edge;
for(i=1;i<=edge;i++)
{
cin>>a>>b;
vec[a].push_back(b);
}
vector<long>tostore;
for(i=1;i<=node;i++)
{
if(vis[i]==0)
{
dfs(i);
tostore.push_back(i);
}
}
memset(vis,0,sizeof(vis));
for(i=tostore.size()-1;i>=0;i--)
{
if(vis[tostore[i]]==0)
{
dfs(tostore[i]);
cnt++;
}
}
printf("Case %ld: %ld\n",cs++,cnt);
for(i=0;i<=node;i++)
{
vec[i].clear();
}
tostore.clear();
}
}
using namespace std;
#define mx 50000*3
long vis[mx];
vector<long>vec[mx];
void dfs(long u)
{
vis[u]=1;
for(long i1=0;i1<vec[u].size();i1++)
{
if(vis[vec[u][i1]]==0)
dfs(vec[u][i1]);
}
}
int main()
{
long ts,cs=1;
cin>>ts;
while(ts--)
{
memset(vis,0,sizeof(vis));
long node,edge,i,a,b,cnt=0;
cin>>node>>edge;
for(i=1;i<=edge;i++)
{
cin>>a>>b;
vec[a].push_back(b);
}
vector<long>tostore;
for(i=1;i<=node;i++)
{
if(vis[i]==0)
{
dfs(i);
tostore.push_back(i);
}
}
memset(vis,0,sizeof(vis));
for(i=tostore.size()-1;i>=0;i--)
{
if(vis[tostore[i]]==0)
{
dfs(tostore[i]);
cnt++;
}
}
printf("Case %ld: %ld\n",cs++,cnt);
for(i=0;i<=node;i++)
{
vec[i].clear();
}
tostore.clear();
}
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন