zoukankan      html  css  js  c++  java
  • hihocoder_offer收割编程练习赛53_1继承顺位

    题目链接: https://hihocoder.com/contest/offers53/problem/1

    解题思路: 仔细阅读继承的规则,对照样例,就可以看出是一个前序遍历, 但是需要根据时间顺序指定遍历子节点的顺序,存储的时候按顺序存就可以了。然后把dead的人删掉。

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 
      4 const int imax_n = 100005;
      5 
      6 struct Edge
      7 {
      8     int u, v;
      9     Edge()
     10     {
     11 
     12     }
     13 };
     14 
     15 map<string, int> mp;
     16 map<int, string> names;
     17 int dead[imax_n];
     18 string king_name;
     19 int cnt_name;
     20 int n;
     21 
     22 int head[imax_n];
     23 Edge edge[imax_n];
     24 int next_edge[imax_n];
     25 int cnt;
     26 
     27 
     28 
     29 void init()
     30 {
     31     memset(head, -1, sizeof(head));
     32     memset(next_edge, -1, sizeof(next_edge));
     33     memset(dead, 0, sizeof(dead));
     34     cnt = 0;
     35     mp.clear();
     36     names.clear();
     37     cnt_name = 0;
     38 }
     39 
     40 void addEdge(int u, int v)
     41 {
     42     edge[cnt].u = u;
     43     edge[cnt].v = v;
     44     next_edge[cnt] = head[u];
     45     head[u] = cnt++;
     46 }
     47 
     48 void dfs(int u)
     49 {
     50     if (dead[u] == 0 && names[u] != king_name)
     51     {
     52         cout<<names[u]<<endl;
     53     }
     54     stack<int> S;
     55     for (int i = head[u]; i != -1; i = next_edge[i])
     56     {
     57         S.push(edge[i].v);
     58     }
     59     while (!S.empty())
     60     {
     61         int v = S.top();
     62         S.pop();
     63         dfs(v);
     64     }
     65 }
     66 
     67 int main()
     68 {
     69     init();
     70     scanf("%d", &n);
     71     cin>>king_name;
     72     mp[king_name] = 1;
     73     names[1] = king_name;
     74     cnt_name = 2;
     75     string op, father, son;
     76     int u, v;
     77     for (int i = 0; i < n; ++i)
     78     {
     79         cin>>op;
     80         if (op.compare("birth") == 0)
     81             cin>>father>>son;
     82         else
     83         {
     84             cin>>father;
     85             dead[mp[father]] = 1;
     86             continue;
     87         }
     88         if (mp.find(father)==mp.end())
     89         {
     90             mp[father] = cnt_name;
     91             names[cnt_name++] = father;
     92         }
     93         if (mp.find(son) == mp.end())
     94         {
     95             mp[son] = cnt_name;
     96             names[cnt_name++] = son;
     97         }
     98         u = mp[father];
     99         v = mp[son];
    100         addEdge(u, v);
    101     }
    102     dfs(1);
    103     return 0;
    104 }
  • 相关阅读:
    Linux:文件解压与压缩
    Linux:环境变量
    Linux:Vim
    Linux:目录&文件基本操作
    Linux:文件权限
    Linux:用户&用户组操作
    Linux:Shell 常用通配符
    Linux:常用shell快捷键
    Linux:Linux 重要人物
    架构:层次化
  • 原文地址:https://www.cnblogs.com/djingjing/p/8687728.html
Copyright © 2011-2022 走看看