QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#874056 | #8622. London Underground | Crysfly | AC ✓ | 1464ms | 65920kb | C++11 | 3.7kb | 2025-01-27 12:44:17 | 2025-01-27 12:44:18 |
Judging History
answer
#include <bits/stdc++.h>
constexpr int N = 500, mod = 998244353;
int n, m;
std::vector<int> G[N];
std::map<std::string, int> Mp;
inline int id(std::string x) {
if(Mp.count(x)) return Mp[x];
Mp[x] = ++ n;
return n;
}
void build() {
std::cin >> m;
for(int i = 1; i <= m; i ++) {
std::string x, y;
std::cin >> x >> y;
G[id(x)].emplace_back(id(y));
G[id(y)].emplace_back(id(x));
}
}
int use[N], fib[N];
int split(std::vector<int>& );
int dfs(std::vector<int>& );
std::map<std::vector<int>, int> ans;
int dfs(std::vector<int> &S) {
if(S.size() == 1) return 2;
if(ans.count(S)) return ans[S];
static int deg[N];
int max = -1, pos = -1;
for(auto u : S) {
deg[u] = 0;
for(auto v : G[u]) if(use[v] == -1) {
deg[u] ++;
}
if(deg[u] > max) {
max = deg[u], pos = u;
}
}
auto istree = [&] {
static int fa[N];
for(auto u : S) fa[u] = u;
std::function<int(int)> find = [&] (int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);};
auto merge = [&] (int u,int v) {fa[find(u)] = find(v);};
for(auto u : S) {
for(auto v : G[u]) if(use[v] == -1) {
if(find(u) == find(v)) {
return false;
}
merge(u, v);
}
}
return true;
};
if(max <= 2) {
int min = 2;
for(auto u : S) min = std::min(min, deg[u]);
if(min == 1) return fib[S.size()];
else return (fib[S.size()] - fib[std::max(0, int(S.size()) - 4)] + mod) % mod;
} else if(istree()) {
static int dp[N][2];
std::function<void(int, int)> dfs = [&] (int u, int par) {
dp[u][0] = dp[u][1] = 1;
for(auto v : G[u]) if(use[v] == -1) {
if(v == par) continue;
dfs(v, u);
dp[u][0] = 1ll * dp[u][0] * (dp[v][0] + dp[v][1]) % mod;
dp[u][1] = 1ll * dp[u][1] * dp[v][0] % mod;
}
};
dfs(1, 1);
return ans[S] = (dp[1][0] + dp[1][1]) % mod;
} else {
use[pos] = 0;
int ans1 = split(S);
use[pos] = 1;
std::vector<int> tmp;
for(auto v : G[pos]) {
if(use[v] == -1) {
tmp.emplace_back(v);
use[v] = 0;
}
}
int ans2 = split(S);
use[pos] = -1;
for(auto x : tmp) use[x] = -1;
return ans[S] = (ans1 + ans2) % mod;
}
}
int split(std::vector<int> &S) {
static int fa[N];
static std::vector<int> vec[N];
for(auto u : S) fa[u] = u;
std::function<int(int)> find = [&] (int x) {return fa[x] == x ? x : fa[x] = find(fa[x]);};
auto merge = [&] (int u,int v) {fa[find(u)] = find(v);};
for(auto u : S) if(use[u] == -1) {
for(auto v : G[u]) if(use[v] == -1) {
merge(u, v);
}
}
for(auto u : S) {
vec[u].clear();
}
for(auto u : S) if(use[u] == -1) {
vec[find(u)].emplace_back(u);
}
std::vector<std::vector<int>> con;
int ans = 1;
for(auto u : S) if(vec[u].size()) con.emplace_back(vec[u]);
for(auto c : con) {
ans = 1ll * ans * dfs(c) % mod;
}
return ans;
}
int main() {
//freopen("in.txt", "r", stdin);
std::ios::sync_with_stdio(false), std::cin.tie(0);
build();
int k;
for(int i = 1; i <= n; i ++) use[i] = -1;
std::vector<int> vec(n);
std::iota(vec.begin(), vec.end(), 1);
std::cin >> k;
for(int i = 1; i <= k; i ++) {
std::string x;
std::cin >> x;
use[id(x)] = 1;
}
fib[0] = 1;
fib[1] = 2;
for(int i = 2; i <= n; i ++) {
fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
}
for(int i = 1; i <= n; i ++) if(use[i] == 1) {
for(auto j : G[i]) {
if(use[j] == 1) {
std::cout << 0 << '\n';
exit(0);
} else {
use[j] = 0;
}
}
}
std::cout << split(vec) << '\n';
std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 99ms
memory: 8704kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
159589981
result:
ok 1 number(s): "159589981"
Test #2:
score: 0
Accepted
time: 194ms
memory: 12544kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
434129880
result:
ok 1 number(s): "434129880"
Test #3:
score: 0
Accepted
time: 1464ms
memory: 65920kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
265208483
result:
ok 1 number(s): "265208483"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 1ms
memory: 4096kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
538295197
result:
ok 1 number(s): "538295197"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
0
result:
ok 1 number(s): "0"
Test #8:
score: 0
Accepted
time: 845ms
memory: 41344kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
336817147
result:
ok 1 number(s): "336817147"
Test #9:
score: 0
Accepted
time: 98ms
memory: 8320kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
400446333
result:
ok 1 number(s): "400446333"
Test #10:
score: 0
Accepted
time: 198ms
memory: 12544kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
976542866
result:
ok 1 number(s): "976542866"
Test #11:
score: 0
Accepted
time: 0ms
memory: 4096kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
602812360
result:
ok 1 number(s): "602812360"
Test #12:
score: 0
Accepted
time: 0ms
memory: 4096kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
8
result:
ok 1 number(s): "8"
Test #13:
score: 0
Accepted
time: 1ms
memory: 4096kb
input:
505 Baker_Street Regents_Park Charing_Cross Embankment Edgware_Road__Bakerloo_ Marylebone Embankment Waterloo Harlesden Willesden_Junction Harrow_and_Wealdstone Kenton Kensal_Green Queens_Park Kenton South_Kenton Kilburn_Park Maida_Vale Lambeth_North Elephant_and_Castle Maida_Vale Warwick_Avenue Mar...
output:
1
result:
ok 1 number(s): "1"
Extra Test:
score: 0
Extra Test Passed