QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#380585 | #5307. Subgraph Isomorphism | kevinyang# | WA | 50ms | 51388kb | C++17 | 3.4kb | 2024-04-07 06:15:41 | 2024-04-07 06:15:42 |
Judging History
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] *= (h[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;
vector<int>path;
while(x!=u){
vis[x] = true;
path.push_back(x);
x = dp[x][0];
}
vector<int>path2;
while(y!=u){
path2.push_back(y);
vis[y] = true;
y = dp[y][0];
}
path.push_back(u);
while(path2.size()){
path.push_back(path2.back());
path2.pop_back();
}
vector<int>vals;
for(int nxt: path){
dfs1(nxt,0);
vals.push_back(hsh[nxt]);
}
bool good = true;
vals.push_back(vals[0]);
vals.push_back(vals[1]);
for(int i = 2; i<vals.size(); i++){
if(vals[i]!=vals[i-2]){
good = false;
}
}
if(good){
cout << "YES\n";
}
else{
cout << "NO\n";
}
reset(n);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 24ms
memory: 51388kb
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: 50ms
memory: 51372kb
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]