QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#870225 | #8622. London Underground | ucup-team087# | AC ✓ | 226ms | 4224kb | C++14 | 6.4kb | 2025-01-25 15:24:11 | 2025-01-25 15:24:19 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;
constexpr int N = 426;
constexpr int M = 505;
// constexpr int N = 4;
// constexpr int M = 4;
int id;
map<string, int> tr;
char buf[1010];
int rd() {
scanf("%s", buf);
if (!tr.count(buf)) tr[buf] = id++;
return tr[buf];
}
Mint off[N], on[N];
using Bi = bitset<N>;
Bi adj[N];
void ae(int u, int v) {
assert(0 <= u); assert(u < N);
assert(0 <= v); assert(v < N);
assert(u != v);
adj[u].set(v);
adj[v].set(u);
}
Mint rec(Bi p);
Mint recConnected(Bi p);
Mint run() {
Bi all;
all.set();
return rec(all);
}
Mint rec(Bi p) {
Mint ans = 1;
for (; ; ) {
const int u = p._Find_first();
if (u >= N) break;
Bi q, r;
q.set(u);
for (; ; ) {
const int v = (q & ~r)._Find_first();
if (v >= N) break;
q |= adj[v] & p;
r.set(v);
}
ans *= recConnected(q);
p &= ~q;
}
return ans;
}
Int counter;
Mint recConnected(Bi p) {
// cerr<<"[recConnected] ";
// for(int u=0;u<N;++u)cerr<<off[u]<<","<<on[u]<<" ";
// for(int u=0;u<N;++u)cerr<<p[u];cerr<<endl;
++counter;
const int n = p.count();
if (n == 0) return 1;
if (n == 1) {
const int u = p._Find_first();
return off[u] + on[u];
}
int maxDeg = -1, um = -1;
for (Bi pp = p; ; ) {
const int u = pp._Find_first();
if (u >= N) break;
pp.reset(u);
const int deg = (adj[u] & p).count();
if (deg == 1) {
const int v = (adj[u] & p)._Find_first();
// cerr<<" u = "<<u<<", v = "<<v<<endl;
const Mint offSave = off[v];
const Mint onSave = on[v];
off[v] *= (off[u] + on[u]);
on[v] *= off[u];
p.reset(u);
const Mint res = recConnected(p);
off[v] = offSave;
on[v] = onSave;
return res;
}
if (maxDeg < deg) {
maxDeg = deg;
um = u;
}
}
p.reset(um);
Mint ret = off[um] * rec(p);
Mint prod = on[um];
Bi q = p & adj[um];
for (Bi qq = q; ; ) {
const int v = qq._Find_first();
if (v >= N) break;
prod *= off[v];
qq.reset(v);
}
ret += prod * rec(p & ~adj[um]);
return ret;
}
int main() {
scanf("%*d");
vector<int> A(M), B(M);
for (int i = 0; i < M; ++i) {
A[i] = rd();
B[i] = rd();
}
assert(id == N);
int K;
scanf("%d", &K);
vector<int> X(K);
for (int k = 0; k < K; ++k) {
X[k] = rd();
}
assert(id == N);
fill(off, off + N, 1);
fill(on, on + N, 1);
for (int k = 0; k < K; ++k) {
off[X[k]] = 0;
}
for (int i = 0; i < M; ++i) {
ae(A[i], B[i]);
}
const Mint ans = run();
cerr<<"counter = "<<counter<<endl;
printf("%u\n", ans.x);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 224ms
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:
159589981
result:
ok 1 number(s): "159589981"
Test #2:
score: 0
Accepted
time: 226ms
memory: 4224kb
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: 225ms
memory: 4224kb
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: 226ms
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:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 225ms
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:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 225ms
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: 226ms
memory: 4224kb
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: 225ms
memory: 4224kb
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: 224ms
memory: 4224kb
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: 225ms
memory: 4224kb
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: 226ms
memory: 3968kb
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: 226ms
memory: 4224kb
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: 225ms
memory: 4224kb
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