QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#252256#5307. Subgraph IsomorphismricofxWA 37ms5952kbC++142.2kb2023-11-15 17:19:282023-11-15 17:19:28

Judging History

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

  • [2023-11-15 17:19:28]
  • 评测
  • 测评结果:WA
  • 用时:37ms
  • 内存:5952kb
  • [2023-11-15 17:19:28]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define ll long long
#define ull unsigned long long
int n,m,sum;
const int N = 1e5 + 10;
const ull mask = std::chrono::steady_clock::now().time_since_epoch().count();
ull shift(ull x) {
  x ^= mask;
  x ^= x << 13;
  x ^= x >> 7;
  x ^= x << 17;
  x ^= mask;
  return x;
}
ull Hash[N];
struct Node{
	int a,b;
}a[N];
std::vector<int> edge[N];
//std::set<ull> trees;
int dep[N],fa[N];
bool mark[N];
bool Mark[N];
void dfs(int x){
	for(int u:edge[x]){
		if(u==fa[x])continue;
		fa[u]=x,dep[u]=dep[x]+1;
		dfs(u);
	}
}

void getHash(int x, int p) {
	Hash[x] = 1;
	for (int i : edge[x]) {
		if (i == p || mark[i])continue;
    	getHash(i, x);
    	Hash[x] += shift(Hash[i]);
	}
}
vector<ll>ans;
void Dfs(int x){
	Mark[x]=true;
	getHash(x,0);
	ans.push_back(Hash[x]);
	for(int i : edge[x]){
		if(Mark[i]||!mark[i])continue;
		Dfs(i);
	}
	Mark[x]=false;
}
int f[N];
int gf(int x){
	return f[x]=(x==f[x]?x:gf(f[x]));
}
bool check(){
	for(int i=0;i+1<ans.size();++i)
		if(ans[i]!=ans[i+1])return false;
	return true;
}
bool Check(){
	for(int i=0;i+2<ans.size();i+=2)
		if(ans[i]!=ans[i+2])return false;
	for(int i=1;i+2<ans.size();i+=2)
		if(ans[i]!=ans[i+2])return false;
	return true;
}
void solve(){
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		cin>>a[i].a>>a[i].b;
	}
	if(m>n){
		cout<<"NO\n";
		return;
	}
	if(m==n-1){
		cout<<"YES\n";
		return;
	}
	for(int i=1;i<=n;++i)
		f[i]=i,mark[i]=false,edge[i].clear();
	int sa,sb,t;
	for(int i=1;i<=m;++i){
		int fx=gf(a[i].a),fy=gf(a[i].b);
		if(fx==fy){
			sa=a[i].a;
			t=sa;
			sb=a[i].b;
			continue;
		}
		f[fx]=fy;
		edge[a[i].a].push_back(a[i].b);
		edge[a[i].b].push_back(a[i].a);
	}
	mark[sa]=mark[sb]=true;
	dfs(1);
	if(dep[sa]<dep[sb])swap(sa,sb);
	while(dep[sa]>dep[sb]){
		sa=fa[sa];
		mark[sa]=true;
	}
	while(sa!=sb){
		mark[sa]=mark[sb]=true;
		sa=fa[sa],sb=fa[sb];
	}
	mark[sa]=true;
	ll now=0;
	ans.clear();
	Dfs(t);
	if(n&1)cout<<(check()?"YES\n":"NO\n");
	else cout<<(check()||Check()?"YES\n":"NO\n");
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5836kb

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

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

result:

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