QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#107753#5307. Subgraph IsomorphismshihoghmeanWA 61ms114000kbC++173.3kb2023-05-22 18:43:442023-10-15 17:25:40

Judging History

你现在查看的是最新测评结果

  • [2023-10-15 17:25:40]
  • 管理员手动重测本题所有提交记录
  • 测评结果:WA
  • 用时:61ms
  • 内存:114000kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-22 18:43:47]
  • 评测
  • 测评结果:0
  • 用时:127ms
  • 内存:96928kb
  • [2023-05-22 18:43:44]
  • 提交

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;
    }
	for(int i=2;i<=10000000;i++){
		if(!p[i]) prime[++tot1]=i;
		for(int j=1;j<=tot1&&i*prime[j]<=10000000;j++){
			p[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
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[i];
    }
    fr(i,top,1){
        Hash1[i]=Hash1[i+1]*base+f[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: 100
Accepted
time: 50ms
memory: 112740kb

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
NO
YES

result:

ok 4 token(s): yes count is 3, no count is 1

Test #2:

score: -100
Wrong Answer
time: 61ms
memory: 114000kb

input:

33192
2 1
1 2
3 2
1 3
2 3
3 3
1 2
1 3
2 3
4 3
1 4
2 4
3 4
4 3
1 3
1 4
2 4
4 4
1 3
1 4
2 4
3 4
4 4
1 3
1 4
2 3
2 4
4 5
1 3
1 4
2 3
2 4
3 4
4 6
1 2
1 3
1 4
2 3
2 4
3 4
5 4
1 5
2 5
3 5
4 5
5 4
1 4
1 5
2 5
3 5
5 5
1 4
1 5
2 5
3 5
4 5
5 5
1 4
1 5
2 4
3 5
4 5
5 5
1 4
1 5
2 4
2 5
3 5
5 6
1 4
1 5
2 4
2 5
3 ...

output:

YES
YES
YES
YES
YES
YES
YES
NO
NO
YES
YES
YES
YES
YES
NO
NO
NO
YES
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
YES
YES
YES
YES
YES
YES
YES
NO
YES
NO
NO
NO
NO
NO
YES
NO
NO
YES
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
YES
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
NO
...

result:

wrong answer expected NO, found YES [6th token]