QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244379 | #6729. Unrooted Trie | Lionel_ZQY | AC ✓ | 380ms | 41632kb | C++20 | 1.5kb | 2023-11-09 00:31:15 | 2023-11-09 00:31:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct node{
int y,c;
};
void solve(){
int n;
cin>>n;
vector<node> edge[n+1];
vector in(n+1,vector<int>(30));
vector<bool> jud(n+1);
for(int i=1;i<n;i++){
int u,v;
char c;
cin>>u>>v>>c;
int num=c-'a';
edge[u].push_back({v,num});
edge[v].push_back({u,num});
in[u][num]++,in[v][num]++;
}
for(int i=1;i<=n;i++){
for(int j=0;j<30;j++){
if(in[i][j]>2){
cout<<"0\n";
return ;
}
}
}
int ans=0,cnt=0;
in.assign(n+1,vector<int>(30));
function<void(int,int)> dfs1=[&](int u,int p){
bool ok=false;
for(auto [x,c]:edge[u]){
if(x==p) continue;
in[u][c]++;
if(in[u][c]>1) ok=true;
dfs1(x,u);
}
jud[u]=ok;
if(ok) cnt++;
};
dfs1(1,0);
if(cnt==0) ans++;
auto cal=[&](int u){
bool ok=false;
for(int i=0;i<30;i++){
if(in[u][i]>1) ok=true;
}
if(ok&&!jud[u]) cnt++;
if(!ok&&jud[u]) cnt--;
jud[u]=ok;
};
function<void(int,int)> dfs2=[&](int u,int p){
for(auto [x,c]:edge[u]){
if(x==p) continue;
in[x][c]++,in[u][c]--;
// int t=cnt;
cal(x);
cal(u);
if(cnt==0) ans++;
// cout<<u<<' '<<x<<' '<<t<<' '<<cnt<<'\n';
// for(int i=1;i<=n;i++){
// cout<<i<<' '<<jud[i]<<'\n';
// }
dfs2(x,u);
in[x][c]--,in[u][c]++;
cal(x);
cal(u);
}
};
dfs2(1,0);
cout<<ans<<'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3520kb
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: 0
Accepted
time: 380ms
memory: 41632kb
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:
15 49 22 68 34 17 28 27 3 4 34 70 37 39 19 24 58 8 16 14 10 73 73 65 35 45 33 81 46 35 78 49 22 13 26 10 33 48 47 3 9 50 8 37 15 84 23 75 26 35 35 61 65 58 30 56 11 8 39 60 88 40 56 17 42 62 12 11 2 59 22 54 14 91 87 1 80 11 45 69 80 33 87 46 56 62 54 80 7 4 48 20 55 19 9 4 38 39 89 35 63 46 24 7 52...
result:
ok 1112 numbers