QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620397 | #5307. Subgraph Isomorphism | Akoasm_X | WA | 11ms | 7744kb | C++20 | 2.2kb | 2024-10-07 17:56:20 | 2024-10-07 17:56:26 |
Judging History
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;
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{
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(){
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();
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);
// cout<<i<<" "<<hsh[i]<<endl;
if(!tmp) tmp = hsh[i];
else if(tmp!=hsh[i]){
fl = 1;
}
if(fl) break;
}
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: 11ms
memory: 5632kb
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]