QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#859836 | #6729. Unrooted Trie | 2018ljw# | AC ✓ | 248ms | 32540kb | C++14 | 1.7kb | 2025-01-18 02:02:42 | 2025-01-18 02:02:42 |
Judging History
answer
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
struct edg{
int x,y,val;
}r[100001];
int n;
char s[2];
vector<int>g[100001];
int dfx[100001],pos[100001],cnt;
int sz[100001],fa[100001];
int cl[100001],cr[100001];
int resp[100001][26];
void dfs0(int x,int fr){
dfx[++cnt]=x;
pos[x]=cnt;
sz[x]=1;
fa[x]=fr;
int i;
for(i=0;i<g[x].size();i++){
int y=g[x][i];
if(y==fr)continue;
dfs0(y,x);
sz[x]+=sz[y];
}
}
int num,sum[100002];
void dfs1(int x){
int i;
for(i=0;i<g[x].size();i++){
int y=g[x][i];
if(y!=fa[x])dfs1(y);
}
if(!cl[x])return;
num++;
int p=cl[x],q=cr[x],l,r;
if(p!=fa[x])sum[pos[p]]++,sum[pos[p]+sz[p]]--;
else sum[1]++,sum[pos[x]]--,sum[pos[x]+sz[x]]++;
if(q!=fa[x])sum[pos[q]]++,sum[pos[q]+sz[q]]--;
else sum[1]++,sum[pos[x]]--,sum[pos[x]+sz[x]]++;
}
void solve(){
int i,j;cnt=num=0;
scanf("%d",&n);
for(i=1;i<=n;i++)g[i].resize(0);
for(i=1;i<n;i++){
int x,y;
scanf("%d%d%s",&x,&y,s);
int e=s[0]-'a';
g[x].push_back(y);
g[y].push_back(x);
r[i]={x,y,e};
}
for(i=1;i<=n;i++)cl[i]=cr[i]=0;
for(i=0;i<=n;i++)sum[i]=0;
for(i=1;i<=n;i++)for(j=0;j<26;j++)resp[i][j]=0;
for(i=1;i<n;i++){
int x=r[i].x,y=r[i].y,e=r[i].val;
if(!resp[x][e])resp[x][e]=y;
else{
if(cl[x]){
printf("0\n");
return;
}
cl[x]=y;
cr[x]=resp[x][e];
}
if(!resp[y][e])resp[y][e]=x;
else{
if(cl[y]){
printf("0\n");
return;
}
cl[y]=x;
cr[y]=resp[y][e];
}
}
dfs0(1,0);
dfs1(1);
for(i=1;i<=n;i++)sum[i]+=sum[i-1];
int res=0;
for(i=1;i<=n;i++)if(sum[i]==num)res++;
printf("%d\n",res);
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10060kb
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: 248ms
memory: 32540kb
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