CFGym - 101086J. Smooth Developer - dfs+字符串处理

J. Smooth Developer
time limit per test
8 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Noura has been recently working on developing Android Applications with coach Alaa Jrad. One of her tasks was to develop an application to help coach Alaa with teaching Geometry to his contestants, so she decided to use coach Fegla's geometry library, as it is one of the best in the region. But while using coach Fegla's geometry library, if you need the code of the function IsPointOnSegment, for example, you will also need to include the function collinear in the code. In a similar manner, the function collinear might yet require other functions to run.

Given the dependencies of each function, and the list of the functions Noura will use, help her find the names of all the functions she needs to include from the library to get her code to compile.

Input

The first line of input contains an integer T (1 ≤ T ≤ 128), the number of test cases.

The first line of each test case contains two space - separated integers N (1 ≤ N ≤ 105), the number of functions in the library, and K(1 ≤ K ≤ N) the number of the functions Noura will use. Then N lines follow, each line will be in the following format:

Si Ci Fi, 1 Fi, 2 ... Fi, Ci

Where Si represents the name of the ith function, Ci (0 ≤ Ci ≤ i) represents the number of functions the ith function depends on, then followed by a list of Ci distinct function names, each represents the name of a function the ith function depends on. It's guaranteed that all function names in the list appeared in the input before. A function can depend on itself (recursive function).

The last line of each test case contains K distinct function names, the list of the functions that Noura is going to use.

Function name doesn't exceed 15 letters and contains only lowercase and uppercase English letters.

Output

For each test case, print the functions Noura needs to include, each on a single line, following the same order of appearing in the input file.

Examples
input
2
2 1
cross 0
polygonArea 1 cross
polygonArea
6 2
intersect 0
circlePoint 0
intLines 1 intersect
circleThree 1 intersect
circleTwo 0
mec 3 circlePoint circleTwo mec
intersect mec
output
cross
polygonArea
intersect
circlePoint
circleTwo
mec
Note

Warning: large Input/Output data, be careful with certain languages.


2.题意概述:

n只球队,每只球队可能会和其他队伍进行比赛,问你最终有哪只球队没有他们的比赛

3.解题思路:

实际上就是给你n个元素,其中一些元素在一个集合中,求那些单元素的集合,考虑对球队名称先映射编号,然后对每个球队进行dfs和标记,最终没被标记的就是答案

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x7fffffff
#define maxn 1001000
#define eps 1e-6
#define pi acos(-1.0)
#define e 2.718281828459
#define mod (int)1e9 + 7;
using namespace std;
typedef long long ll;
int head[maxn], next[maxn], to[maxn], vis[maxn], cnt;
map<string, int> mp;
char name[maxn][20], teamname[20];
void addedge(int u, int v)
{
  next[cnt] = head[u];
  to[cnt] = v;
  head[u] = cnt++;
}
void dfs(int u)
{
  if (vis[u])
    return;
  vis[u] = 1;
  for (int i = head[u]; i != -1; i = next[i])
  {
    int v = to[i];
    if (!vis[v])
      dfs(v);
  }
}
int main()
{
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
  long _begin_time = clock();
#endif
  int t, n, k;
  scanf("%d", &t);
  while (t--)
  {
    memset(head, -1, sizeof(head));
    memset(vis, 0, sizeof(vis));
    mp.clear();
    cnt = 0;
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)
    {
      int num;
      scanf("%s%d", name[i], &num);
      mp[string(name[i])] = i;
      for (int j = 0; j < num; j++)
      {
        scanf("%s", teamname);
        addedge(i, mp[string(teamname)]);
      }
    }
    for (int i = 0; i < k; i++)
    {
      scanf("%s", teamname);
      dfs(mp[string(teamname)]);
    }
    for (int i = 0; i < n; i++)
    {
      if (vis[i])
        puts(name[i]);
    }
  }
#ifndef ONLINE_JUDGE
  long _end_time = clock();
  printf("time = %ld ms\n", _end_time - _begin_time);
#endif
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值