QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#243755 | #6729. Unrooted Trie | gzzz | AC ✓ | 430ms | 44808kb | C++20 | 2.0kb | 2023-11-08 16:52:07 | 2023-11-08 16:52:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1e5+10;
ll dp[maxn],ans,pan[maxn];
map<char,int> mp[maxn];
vector<pair<int,char>> e[maxn];
void dfs(int x,int fa){
for(int i=0;i<e[x].size();i++){
int to=e[x][i].first;
if(to==fa) continue;
else {
dfs(to,x);
dp[x]+=dp[to];
char w=e[x][i].second;
mp[x][w]++;
}
}
for(auto it=mp[x].begin();it!=mp[x].end();it++){
if(it->second>=2) {
dp[x]++;
pan[x]=1;
break;
}
}
}
void solve(int x,int fa,int sum){
// cout<<x<<" "<<dp[x]<<" "<<sum<<"\n";
if(!dp[x]&&!sum) {
ans++;
}
for(int i=0;i<e[x].size();i++){
int to=e[x][i].first;
if(to==fa) continue;
else {
ll tmpsum=sum,tmpdp=dp[to],ori1=pan[x],ori2=pan[to];
tmpsum+=dp[x]-dp[to];
char w=e[x][i].second;
mp[to][w]++;
mp[x][w]--;
bool flag=true;
for(auto it=mp[x].begin();it!=mp[x].end()&&flag;it++){
if(it->second>=2) {
flag=false;
}
}
if(pan[x]&&flag) {
tmpsum--;
pan[x]=0;
}
flag=true;
for(auto it=mp[to].begin();it!=mp[to].end()&&flag;it++){
if(it->second>=2) {
flag=false;
}
}
if(!pan[to]&&!flag) {
pan[to]=1;
dp[to]++;
}
solve(to,x,tmpsum);
dp[to]=tmpdp;
mp[to][w]--;
mp[x][w]++;
pan[x]=ori1;
pan[to]=ori2;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;cin>>t;
while(t--) {
ans=0;
int n;cin>>n;
for(int i=1;i<=n;i++){
e[i].clear();
mp[i].clear();
dp[i]=0;
pan[i]=0;
}
for(int i=1;i<n;i++){
int fr,to;cin>>fr>>to;
char w;cin>>w;
e[fr].push_back(pair<int,char>(to,w));
e[to].push_back(pair<int,char>(fr,w));
}
dfs(1,0);
// for(int i=1;i<=n;i++){
// cout<<dp[i]<<" "<<pan[i]<<"\n";
// for(auto it=mp[i].begin();it!=mp[i].end();it++){
// cout<<it->first<<" "<<it->second<<" ";
// }
// cout<<"\n";
// }
solve(1,0,0);
cout<<ans<<"\n";
}
}
/*
1
6
3 1 a
3 2 a
3 4 b
4 5 c
4 6 d
*/
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 10900kb
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: 430ms
memory: 44808kb
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