QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#620423 | #5307. Subgraph Isomorphism | Akoasm_X | WA | 2ms | 7684kb | C++20 | 2.4kb | 2024-10-07 18:04:53 | 2024-10-07 18:04:56 |
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
詳細信息
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 7684kb
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]