标签:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1066 Accepted Submission(s): 303
#include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int MAXN=10005; vector<int> arc[MAXN]; int n; int par[MAXN]; int dist[MAXN]; char value[MAXN],s[MAXN]; int len,vis[MAXN]; void dfs1(int u,int fa) { par[u]=fa; int mx=0; for(int i=0;i<arc[u].size();i++) { int to=arc[u][i]; if(to!=fa) { dfs1(to,u); mx=max(dist[to],mx); } } dist[u]=mx+1; } bool dfs2(int u,int net) { if(net==len) return true; vis[u]=1; for(int i=0;i<arc[u].size();i++) { int to=arc[u][i]; if(!vis[to]&&par[u]!=to&&value[to]==s[net]&&(len-net)<=dist[to]) { if(dfs2(to,net+1)) { return true; } } } int fa=par[u]; if(!vis[fa]&&value[fa]==s[net]) { if(dfs2(fa,net+1)) { return true; } } return false; } int main() { int T; scanf("%d",&T); for(int cas=1;cas<=T;cas++) { scanf("%d",&n); for(int i=1;i<=n;i++) arc[i].clear(); for(int i=0;i<n-1;i++) { int u,v; scanf("%d%d",&u,&v); arc[u].push_back(v); arc[v].push_back(u); } scanf("%s",value+1); scanf("%s",s); dfs1(1,0); len=strlen(s); bool tag=false; for(int i=1;i<=n;i++) { if(value[i]==s[0]) { memset(vis,0,sizeof(vis)); if(dfs2(i,1)) { tag=true; break; } } } printf("Case #%d: ",cas); if(tag) printf("Find\n"); else printf("Impossible\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/program-ccc/p/5814444.html