QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114973 | #5955. Symmetric Trees | zhouhuanyi | 25 ✓ | 517ms | 127364kb | C++11 | 2.9kb | 2023-06-24 11:37:25 | 2023-06-24 11:37:28 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<random>
#include<cstdlib>
#include<ctime>
#define N 20000
#define mod1 10000019
#define mod2 10000079
#define mod3 10000139
#define M 11000000
using namespace std;
mt19937 RAND(time(0));
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int T,n,fa[N+1],rd1[M+1],rd2[M+1],rd3[M+1];
char c[N+1];
bool op,used[N+1],vis[N+1];
vector<int>E[N+1];
int MD1(int x)
{
return (x>=mod1)?x-mod1:x;
}
int MD2(int x)
{
return (x>=mod2)?x-mod2:x;
}
int MD3(int x)
{
return (x>=mod3)?x-mod3:x;
}
struct reads
{
int hsh1,hsh2,hsh3;
bool operator < (const reads &t)const
{
if (hsh1!=t.hsh1) return hsh1<t.hsh1;
else if (hsh2!=t.hsh2) return hsh2<t.hsh2;
else return hsh3<t.hsh3;
}
bool operator == (const reads &t)const
{
return hsh1==t.hsh1&&hsh2==t.hsh2&&hsh3==t.hsh3;
}
};
reads dp[N+1];
struct rds
{
int num;
reads data;
bool operator < (const rds &t)const
{
return data<t.data;
}
};
rds tong[N+1];
int length;
reads operator + (reads a,reads b)
{
return (reads){rd1[MD1(a.hsh1+b.hsh1)],rd2[MD2(a.hsh2+b.hsh2)],rd3[MD3(a.hsh3+b.hsh3)]};
}
void add(int x,int y)
{
E[x].push_back(y),E[y].push_back(x);
return;
}
void solve(int x)
{
int ps=1,cnt=0;
dp[x]=(reads){c[x],c[x],c[x]},vis[x]=1,length=0;
for (int i=0;i<E[x].size();++i)
if (fa[E[x][i]]==x)
tong[++length]=(rds){E[x][i],dp[E[x][i]]};
sort(tong+1,tong+length+1);
for (int i=1;i<=length;++i) dp[x]=dp[x]+tong[i].data;
while (ps<=length)
{
if (ps+1<=length&&tong[ps].data==tong[ps+1].data) ps+=2;
else vis[x]=vis[tong[ps].num],cnt++,ps++;
}
if (cnt>=2) vis[x]=0;
return;
}
void dfs(int x)
{
used[x]=1;
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i]])
fa[E[x][i]]=x,dfs(E[x][i]);
solve(x);
return;
}
void dfs2(int x)
{
used[x]=1,op|=vis[x];
for (int i=0;i<E[x].size();++i)
if (!used[E[x][i]])
fa[x]=E[x][i],fa[E[x][i]]=0,solve(x),op|=(dp[x]==dp[E[x][i]]),solve(E[x][i]),dfs2(E[x][i]),fa[E[x][i]]=x,fa[x]=0,solve(E[x][i]),solve(x);
return;
}
int main()
{
int x,y;
T=read();
for (int i=0;i<mod1;++i) rd1[i]=i;
for (int i=0;i<mod2;++i) rd2[i]=i;
for (int i=0;i<mod3;++i) rd3[i]=i;
shuffle(rd1,rd1+mod1,RAND),shuffle(rd2,rd2+mod2,RAND),shuffle(rd3,rd3+mod3,RAND);
for (int qt=1;qt<=T;++qt)
{
n=read(),op=0;
for (int i=1;i<=n;++i) used[i]=fa[i]=vis[i]=0,E[i].clear();
for (int i=1;i<=n;++i) cin>>c[i];
for (int i=1;i<=n-1;++i) x=read(),y=read(),add(x,y);
dfs(1);
for (int i=1;i<=n;++i) used[i]=0;
dfs2(1);
if (op) printf("Case #%d: SYMMETRIC\n",qt);
else printf("Case #%d: NOT SYMMETRIC\n",qt);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 331ms
memory: 127364kb
input:
100 9 I P T X E A X E A 2 8 7 9 6 8 4 6 1 3 3 5 8 9 1 2 4 S S S P 1 2 1 3 2 4 12 Q Q Q Q Q Q Q Q Q Q Q Q 1 9 3 5 2 12 9 12 8 10 5 6 5 10 2 11 3 4 2 4 7 8 6 Z H H O Q Q 1 4 2 6 4 5 4 6 3 5 6 A A A A A A 3 4 2 6 2 3 4 5 1 4 12 K H K K H H H H H H H K 3 12 7 8 6 8 7 12 7 9 2 11 2 10 1 12 4 7 2 5 5 12 9...
output:
Case #1: SYMMETRIC Case #2: SYMMETRIC Case #3: SYMMETRIC Case #4: SYMMETRIC Case #5: SYMMETRIC Case #6: NOT SYMMETRIC Case #7: SYMMETRIC Case #8: SYMMETRIC Case #9: SYMMETRIC Case #10: SYMMETRIC Case #11: SYMMETRIC Case #12: SYMMETRIC Case #13: SYMMETRIC Case #14: SYMMETRIC Case #15: SYMMETRIC Case ...
result:
ok 100 lines
Subtask #2:
score: 18
Accepted
Test #2:
score: 18
Accepted
time: 517ms
memory: 127136kb
input:
100 5771 P P P P P U P U U P P P P P U P P U P U P U P U U U U P P U P U P U P U U U P P P U P P P U P U P U P U P U U P U P U U U U P U P P P P U P U U U U U U U U P U U U P P P U U U U P P P U P P U P U P U U U U U P U P U U U U P U U U P U P U P U U P P P U U P P U P P P U P U P U P P P P P P P P...
output:
Case #1: NOT SYMMETRIC Case #2: SYMMETRIC Case #3: SYMMETRIC Case #4: SYMMETRIC Case #5: NOT SYMMETRIC Case #6: NOT SYMMETRIC Case #7: SYMMETRIC Case #8: NOT SYMMETRIC Case #9: NOT SYMMETRIC Case #10: NOT SYMMETRIC Case #11: NOT SYMMETRIC Case #12: SYMMETRIC Case #13: SYMMETRIC Case #14: NOT SYMMETR...
result:
ok 100 lines
Extra Test:
score: 0
Extra Test Passed