标签:
★★ 输入文件:messagew.in
输出文件:messagew.out
简单对比
时间限制:1 s 内存限制:128 MB
题解:
tarjan
AC代码:
#include<cstdio> #include<vector> #include<stack> using namespace std; #define N 100010 int n,m,sd,pd,id[N],sum[N],low[N],dfn[N]; bool mark[N]; stack<int>s; vector<int>grap[N]; void tarjan(int v){ low[v]=dfn[v]=++pd; mark[v]=1; s.push(v); for(int i=0;i<grap[v].size();i++){ int w=grap[v][i]; if(!dfn[w]){ tarjan(w); low[v]=min(low[v],low[w]); } else if(mark[w]){ low[v]=min(low[v],dfn[w]); } } int u; if(low[v]==dfn[v]){ sd++; do{ u=s.top(); s.pop(); id[u]=sd; sum[sd]++; mark[u]=0; }while(u!=v); } } int main(){ freopen("messagew.in","r",stdin); freopen("messagew.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1,x,y;i<=m;i++) scanf("%d%d",&x,&y),grap[x].push_back(y); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); for(int i=1;i<=n;i++) puts(sum[id[i]]>1?"T":"F"); return 0; }
标签:
原文地址:http://www.cnblogs.com/shenben/p/5838723.html