QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#115872 | #5307. Subgraph Isomorphism | PetroTarnavskyi# | WA | 36ms | 15568kb | C++17 | 1.9kb | 2023-06-27 16:43:59 | 2023-06-27 16:44:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int N = 400447;
const int mod = 1e9 + 9;
const int p = 47;
VI g[N];
int pw[N];
int par[N];
bool used[N];
VI cycl;
int add(int a, int b)
{
a += b;
if (a >= mod)
a -= mod;
return a;
}
int mult(int a, int b)
{
return LL(a) * b % mod;
}
bool dfs1(int v, int from = -1)
{
par[v] = from;
used[v] = 1;
bool ok = false;
for (auto to : g[v])
{
if (to == from) continue;
if (used[to])
{
cycl.PB(to);
while (v != to)
{
cycl.PB(v);
v = par[v];
}
return true;
}
else
ok |= dfs1(to, v);
if (ok) return ok;
}
return false;
}
int dfs2(int v)
{
used[v] = 1;
VI vec;
for (auto to : g[v])
if (!used[to])
vec.PB(dfs2(to));
sort(ALL(vec));
int hs = pw[47];
FOR (i, 0, SZ(vec)) hs = add(hs, mult(pw[i], vec[i]));
return hs;
}
void solve()
{
int n, m;
cin >> n >> m;
FOR (i, 0, m)
{
int a, b;
cin >> a >> b;
a--, b--;
g[a].PB(b);
g[b].PB(a);
}
if (m == n - 1)
{
cout << "YES\n";
}
else if (m > n)
{
cout << "NO\n";
}
else
{
dfs1(0, -1);
FOR (i, 0, n) used[i] = 0;
set<int> s;
for (auto v : cycl) used[v] = 1;
for (auto v : cycl) s.insert(dfs2(v));
cout << (SZ(s) < 2 ? "YES\n" : "NO\n");
}
cycl.clear();
FOR (i, 0, n)
{
g[i].clear();
used[i] = 0;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
pw[0] = 1;
FOR (i, 1, N) pw[i] = mult(pw[i - 1], p);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 14800kb
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: 36ms
memory: 15568kb
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]