codevs1506 Messenger (Tarjan) _tarjan

Source: Internet
Author: User

This question many solution, I use Tarjan again to write again

#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <
Stack> using namespace std;
const int MAXN = 2000;
int n,m,to[maxn<<1],head[maxn],cd[maxn],blt[maxn],nxt[maxn<<1],tot,scc[maxn],sz[maxn],tim=0;
BOOL Vis[maxn],flag = 0; struct Edge {int from,to;}
e[maxn<<1];
Stack <int> s;
    void build (int f,int t) {To[++tot] = t;
    Nxt[tot] = head[f];
HEAD[F] = tot; 
int low[maxn],dfn[maxn],scccnt = 0;
    void Dfs (int u) {low[u] = dfn[u] = ++tim;
    S.push (U);
    int V;
        for (int i = head[u]; i = i = Nxt[i]) {v = to[i];
            if (!dfn[v]) {DFS (v);
        Low[u] = min (low[u],low[v]);
        else if (!scc[v]) {Low[u] = min (low[u],dfn[v));
        } if (low[u] = = Dfn[u]) {int x;
        Scccnt + +;
            while (!s.empty ()) {x = S.top ();
            S.pop (); SCC[X] = scccnt;
            sz[scccnt]++;
        if (U = = x) break;
    int main () {scanf ("%d%d", &n,&m);
        for (int i = 1; I <= m + +) {scanf ("%d%d", &e[i].from,& e[i].to);
    Build (E[i].from, e[i].to);
    for (int i = 1; I <= n; i + +) if (!dfn[i]) DFS (i);
    int ans = 0,q = 0;
    int xx[5000];
            for (int i = 1; I <= scccnt i + +) {if (sz[i]>1) {q + +;
        XX[Q] = i;
        for (int j = 1; j <= Q; j + +) for (int i = 1; I <= n; i + +) {if (scc[i] = = Xx[j])
        {Vis[i] = 1;
        for (int i = 1; I <= n; i + +) {if (Vis[i]) puts ("T");
    Else puts ("F");
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.