QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#107759 | #5307. Subgraph Isomorphism | shihoghmean | WA | 28ms | 20436kb | C++17 | 3.2kb | 2023-05-22 18:58:12 | 2023-05-22 18:58:16 |
Judging History
answer
// Problem: G. Subgraph Isomorphism
// Contest: Codeforces - The 2022 ICPC Asia Hangzhou Regional Programming Contest
// URL: https://codeforces.com/gym/104090/problem/G
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define ll long long
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fr(i,a,b) for(int i=a;i>=b;i--)
#define py puts("YES")
#define pn puts("NO")
#define pt puts("")
#define pb push_back
#define wt(x) write(x),puts("")
#define wr(x) write(x) ,putchar(' ')
#define tx printf("fds")
#define mp make_pair
#define fi first
#define se second
inline int read(){
int x=0,k=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') k=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+ch-48;
ch=getchar();
}
return x*k;
}
void write(int x){
if(x<0){
x=-x;
putchar('-');
}
if(x>9) write(x/10);
putchar(x%10+'0');
}
int power(int x,int y,int mod){
int num=1;
while(y){
if(y&1) num=(num*x)%mod;
x=x*x%mod;
y>>=1;
}
return num;
}
int mul(int x,int y,int mod){
int num=0;
while(y){
if(y&1) num=(num+x)%mod;
x=(x+x)%mod;
y>>=1;
}
return num;
}
const int N=1e6+7,mod=998244353;
int n,m,tot,tot1;
int head[N];
struct edge{
int to,next;
}e[N];
void add(int u,int v){
e[++tot]={v,head[u]};
head[u]=tot;
}
int vis[N],is_round[N],END,siz[N],st[N],top,f[N];
int Hash[N],bases[N],base=233,Hash1[N];
int p[10000001],prime[N];
void pre(){
bases[0]=1;
for(int i=1;i<=1000000;i++){
bases[i]=bases[i-1]*base;
int x=rand(),y=rand(),z=rand();
prime[i]=x<<30+y<<15+z;
}
}
void dfs(int u,int fa){
if(vis[u]){
while(st[top]!=u){
is_round[st[top]]=1;
top--;
}
is_round[u]=1;
END=1;
return ;
}
vis[u]=1;
st[++top]=u;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa) continue;
dfs(v,u);
if(END) return ;
while(st[top]!=u){
top--;
}
}
}
void dfs1(int u,int fa){
f[u]=1;
siz[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(is_round[v]||v==fa) continue;
dfs1(v,u);
siz[u]+=siz[v];
f[u]+=f[v]*prime[siz[v]];
}
}
void dfs2(int u,int fa){
st[++top]=u;
vis[u]=1;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v==fa||vis[v]||!is_round[v]) continue;
dfs2(v,u);
}
}
bool solve(){
fo(i,1,n) vis[i]=0,is_round[i]=0,siz[i]=0,f[i]=0;
END=0;
top=0;
dfs(1,0);
top=0;
fo(u,1,n){
if(is_round[u]) dfs1(u,0);
vis[u]=0;
}
fo(u,1,n){
if(is_round[u]){
dfs2(u,0);
break;
}
}
fo(i,1,top+1) Hash[i]=0,Hash1[i]=0;
fo(i,1,top){
Hash[i]=Hash[i-1]*base+f[st[i]];
}
fr(i,top,1){
Hash1[i]=Hash1[i+1]*base+f[st[i]];
}
fo(i,2,top){
int o=(Hash[top]-Hash[i-1]*bases[top-i+1])*bases[i-1]+Hash[i-1];
if(Hash[top]!=o&&o!=Hash1[1]) return false;
}
return true;
}
signed main(){
pre();
int tt=read();
while(tt--){
n=read();m=read();
fo(i,0,tot) head[i]=0;
tot=0;
fo(i,1,m){
int x=read(),y=read();
add(x,y);add(y,x);
}
if(m==n-1){
py;
continue;
}
if(m>n){
pn;
continue;
}
if(solve()) py;
else pn;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 28ms
memory: 20436kb
input:
4 7 6 1 2 2 3 3 4 4 5 5 6 3 7 3 3 1 2 2 3 3 1 5 5 1 2 2 3 3 4 4 1 1 5 1 0
output:
YES YES YES YES
result:
wrong answer expected NO, found YES [3rd token]