这个题,属实有点唬人,学妹问我是不是拓扑排序
就是给了一些已经有的材料,和一些and和or的条件,只有满足前缀才能获得后一种材料,问最后能有几种材料
我一开始想的是网络流带上下界,然后看了下过题人数感觉有点问题,因为那么复杂的建图不可能那么多人都掌握
结果看到数据只有1k,不知道为什么想到了bellman ford和之前的leetcode杯的那个最短路
然后忽然想到了,因为暴力模拟无论输入的条件是什么顺序,我们都可以在n次之后到达稳定状态
那就扔进set暴力模拟一下就完事儿了
也不知道Luka为什么会在这题上面翻车23333
#include <bits/stdc++.h> using namespace std; #define limit (1000000 + 5)//防止溢出 #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f #define lowbit(i) i&(-i)//一步两步 #define EPS 1e-9 #define FASTIO ios::sync_with_stdio(false);cin.tie(0),cout.tie(0); #define ff(a) printf("%d\n",a ); #define pi(a, b) pair<a,b> #define rep(i, a, b) for(ll i = a; i <= b ; ++i) #define per(i, a, b) for(ll i = b ; i >= a ; --i) #define MOD 998244353 #define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next) #define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\data.txt", "rt", stdin) #define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\akioi\\dabiao.txt", "wt", stdout) typedef long long ll; typedef unsigned long long ull; char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf; inline ll read() { #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) ll sign = 1, x = 0; char s = getchar(); while (s > '9' || s < '0') { if (s == '-')sign = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x << 3) + (x << 1) + s - '0'; s = getchar(); } return x * sign; #undef getchar }//快读 void print(ll x) { if (x / 10) print(x / 10); *O++ = x % 10 + '0'; } void write(ll x, char c = 't') { if (x < 0)putchar('-'), x = -x; print(x); if (!isalpha(c))*O++ = c; fwrite(obuf, O - obuf, 1, stdout); O = obuf; } const ll mod = 1e9 + 7; ll quickPow(ll base, ll expo) { ll ans = 1; while (expo) { if (expo & 1)(ans *= base) %= mod; expo >>= 1; base = base * base; base %= mod; } return ans % mod; } ll C(ll n, ll m) { if (n < m)return 0; ll x = 1, y = 1; if (m > n - m)m = n - m; rep(i, 0, m - 1) { x = x * (n - i) % mod; y = y * (i + 1) % mod; } return x * quickPow(y, mod - 2) % mod; } ll lucas(ll n, ll m) { return !m || n == m ? 1 : C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod; } ll fact[limit]; void calc(){ fact[0] = 1; rep(i,1,1e3)fact[i] = (fact[i - 1] * i) % mod; } ll A(ll x, ll y){ return (C(x,y) * fact[y]) % mod; } int kase; int n, m, k; int a[limit]; int tot; map<string, int>mp; int id(string x){ auto & it = mp[x]; if(!it)it = ++tot; return it; } set<pi(pi(int, int), pi(int, int))>constraint; void solve() { cin>>n; set<int>s; rep(i,1,n){ string str; cin>>str; if(str != "if"){ s.insert(id(str)); }else{ string token; cin>>token; int fst = id(token); cin>>token; string expr = token; cin>>token; int scd = id(token); cin>>token; cin>>token; int cur_id = id(token); if(expr == "and"){ if(fst > scd)swap(fst, scd); constraint.insert({{fst, scd}, {cur_id, 0}}); }else{ if(fst > scd)swap(fst, scd); constraint.insert({{fst, scd}, {cur_id, 1}}); } } } int last = -1; int cur = s.size(); rep(i,1,n<<1){ vector<pi(pi(int, int), pi(int, int))>erased; for(auto it : constraint){ auto [u, oper] = it; auto [key, op] = oper; auto [fst, scd] = u; if(!op){ if(s.count(fst) and s.count(scd)){ s.insert(key); erased.push_back(it); } }else{ if(s.count(fst) or s.count(scd)){ s.insert(key); erased.push_back(it); } } } for(auto it: erased)constraint.erase(it); } cout<<s.size()<<endl; } int32_t main() { #ifdef LOCAL FOPEN; // FOUT; #endif FASTIO // cin >> kase; // while (kase--) solve(); cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }