#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<set>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
using namespace std;
struct Node{
int v,w,next;
}a[10005];
int p[10005],e;
int n;
void qxx(int u,int v,int w){
a[++e].v=v;
a[e].w=w;
a[e].next=p[u];
p[u]=e;
}
int dfn[10005],c,low[10005],g[10005],total;
bool in[10005];
stack<int> s;
int tarjin(int u){
dfn[u]=low[u]=++c;
in[u]=1;
s.push(u);
for(int i=p[u];i;i=a[i].next){
int v=a[i].v;
if(!dfn[v]){
tarjin(v);
low[u]=min(low[u],low[v]);
}
else if(in[v]){
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
total++;
int i;
do{
i=s.top();
in[i]=0;
g[i]=total;
s.pop();
}while(i!=u);
}
}
}
int main(){
freopen("17.in","r",stdin);
freopen("17.out","w",stdout);
scanf("%d",&n);
int a,b,c;
for(int i=1;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
qxx(a,b,c);
}
tarjin(1);
printf("%d",total);
return 0;
}
有向图的强连通分量 Tarjin算法
最新推荐文章于 2022-06-19 19:44:04 发布