QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#69427 | #5307. Subgraph Isomorphism | magicduck# | WA | 1ms | 5748kb | C++14 | 3.0kb | 2022-12-27 15:38:50 | 2023-10-15 17:23:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
template <typename T> inline void read(T &F) {
int R = 1; F = 0; char CH = getchar();
for(; !isdigit(CH); CH = getchar()) if(CH == '-') R = -1;
for(; isdigit(CH); CH = getchar()) F = F * 10 + CH - 48;
F *= R;
}
inline void file(string str) {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
const int N = 1e5 + 10;
int fa[N], now, fst[N], nxt[N << 1], num[N << 1], siz[N], g[N], R, n, m;
void add(int u, int v) {
nxt[++now] = fst[u], fst[u] = now, num[now] = v;
nxt[++now] = fst[v], fst[v] = now, num[now] = u;
}
unsigned long long f[N], pw[N]; const unsigned long long B = 19260817;
void dfs(int x, int y) {
vector<pair<unsigned long long, int> > v; siz[x] = 1;
for(int i = fst[x]; i; i = nxt[i]) {
const int z = num[i];
if(z == y) continue; dfs(z, x);
v.emplace_back(f[z], siz[z]);
siz[x] += siz[z];
}
sort(v.begin(), v.end());
f[x] = 1; int s = 1;
for(auto i : v) f[x] = f[x] + i.first * pw[s], s += i.second;
}
void calc(int x) {
siz[x] = 1; g[x] = 0;
for(int i = fst[x]; i; i = nxt[i]) {
if(num[i] == fa[x]) continue;
fa[num[i]] = x;
calc(num[i]); siz[x] += siz[num[i]];
g[x] = max(g[x], siz[num[i]]);
}
g[x] = max(g[x], n - siz[x]);
if(g[x] < g[R]) R = x;
}
int get_fa(int x) {
return fa[x] == x ? x : fa[x] = get_fa(fa[x]);
}
pair<int, int> edge[N];
unsigned long long ran() {
random_shuffle(edge + 1, edge + m + 1);
for(int i = 1; i <= n + 1; i++) fst[i] = 0, fa[i] = i; now = 0;
vector<pair<int, int> > e;
for(int i = 1; i <= m; i++) {
const int x = edge[i].first, y = edge[i].second;
if(get_fa(x) == get_fa(y)) continue;
e.emplace_back(x, y); add(x, y);
fa[get_fa(x)] = get_fa(y);
}
fa[1] = R = 0; g[R] = 1e9;
calc(1); int a = 0, b = 0, r = 0;
if(g[R] == g[fa[R]]) a = R, b = fa[R], e.emplace_back(n + 1, a), e.emplace_back(n + 1, b), r = n + 1; else a = R, r = R;
now = 0;
for(int i = 1; i <= n + 1; i++) fst[i] = 0;
for(auto i : e) {
if((i.first == a && i.second == b) || (i.first == b && i.second == a)) continue;
add(i.first, i.second);
}
dfs(r, 0); return f[r];
}
int main() {
//file("test");
srand(time(0));
int T; read(T);
while(T--) {
read(n), read(m);
pw[0] = 1;
for(int i = 1; i <= n + 1; i++) pw[i] = pw[i - 1] * B;
for(int i = 1; i <= m; i++) read(edge[i].first), read(edge[i].second);
unsigned long long G = ran(); int flag = 1;
for(int i = 1; i <= 20; i++)
if(ran() != G) {
puts("NO"); flag = 0; break;
}
if(flag) puts("YES");
}
#ifdef _MagicDuck
fprintf(stderr, "# Time: %.3lf s", (double)clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5748kb
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]