QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#643171 | #6729. Unrooted Trie | hewanying | WA | 251ms | 21600kb | C++14 | 1.0kb | 2024-10-15 19:28:45 | 2024-10-15 19:28:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=2e5+5,inf=1e9;
int n,used[27],ct[N],tot=0,ans=0;
struct Nod{int v,w;};
vector<Nod> G[N];
void NO(){cout<<"0\n",exit(0);}
bool dfs(int u,int fa){
memset(used,0,sizeof(used));
for(auto i:G[u]){
if(used[i.w]){
if(used[i.w]==inf) return false;
if(used[i.w]==fa) ++ct[i.v],--ct[u];
else ++ct[i.v],++ct[used[i.w]],++tot;
used[i.w]=inf;
}else used[i.w]=i.v;
}
for(auto i:G[u]) if(i.v!=fa&&!dfs(i.v,u)) return false;
return true;
}
void getans(int u,int fa){
if(ct[u]==tot) ++ans;
for(auto i:G[u]) if(i.v!=fa) ct[i.v]+=ct[u],getans(i.v,u);
}
void SOLVE(){
cin>>n;ans=tot=0;
for(int i=1;i<=n;i++) G[i].clear(),ct[i]=0;
for(int i=1,u,v;i<n;i++){
char ch;cin>>u>>v>>ch;
G[u].pb({v,ch-'a'}),G[v].pb({u,ch-'a'});
}
if(!dfs(1,0)) return cout<<"0\n",void();
getans(1,0);cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int _;cin>>_;
while(_--) SOLVE();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 8276kb
input:
2 6 3 1 a 3 2 a 3 4 b 4 5 c 4 6 d 6 3 1 a 3 2 a 3 4 b 5 4 c 6 4 c
output:
2 0
result:
ok 2 number(s): "2 0"
Test #2:
score: -100
Wrong Answer
time: 251ms
memory: 21600kb
input:
1112 19 15 18 a 7 18 c 11 14 b 10 17 b 8 14 a 1 3 b 12 2 a 16 3 c 16 4 b 2 3 a 15 5 a 3 18 d 16 9 a 18 13 b 8 4 b 17 7 a 9 6 a 13 19 a 52 8 32 a 14 51 a 30 52 a 48 36 b 27 39 c 39 51 b 35 15 a 51 52 d 45 51 e 39 26 a 20 12 b 34 18 a 9 12 e 25 5 a 9 13 b 41 51 c 1 52 n 33 14 c 22 30 b 17 4 b 12 52 c ...
output:
14 3 11 0 34 17 15 3 3 0 28 70 3 4 19 11 5 8 0 7 10 0 2 0 4 19 5 0 0 35 78 4 3 3 0 10 9 48 1 4 4 0 3 0 1 1 4 5 26 0 11 1 0 3 2 0 1 8 2 5 0 1 2 17 27 1 8 0 2 0 2 54 14 91 0 1 6 11 6 2 4 2 87 2 5 0 1 2 6 0 1 3 0 19 2 4 13 39 89 3 23 46 6 7 2 4 1 25 6 8 5 56 0 2 0 0 1 49 3 1 0 2 3 9 4 36 38 2 20 1 0 2 ...
result:
wrong answer 1st numbers differ - expected: '15', found: '14'