-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy path00599.cc
41 lines (40 loc) · 814 Bytes
/
00599.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// https://uva.onlinejudge.org/external/5/599.pdf
#include<bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
int main(){
int t;
cin>>t;
string s;
getline(cin,s);
while(t--){
vvi g(26);
while(1){
getline(cin,s);
if(s[0]=='*')break;
int u=s[1]-'A',v=s[3]-'A';
g[u].push_back(v);
g[v].push_back(u);
}
getline(cin,s);
vi n(26);
for(int i=0;i<s.size();i+=2)n[s[i]-'A']=1;
vi c(26);
function<void(int)>dfs=[&](int u){
c[u]=1;
for(int v:g[u])
if(!c[v])
dfs(v);
};
int a=0,b=0;
for(int i=0;i<26;i++)
if(n[i]&&!c[i])
if(g[i].empty())a++;
else{
dfs(i);
b++;
}
cout<<"There are "<<b<<" tree(s) and "<<a<<" acorn(s).\n";
}
}