QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#627833 | #5307. Subgraph Isomorphism | Tolret | TL | 1ms | 3636kb | C++20 | 2.1kb | 2024-10-10 17:18:59 | 2024-10-10 17:19:00 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long int
#define ull unsigned long long
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int maxn=1e5+5;
const int inf=1e18;
const ull mask = mt19937_64(time(nullptr))();
ull shift(ull x) {
x ^= mask;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x ^= mask;
return x;
}
int n,m;
vector<int>edge[maxn];
bool tag[maxn];
int du[maxn];
ull tmp[maxn];
void dfs(int x,int pre)
{
tmp[x]=1;
for(auto j:edge[x])
{
if(j==pre||tag[j])continue;
dfs(j,x);
tmp[x]+=shift(tmp[j]);
}
}
int qi;vector<ull>hh;
void dfs2(int x,int pre)
{
hh.pb(tmp[x]);
for(auto j:edge[x])
{
if(!tag[j]||j==pre)continue;
if(j==qi)continue;
dfs2(j,x);
break;
}
}
void solve()
{
cin>>n>>m;hh.clear();
for(int i=1;i<=n;i++)
{
edge[i].clear();tag[i]=true;du[i]=0;
}
queue<int>q;
for(int i=1;i<=m;i++)
{
int u,v;cin>>u>>v;du[u]++;du[v]++;
edge[u].pb(v);edge[v].pb(u);
}
if(m==n-1)
{
cout<<"YES"<<'\n';return;
}
else if(m>n)
{
cout<<"NO"<<'\n';return;
}
for(int i=1;i<=n;i++)
{
if(du[i]==1)
q.push(i);
}
while(!q.empty())
{
int x=q.front();tag[x]=false;
q.pop();
for(auto j:edge[x])
{
if(du[j]==0)continue;
du[j]--;
du[x]--;
if(du[x]==1)
q.push(x);
}
}
for(int i=1;i<=n;i++)
{
if(tag[i])
dfs(i,0);
}
ull ff=0;bool ee=false;
for(int i=1;i<=n;i++)
{
if(tag[i])
{
if(ff==0)
ff=tmp[i];
else if(ff!=tmp[i])
{
ee=true;break;
}
}
}
if(!ee)
{
cout<<"YES"<<'\n';return;
}
for(int i=1;i<=n;i++)
{
if(tag[i])
{
qi=i;
dfs2(i,0);
break;
}
}
if((int)hh.size()%2)
{
cout<<"NO"<<'\n';
}
else
{
for(int i=3;i<(int)hh.size();i+=2)
{
if(hh[i]!=hh[i-2]||hh[i-1]!=hh[i-3])
{
cout<<"NO"<<'\n';return;
}
}
cout<<"YES"<<'\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cout<<fixed<<setprecision(10);
int T=1;
cin>>T;
for(int i=1;i<=T;i++)
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3636kb
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
Time Limit Exceeded
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 ...