QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#380542#5307. Subgraph Isomorphismkevinyang#WA 53ms51724kbC++173.1kb2024-04-07 05:39:282024-04-07 05:39:29

Judging History

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

  • [2024-04-07 05:39:29]
  • 评测
  • 测评结果:WA
  • 用时:53ms
  • 内存:51724kb
  • [2024-04-07 05:39:28]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = (int)1e9+7;
mt19937_64 rng(std::chrono::system_clock::now().time_since_epoch().count());
int mxn = 200005;
vector<vector<int>>adj(mxn); int ln = 20;
vector<vector<int>>dp(mxn,vector<int>(ln));
vector<int>lvl(mxn);
vector<bool>vis(mxn);
vector<int>h(mxn);
vector<int>hsh(mxn);
void dfs(int u, int p){
	lvl[u] = lvl[p]+1;
	dp[u][0] = p;
	for(int i = 1; i<ln; i++){
		dp[u][i] = dp[dp[u][i-1]][i-1];
	}
	for(int nxt: adj[u]){
		if(nxt==p)continue;
		dfs(nxt,u);
	}
}
int lca(int x, int y){
	if(lvl[x]<lvl[y])swap(x,y);
	int dif = lvl[x]-lvl[y];
	for(int i = 0; i<ln; i++){
		if((1<<i)&dif){
			x = dp[x][i];
		}
	}
	if(x==y)return x;
	for(int i = ln-1; i>=0; i--){
		if(dp[x][i]!=dp[y][i]){
			x = dp[x][i]; y = dp[y][i];
		}
	}
	return dp[x][0];
}
int dist(int x, int y){
	int u = lca(x,y);
	return lvl[x]+lvl[y]-2*lvl[u];
}
void reset(int n){
	for(int i = 1; i<=n; i++){
		adj[i].clear();
		for(int j = 0; j<ln; j++){
			dp[i][j] = 0;
		}
		lvl[i] = 0;
		vis[i] = false;
		hsh[i] = 0;
	}
}
void dfs1(int u, int p){
	lvl[u] = 1;
	hsh[u] = 1;
	for(int nxt: adj[u]){
		if(nxt==p || vis[nxt])continue;
		dfs1(nxt,u);
		lvl[u] = max(lvl[u],lvl[nxt]+1);
	}
	for(int nxt: adj[u]){
		if(nxt==p || vis[nxt])continue;
		hsh[u] *= (lvl[u] + hsh[nxt])%mod;
		hsh[u]%=mod;
	}
}
struct DisjointSet{
	vector<int>parent,sz,mn,mx;
	int size;
	void init(int n){
		size = n;
		parent.resize(n+1); sz.resize(n+1); mx.resize(n+1); mn.resize(n+1);
		for(int i = 1; i<=n; i++){
			parent[i] = i;
			sz[i] = 1;
			mx[i] = i;
			mn[i] = i;
		}
	}
	int find(int x){
		if(parent[x]==x)return x;
		return find(parent[x]);
	}
	void Union(int x, int y){
		x = find(x); y = find(y);
		if(x==y)return;
		if(sz[x]<sz[y]){
			parent[x] = y;
			mx[y] = max(mx[y],mx[x]);
			mn[y] = min(mn[y],mn[x]);
			sz[y]+=sz[x];
		}
		else{
			parent[y] = x;
			sz[x]+=sz[y];
			mx[x] = max(mx[x],mx[y]);
			mn[x] = min(mn[x],mn[y]);
		}
	}
};
signed main(){
	cin.tie(nullptr)->sync_with_stdio(false);
	int t;
	cin >> t;
	for(int i = 0; i<mxn; i++){
		h[i] = rng()%mod;
	}
	while(t--){
		int n,m;
		cin >> n >> m;
		DisjointSet ds;
		ds.init(n+1);
		pair<int,int>p = {0,0};
		for(int i = 0; i<m; i++){
			int x,y;
			cin >> x >> y;
			if(ds.find(x)==ds.find(y)){
				p = {x,y};
			}
			else{
				ds.Union(x,y);
				adj[x].push_back(y);
				adj[y].push_back(x);
			}
		}
		if(m==n-1){
			cout << "YES\n";
			reset(n);
			continue;
		}
		if(m>=n+1){
			cout << "NO\n";
			reset(n);
			continue;
		}
		dfs(1,0);
		int x = p.first;
		int y = p.second;
		int u = lca(x,y);
		for(int i = 1; i<=n; i++){
			lvl[i] = 0;
		}
		vis[u] = true;
		while(x!=u){
			vis[x] = true;
			x = dp[x][0];
		}
		while(y!=u){
			vis[y] = true;
			y = dp[y][0];
		}
		set<int>st;
		for(int i = 1; i<=n; i++){
			if(!vis[i])continue;
			dfs1(i,0);
			st.insert(hsh[i]);
		}
		if(st.size() <= 1){
			cout << "YES\n";
		}
		else{
			cout << "NO\n";
		}
		reset(n);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 51724kb

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

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
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 YES, found NO [39th token]