while (!q.empty()) { int i, j, r, c; ungao(q.front(), i, j, r, c); q.pop(); for (int k = 0; k < 4; k++) { int ii = i + dir[k][0], jj = j + dir[k][1]; int rr = r + dir[k][0], cc = c + dir[k][1]; if (fall(ii, jj)) continue; if (fall(rr, cc)) { flag[S] = true; continue; } int nxt = gao(ii, jj, rr, cc); if (vis[nxt] >= 0) continue; q.push(nxt); vis[nxt] = S; } } }
intcheck(int i, int j){ for (int r = 0; r < n; r++) for (int c = 0; c < m; c++) if (MAP[r][c] == '.') { if (i == r && j == c) continue; int msk = gao(i, j, r, c); if (vis[msk] == -1) bfs(msk); if (!flag[vis[msk]]) return0; } return1; }
voidsolve(){ scanf("%d%d", &n, &m); MAP.clear(); for (int i = 0; i < n; i++) { scanf("%s", s); MAP.push_back(string(s)); }
memset(vis, -1, sizeof(int) * (n * m * n * m + 3)); ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (MAP[i][j] == '.') ans += check(i, j); printf("%d\n", ans); }
intmain(){ int tcase; scanf("%d", &tcase); while (tcase--) solve(); return0; }
usingnamespace std; typedeflonglong LL; constint N = 1e5 + 10, M = 1e6 + 10; int din[N],dout[N]; int h[N],e[M],ne[M],idx; int n,m; int color[N]; bool st[N]; vector<int>ver[N + 1];
voidadd(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx ++; }
voidsolve() { unordered_set<LL>S; idx = 0; cin >> n >> m; for(int i = 1; i <= n; i ++) h[i] = -1, color[i] = din[i] = dout[i] = st[i] = 0; for(int i = 1; i <= m; i ++) ver[i].clear();
for(int i = 1; i <= n; i ++) { int cnt; cin >> cnt; while(cnt --) { int a; cin >> a; color[a] = i; ver[a].push_back(i); } }
for(int i = 1; i <= m; i ++) { int t = color[i]; if(!color[i]) continue; for(auto c : ver[i]) { if(c >= t) continue; LL hash = (LL)c * N + t; if(!S.count(hash)) { add(c, t); S.insert(hash); din[t] ++; dout[c] ++; } } } vector<int>path; queue<int>q;
for(int i = 1; i <= n; i ++) if(!din[i]) q.push(i); bool flag = false;
while(q.size()) { int t = q.front(); if(q.size() >= 2) { q.pop(); int tt = q.front(); st[t] = st[tt] = true; if(t > tt) path.push_back(t),path.push_back(tt); else path.push_back(tt), path.push_back(t); flag = true; break; } path.push_back(t); st[t] = true; q.pop(); for(int i = h[t]; ~i; i = ne[i]) { int j = e[i]; if(!(--din[j])) q.push(j); } }
if(!flag) cout << "No" << '\n'; else { cout << "Yes" << '\n'; for(int c : path) cout << c << ' '; for(int i = 1; i <= n; i ++) if(!st[i]) cout << i << ' '; cout << '\n'; } }
A.clear(); for (int i = 1; i <= n; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); // 把所有大小为 2 的包裹拆成两个大小为 1 的包裹 A.push_back(pii(-z, x * y)); } // 按楼层从高到低排序 sort(A.begin(), A.end());
ans = 0; // now:这一趟电梯的最高楼层 // sm:现在电梯里已经放了多少包裹 longlong now = -A[0].first, sm = 0; // 依次处理每种包裹 for (pii p : A) { sm += p.second; // 新包裹的加入导致电梯里放了超过 K 个包裹 if (sm > K) { ans += now; now = -p.first; sm -= K;
// 用除法快速处理同一种包裹 // 这里分子是 sm - 1,是为了防止 sm % K == 0 的情况,导致 now 的值出错 longlong t = (sm - 1) / K; ans += now * t; sm -= t * K; } } ans += now;
printf("%lld\n", ans); }
intmain(){ int tcase; scanf("%d", &tcase); while (tcase--) solve(); return0; }