Group programming Ladder-practice set-l2-016. May the world lovers are long-lost siblings (Dfs)

Source: Internet
Author: User

Record a vegetable-driven growth.

Topic links

A look at the topic, I thought you can use the LCA algorithm to write, wrote a half-day, later found not a tree (WOC ...)
Look at the topic, not more than 5 generations, then the direct mob Search,

When querying, mark a person's ancestors for no more than 5 generations, and then search upward from another person for a tagged ancestor within 5 generations.

#include <bits/stdc++.h> using namespace std; #define ALL (v) (v). Begin (), (v). End () #define CL (A, B) memset (A,b,sizeof (a)) #define CLR Clear () #define PB push_back #defin
E MP Make_pair #define FI first #define SE second typedef pair<int,int> PII;
const int MAXN = 100000 + 10;
int SEX[MAXN],VIS[MAXN],USED[MAXN];
vector<int>tree[maxn];
    void dfs1 (int x,int cnt) {vis[x] = 1;
    if (cnt = = 4) {return;
        } for (int i = 0; i < tree[x].size (); i++) {if (!vis[tree[x][i]]) {DFS1 (tree[x][i],cnt+1);
    }}} bool Dfs2 (int x,int cnt) {if (cnt = = 5) {return false;
    } if (Vis[x]) {return true;
    } Used[x] = 1;
    BOOL ret = FALSE; for (int i = 0; i < tree[x].size (); i++) {if (!used[tree[x][i]]) {ret = (ret | | DFS2 (tree[x][i],cnt
        +1));
}} return ret;
    } int main () {int n;
        while (~SCANF ("%d", &n)) {int x, y, Z;
        Char s[10]; CL (Sex,-1);
            for (int i = 0; i < n; i++) {scanf ("%d%s%d%d", &x,s,&y,&z);
            SEX[X] = (s[0] = = ' M '? 1:0);
                if (Y! =-1) {TREE[X].PB (y);
            Sex[y] = 1;
                } if (Z! =-1) {TREE[X].PB (z);
            Sex[z] = 0;
        }} int q;scanf ("%d", &q);
            while (q--) {scanf ("%d%d", &x,&y);
            if (sex[x] = = Sex[y]) puts ("Never mind");
                else {cl (vis,0); cl (used,0);
                DFS1 (x,0);
                bool ans = DFS2 (y,0); Puts (ans? ")
            No ":" Yes ");
}}} 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.