QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#578857 | #9349. Exchanging Gifts | EricW | RE | 0ms | 0kb | C++23 | 3.8kb | 2024-09-20 21:57:23 | 2024-09-20 21:57:23 |
answer
// Program written by EricWu ~~~
#include <bits/stdc++.h>
#define lowbit(x) (x & -x)
using namespace std;
inline char gc(void) {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}
#define gc() getchar()
template <class T> inline void read(T &x) {
T f = 1; x = 0; static char c = gc();
for (; !isdigit(c); c = gc()) if (c == '-') f = -f;
for (; isdigit(c); c = gc()) x = x * 10 + (c & 15);
if (f == -1) x = -x;
}
inline void readstr(char *s) {
do *s = gc(); while ((*s == ' ') || (*s == '\n') || (*s == '\r'));
do *(++s) = gc(); while ((~*s) && (*s != ' ') && (*s != '\n') && (*s != '\r'));
*s = 0; return;
}
inline void readch(char&x) { while (isspace(x = gc())); }
char pf[100000], *o1 = pf, *o2 = pf + 100000;
#define ot(x) (o1 == o2 ? fwrite(pf, 1, 100000, stdout), *(o1 = pf) ++= x : *o1 ++= x)
template <class T>
inline void println(T x, char c = '\n') {
if (x < 0) ot(45), x = -x;
static char s[15], *b; b = s;
if (!x) *b ++= 48;
for (; x; * b ++= x % 10 + 48, x /= 10);
for (; b-- != s; ot(*b)); ot(c);
}
template <class T> inline void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
template <class T> inline void writeln(T x, char c = '\n') { write(x); putchar(c); }
template <class T> inline void chkmax(T &x, const T y) { x > y ? x = x : x = y; }
template <class T> inline void chkmin(T &x, const T y) { x < y ? x = x : x = y; }
#define Ms(arr, opt) memset(arr, opt, sizeof(arr))
#define Mp(x, y) make_pair(x, y)
#define endl '\n'
typedef long long ll;
typedef pair <int, int> pii;
typedef array<int, 3> piii;
using i64 = long long;
const int inf = 1e9;
void solve() {
int n; read(n);
vector adj(n, vector<int>());
vector<int> ind(n, 0), dp(n, 0);
vector cnt(n, vector<int>());
vector<int> sorted;
set<int> valid;
bool flag = false;
for (int i = 0;i < n;i++) {
int opt; read(opt);
if (opt == 1) {
if (i == n - 1) flag = true;
int k; read(k); cnt[i].resize(k + 1);
cnt[i][0] = k;
for (int j = 1;j <= k;j++)
read(cnt[i][j]), sorted.emplace_back(cnt[i][j]);
} else {
int x, y; read(x), read(y); x --, y --;
adj[i].push_back(x), adj[i].push_back(y);
ind[x] ++, ind[y] ++;
}
}
sort(sorted.begin(), sorted.end());
sorted.erase(unique(sorted.begin(), sorted.end()), sorted.end());
if (flag) {
// cout << "yes\n";
int siz = cnt[n - 1][0];
vector<int> s(siz);
for (int j = 1;j <= siz;j++) {
auto idx = lower_bound(sorted.begin(), sorted.end(), cnt[n - 1][j]) - sorted.begin();
s[idx] += 1;
}
sort(s.begin(), s.end(), [](auto &x, auto &y){
return x > y;
});
i64 ans = 0, last = 0;
for (int i = 0;i < s.size();i++) {
if (s[i] >= last) ans += (last * 2), last = s[i] - last;
else ans += (s[i] * 2), last -= s[i];
}
writeln(ans);
return ;
}
queue<int> q;
q.emplace(n - 1); dp[n - 1] = 1;
while (q.size()) {
auto u = q.front(); q.pop();
if (adj[u].size() == 0) valid.insert(u);
for (auto &v : adj[u]) {
dp[v] += dp[u];
ind[v] --;
if (ind[v] == 0) q.emplace(v);
}
}
vector<int> s(sorted.size());
for (auto &it : valid) {
int siz = cnt[it][0];
for (int i = 1;i <= siz;i++) {
auto idx = lower_bound(sorted.begin(), sorted.end(), cnt[it][i]) - sorted.begin();
s[idx] += dp[it];
}
}
sort(s.begin(), s.end(), [](auto &x, auto &y) {
return x > y;
});
i64 ans = 0, last = 0;
for (int i = 0;i < s.size();i++) {
if (s[i] >= last) ans += (last * 2), last = s[i] - last;
else ans += (s[i] * 2), last -= s[i];
}
writeln(ans);
}
int main() {
int T = 1; read(T);
while (T--) solve();
system("pause");
return 0;
}
详细
Test #1:
score: 0
Dangerous Syscalls
input:
2 1 1 5 3 3 2 1 3 3 1 3 3 3 2 1 4 2 2 3 3 2 1 2