QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620438#5307. Subgraph IsomorphismAkoasm_XWA 7ms7764kbC++202.5kb2024-10-07 18:08:372024-10-07 18:08:37

Judging History

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

  • [2024-10-07 18:08:37]
  • 评测
  • 测评结果:WA
  • 用时:7ms
  • 内存:7764kb
  • [2024-10-07 18:08:37]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define int long long
typedef long long LL;

inline int read(){
	int x = 0 , f = 1 ; char c = getchar() ;
    while( c < '0' || c > '9' ) { if( c == '-' ) f = -1 ; c = getchar() ; } 
    while( c >= '0' && c <= '9' ) { x = x * 10 + c - '0' ; c = getchar() ; } 
    return x * f ;
}

const int maxn = 1e5+20;
const LL mod = 9982443663;
int n,m,fl,vvv,top;
int sta[maxn];
int l[maxn],r[maxn],sz[maxn];
LL hsh[maxn];
bool vis[maxn],cir[maxn];
vector<int> E[maxn],V[maxn];

void dfs1(int x,int fa){
	r[x] = fa;
	if(vis[x]){
		fl = 1;
		int now = x;
		do{
			sta[++top] = now;
			cir[now] = 1;
			now = l[now];
		}while(now!=x);
		return ;
	}
	vis[x] = 1;
	for(int y : E[x]){
		if(y==fa) continue;
		l[x] = y;
		dfs1(y,x);
		if(fl) return ;
	}
} 

bool cmp(int a,int b){
	return hsh[a] < hsh[b];
}

void dfs2(int x,int fa){
	if(vis[x]){
//		cout<<"ddd"<<endl;
		fl = 1;
		return ;
	}
	hsh[x] = 0;
//	sz[x] = 1;
	vis[x] = 1;
	for(int y : E[x]){
		if(y==fa) continue;
		if(fa==0 && (y==l[x]||y==r[x])) continue;
		if(cir[y]){
			fl = 1;
			return ;
		}
		V[x].push_back(y);
		dfs2(y,x);
//		sz[x] += sz[y];
		if(fl) return ;
	}
	if(V[x].size()==0) hsh[x] = 1;
	else{
		sort(V[x].begin(),V[x].end(),cmp);
		hsh[x] = 1908537;
		for(int y : V[x]){
			hsh[x] = ((hsh[x] * 91107) ^ hsh[y]) % mod;
		}
	}
}

void solve(){
	vvv++;
	n = read();m = read();fl = 0;
	for(int i=1;i<=n;i++) E[i].clear(),V[i].clear();
	for(int i=1;i<=n;i++) l[i] = r[i] = vis[i] = cir[i] = hsh[i] = sz[i] = 0;
	for(int i=1;i<=m;i++){
		int x = read(),y = read();
//		if(vvv==39) cout<<x<<" "<<y<<" ";
		E[x].push_back(y);
		E[y].push_back(x);
	} 
	if(m+1==n) puts("YES");
	else if(m >= n + 1) puts("NO");
	else{
		dfs1(1,0);
		fl = 0;
		LL tmp = 0;
		for(int i=1;i<=n;i++) vis[i] = 0;
		for(int i=1;i<=n;i++){
			if(!cir[i]) continue;
			dfs2(i,0);
			if(fl) break;
		}
		if(top & 1){
			for(int i=1;i<=top;i++){
				if(hsh[sta[i]] != hsh[sta[1]]) fl = 1;
			}
		}
		else{
			for(int i=1;i<=top;i++){
	//			cout<<sta[i]<<endl;
				if(i&1){
					if(hsh[sta[i]] != hsh[sta[1]]) fl = 1;
				}
				else{
					if(hsh[sta[i]] != hsh[sta[2]]) fl = 1;
				}
			}
		}

		if(fl) puts("NO");
		else puts("YES");
	}
}

signed main(){
//	freopen("1.txt","r",stdin);
	int T = 1;
	T = read();
	while(T--) solve();
	return 0;
}

//5 5
//1 2
//2 3
//3 4
//4 1
//1 5

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7744kb

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: 7ms
memory: 7764kb

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

result:

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