QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#111296 | #5955. Symmetric Trees | yyyyxh | 25 ✓ | 57ms | 2408kb | C++17 | 2.9kb | 2023-06-06 16:59:33 | 2023-06-06 16:59:34 |
Judging History
answer
#include <cstdio>
#include <random>
#include <algorithm>
#define fi first
#define se second
using namespace std;
typedef unsigned long long ull;
mt19937_64 rng(2007329);
int read(){
char c=getchar();int x=0;
while(c<48||c>57) c=getchar();
do x=(x<<1)+(x<<3)+(c^48),c=getchar();
while(c>=48&&c<=57);
return x;
}
const int N=10003;
int n;
ull val[N],ca[26],cb[26];
ull f[N],g[N];
int col[N],sz[N];
int hd[N],ver[N<<1],nxt[N<<1],tot;
void add(int u,int v){
nxt[++tot]=hd[u];hd[u]=tot;ver[tot]=v;
}
void dfs(int u,int fa){
sz[u]=1;
f[u]=ca[col[u]];
for(int i=hd[u];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
dfs(v,u);
f[u]+=f[v];
sz[u]+=sz[v];
}
f[u]^=cb[col[u]]^val[sz[u]];
}
void calc(int u,int fa){
ull cur=ca[col[u]]+g[u];
for(int i=hd[u];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
cur+=f[v];
}
for(int i=hd[u];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
g[v]=(cur-f[v])^cb[col[u]]^val[n-sz[v]];
calc(v,u);
}
}
int eu[N],ev[N];
bool flag,del[N];
pair<ull,int> stk[N];
int tp;
bool vis[N];
int ft[N];
int rt(int x){
if(ft[x]==x) return x;
return ft[x]=rt(ft[x]);
}
void proc(int u,int fa){
tp=0;eu[u]=0;ev[u]=0;
if(fa){
stk[tp++]=make_pair(g[u],fa);
if(g[u]==f[u]) flag=1;
}
for(int i=hd[u];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
stk[tp++]=make_pair(f[v],v);
}
sort(stk,stk+tp);
/*
printf("##%d ",u);
for(int i=0;i<tp;++i) printf("%llu ",stk[i].fi);
putchar('\n');
*/
for(int i=0;i<tp;++i){
if(i+1==tp||stk[i].fi!=stk[i+1].fi){
if(eu[u]){
if(ev[u]){del[u]=1;break;}
else ev[u]=stk[i].se;
}
else eu[u]=stk[i].se;
}
else ++i;
}
int p=0;
for(int i=hd[u];i;i=nxt[i]){
int v=ver[i];
if(v==fa) continue;
proc(v,u);
}
//printf("!%d %d %d\n",u,eu[u],ev[u]);
vis[u]=1;
if(!del[u]){
if(vis[rt(eu[u])]){
int x=rt(eu[u]);
if(del[x]) del[u]=1;
else{
ft[rt(x)]=rt(u);
if(u==eu[x]) eu[u]=ev[x];
if(u==ev[x]) eu[u]=eu[x];
}
}
if(vis[rt(ev[u])]){
int x=rt(ev[u]);
if(del[x]) del[u]=1;
else{
ft[rt(x)]=rt(u);
if(u==eu[x]) ev[u]=ev[x];
if(u==ev[x]) ev[u]=eu[x];
}
}
}
//printf("#%d %d %d\n",u,eu[u],ev[u]);
if(!eu[u]&&!ev[u]&&!del[u]) flag=1;
}
void solve(int tid){
n=read();flag=0;
for(int i=0;i<26;++i) ca[i]=rng(),cb[i]=rng();
for(int i=1;i<=n;++i) val[i]=rng();
for(int i=1;i<=n;++i) ft[i]=i;
char c=getchar();
for(int i=1;i<=n;++i){
while(c<'A'||c>'Z') c=getchar();
col[i]=c-'A';c=getchar();
}
for(int i=1;i<n;++i){
int u=read(),v=read();
add(u,v);add(v,u);
}
dfs(1,0);calc(1,0);proc(1,0);
if(flag) printf("Case #%d: SYMMETRIC\n",tid);
else printf("Case #%d: NOT SYMMETRIC\n",tid);
for(int i=1;i<=n;++i) hd[i]=0,del[i]=vis[i]=0;
while(tot) ver[tot]=nxt[tot]=0,--tot;
}
int main(){
int tc=read();
for(int i=1;i<=tc;++i) solve(i);
return 0;
}
詳細信息
Subtask #1:
score: 7
Accepted
Test #1:
score: 7
Accepted
time: 0ms
memory: 1648kb
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: 57ms
memory: 2408kb
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