2333333333333333

来源:互联网 发布:网络信息安全的3个模型 编辑:程序博客网 时间:2024/06/16 16:42

//

//  main.c

//  example

//

//  Created by Adam on 15/2/2.

//  Copyright (c) 2015 Adam. All rights reserved.

//


#include <iostream>

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "algorithm"

#include <queue>

#define maxn 1000100

using namespace std;


int first[maxn];

int fir_[maxn];

int a,b,cnt,cnt_,leap;

char strall[maxn];

char str[maxn];


struct node{

   int to;

   int next;

}edge[2000005];


struct pnode{

   int to;

   int next;

}cb[2000005];



void addedge(int x,int y)

{

   edge[cnt].to=y;

    edge[cnt].next=first[x];

   first[x]=cnt++;

}


void addpoint(int x,int y)

{

   cb[cnt_].to=y;

    cb[cnt_].next=first[x];

   first[x]=cnt_++;

}


void dfs(int now,int pre,int d,int len)

{

   if(leap) return;

   if(d==len) { leap=1;return; }

   for(int i=first[now];i;i=edge[i].next)

    {

       int tmp=edge[i].to;

       if(tmp==pre) continue;

       if(str[d]==strall[tmp])

           dfs(tmp, now, d+1, len);

    }

    

}

int main()

{

   int t;

   scanf("%d",&t);

   while(t--)

    {

       int n,len;

       leap=0;

       scanf("%d",&n);

       for(int i=1;i<=n;i++)//初始化树

           first[i]=0;

       cnt=1;

       for(int i=1;i<n;i++)//建树

        {

           scanf("%d%d",&a,&b);

           addedge(a, b);

           addedge(b, a);

        }

       scanf("%s",strall+1);

        

       for(int i=0;i<=26;i++)//初始化字母表

           fir_[i]=0;

       cnt_=1;

       for(int i=1;strall[i]!=0;i++)//建字母表

           addpoint(str[i]-'a', i);

       scanf("%s",str);

        len=strlen(str);

       for(int i=fir_[str[0]-'a'];i;i=cb[i].next)//从要求的字符串的第一个字母开始搜索

        {

           dfs(cb[i].to,0,1,len);

           if(leap) break; //找到就弹出来

        }

        

       if(leap) printf("Find\n");

        elseprintf("Impossible\n");

    }

    return 0;

}

0 0