Codevs 1506 Messenger _ACM

Source: Internet
Author: User

Topic Link Click Open link


A give a message to B, B to C transmission message ... Ask if the message from a will eventually return to a.

Train of thought: judge whether the topological arrangement asks if it is ring.

DFS detonation, a known all is stored in the adjacency table of A. Enumerate the people that each a knows, Dfs.

AC Code:

#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <cmath > #include <iomanip> #include <map> #include <queue> #include <set> #define PI ACOs (-1.0) #
Define EPS 1e-8 typedef long LL;
using namespace Std;
int Vis[1505],head[1505],cnt,flag; struct node {int u,v,next;}
E[5505];
    void Init () {memset (vis,0,sizeof (VIS));
    Memset (head) (head,-1,sizeof);
cnt=0;
    } void Add_edge (int u,int v) {e[cnt].u=u;
    E[cnt].v=v;
    E[cnt].next=head[u];
head[u]=cnt++;
    } void Dfs (int sta,int x) {if (flag) return;
        if (sta==x) {flag=1;
    Return
    } vis[x]=1;
        for (int i=head[x]; I!=-1 i=e[i].next) {int tmp=e[i].v;
        if (!vis[tmp] &&!flag) DFS (STA,TMP);
    if (flag) return;
} return;
    int main () {init ();
    int n,m,i,j,u,v;
    scanf ("%d%d", &n,&m); For (i=0 i<m; i++) {scanf ("%d%d", &AMP;U,&AMP;V);
    Add_edge (U,V);
        for (I=1; i<=n; i++) {flag=0;
        memset (Vis) (vis,0,sizeof);
            for (int j=head[i]; j!=-1 j=e[j].next) {dfs (I,E[J].V);
        if (flag) break;
        } if (flag) printf ("t\n");

    else printf ("f\n");
return 0;
 }


The first handwritten adjacency table, formerly all registration, on the adjacency table understanding is not in place. Through this, also deepened the understanding of the adjacency table. Will not appear after the edge of the serial number as the vertex number of such a tease behavior ....


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.