QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#620421 | #5307. Subgraph Isomorphism | Akoasm_X | WA | 1ms | 7732kb | C++20 | 2.4kb | 2024-10-07 18:04:39 | 2024-10-07 18:04:39 |
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,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] = x;
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;
}
for(int i=1;i<=top;i++){
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: 0
Wrong Answer
time: 1ms
memory: 7732kb
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 YES YES
result:
wrong answer expected NO, found YES [3rd token]